博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 277 Puzzle
阅读量:6908 次
发布时间:2019-06-27

本文共 2904 字,大约阅读时间需要 9 分钟。

题意:输入5x5的字符串,输入操作,要求输出完成操作后的字符串。

注意:①输入的操作执行可能会越界,如果越界则按题目要求输出不能完成的语句。

②除了最后一次的输出外,其他输出均要在后面空一行。

③操作的最后一个换行符可能会占据str[0],需要用c来getchar()

1 #include "stdio.h"  2 #include "stdlib.h"  3 #include "string.h"  4 int noConfig=0,x,y,findEmpty=0,count=0,cmdcount=0;  5 //noConfig是指令不能执行(越界时),findEmpty是一边输入一边查找空字符的时候用,1为找到  6 //count是puzzle的计数,cmdcount是命令的计数  7 char str[5][5],cmd[100];  8 //str是输入的字符串,cmd是操作指令  9 int judge(int exi,int exj)//判断越界 10 { 11     if(exi<0||exi>=5||exj<0||exj>=5) 12     { 13         return -1; 14     } 15     return 1; 16 } 17  18 int swap(int i,int j,char cmd)//str[exi][exj]交换str[i][j]中的空字符 19 { 20      int exi,exj; 21 //     printf(" i=%d j=%d cmd=%c\n",i,j,cmd); 22      switch (cmd) 23      { 24      case 'A': 25         exi=i-1; 26         exj=j; 27         break; 28      case 'B': 29         exi=i+1; 30         exj=j; 31         break; 32      case 'L': 33         exi=i; 34         exj=j-1; 35         break; 36      case 'R': 37         exi=i; 38         exj=j+1; 39         break; 40      } 41 //     printf("exi=%d exj=%d\n",exi,exj); 42     //判断 43     if(judge(exi,exj)==-1) 44     { 45         noConfig=1; 46         return -1; 47     } 48     //交换 49     str[y][x]=str[exi][exj]; 50 //    printf(" Y,X:str[%d][%d]=%c\n",y,x,str[y][x]); 51     str[exi][exj]=' '; 52 //    printf(" EXI,EXJ:str[%d][%d]=%c\n",y,x,str[exi][exj]); 53     y=exi; 54     x=exj; 55 //    printf("after change:\n"); 56 //    for(i=0;i<5;i++) 57 //        { 58 //            for(j=0;j<5;j++) 59 //            { 60 //                printf("%c",str[i][j]); 61 //            } 62 //            printf("\n"); 63 //        } 64     return 1; 65 } 66  67 int main() 68 { 69     char c; 70     int i,j,k; 71     while(1) 72     { 73         noConfig=0; 74         cmdcount=0; 75         memset(str,'\0',sizeof(str)); 76         for(i=0;i<5;i++) 77         { 78             gets(str[i]); 79             if(str[0][0]=='Z') 80             { 81                 return 0; 82             } 83 //            for(j=0;j<5;j++)//边输入边查找 84 //            { 85 //                if(str[i][j]==' '||str[i][j]=='\0') 86 //                { 87 //                    x=j; 88 //                    y=i; 89 //                    findEmpty=1; 90 ////                    printf("Y=%d X=%d\n",y,x); 91 //                } 92 //            } 93         } 94         for(i=0;i<5;i++) 95         { 96             for(j=0;j<5;j++) 97             { 98                 if(str[i][j]==' '||str[i][j]=='\0') 99                 {100                     x=j;101                     y=i;102 //                    printf("Y=%d X=%d\n",y,x);103                 }104             }105         }106         count++;107         while((c=getchar())!='0')108         {109             cmd[cmdcount++]=c;110         }111         c=getchar();//用c取最后的换行符,不然str[0]会被换行符占据112         for(i=0;i

 

转载于:https://www.cnblogs.com/fudanxi/p/10279768.html

你可能感兴趣的文章
Maven编译出现“[ERROR] java.lang.OutOfMemoryError: Java heap space”
查看>>
通过XShell链接虚拟机的CentOS
查看>>
wmic windows
查看>>
Shiro学习(总结)
查看>>
大话设计模式C++版——建造者模式
查看>>
SharePoint 2013 隐藏部分Ribbon菜单
查看>>
[PHP] 网盘搜索引擎-采集爬取百度网盘分享文件实现网盘搜索(二)
查看>>
OCP学习基本知识点总结
查看>>
HDU1009_FatMouse&#39; Trade【贪心】【水题】
查看>>
iOS NSMutableDictionary中UIImage的存储和读取
查看>>
二叉堆
查看>>
使用cssQuery选择器语法来查找元素
查看>>
Linux IPC实践(7) --Posix消息队列
查看>>
使用python创建cocos2d-x项目
查看>>
上网管理 一些主流的视频网站
查看>>
python -- 字符串和编码
查看>>
Java中的Enum的继承
查看>>
[Android]RecyclerView的简单演示样例
查看>>
怎样在Java中运行Hive命令或HiveQL
查看>>
使用enca进行字符集转码
查看>>