码迷,mamicode.com
首页 > 其他好文 > 详细

UVa - 227 - Puzzle

时间:2016-03-14 21:51:55      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

给空格子上下左右的互换操作,问最后是怎样的

注意一行的最后一个若是空格,需要自己加
注意读取时 操作可能分好多行,一定要读取到 0 为止

  1 #include <iostream>
  2 #include <cstring>
  3 using namespace std;
  4 char map[50][50],op[1000],c,tmp;
  5 int k,t,x,y,cnt; 
  6 bool flag;
  7 void fuc()
  8 {
  9     flag=1;
 10     for(int i=0;op[i]!=0;i++)
 11     {
 12         if(flag==0) break;
 13         else if(op[i]==A)
 14         {
 15             if(x-1>=0)
 16             {
 17                 tmp=map[x-1][y];
 18                 map[x-1][y]= ;
 19                 map[x][y]=tmp;
 20                 x--;
 21             }
 22             else flag=0;
 23         } 
 24         else if(op[i]==B)
 25         {
 26             if(x+1<=4)
 27             {
 28                 tmp=map[x+1][y];
 29                 map[x+1][y]= ;
 30                 map[x][y]=tmp;
 31                 x++;
 32             }
 33             else flag=0;
 34         }
 35         else if(op[i]==R)
 36         {
 37             if(y+1<=4)
 38             {
 39                 tmp=map[x][y+1];
 40                 map[x][y+1]= ;
 41                 map[x][y]=tmp;
 42                 y++;
 43             }
 44             else flag=0;
 45         }
 46         else if(op[i]==L)
 47         {
 48             if(y-1>=0)
 49             {
 50                 tmp=map[x][y-1];
 51                 map[x][y-1]= ;
 52                 map[x][y]=tmp;
 53                 y--;
 54             }
 55             else flag=0;
 56         }
 57     }
 58 }
 59 int main()
 60 {
 61     k=1;
 62     while(gets(map[0]))
 63     {
 64         if(map[0][0]==Z&&map[0][1]==\0) break;
 65         for(int i=1;i<5;i++) gets(map[i]);
 66         for(int i=0;i<5;i++)
 67         {
 68             if(strlen(map[i])!=5)
 69             {
 70                 map[i][4]= ;
 71                 break;
 72             }
 73         }
 74         cnt=0;
 75         while(c=getchar())
 76         {
 77             if(c<=Z&&c>=A) op[cnt++]=c;
 78             if(c==0)
 79             {
 80                 op[cnt++]=c;
 81                 getchar();break;  //0 之后还有一个回车需要读取!!
 82             } 
 83         }
 84         for(int i=0;i<5;i++)
 85             for(int j=0;j<5;j++)
 86                 if(map[i][j]== )
 87                 {
 88                     x=i;y=j; break;
 89                 }
 90         fuc(); 
 91         if(k>1) puts("");
 92         printf("Puzzle #%d:\n",k++);
 93         if(flag==0) puts("This puzzle has no final configuration.");
 94         else 
 95         {
 96             for(int i=0;i<5;i++)
 97             {
 98                 for(int j=0;j<4;j++)
 99                 {
100                     printf("%c ",map[i][j]);
101                 }
102                 printf("%c\n",map[i][4]);
103             }
104         }
105     }
106 }
107 /*
108 TRGSJ
109 XDOKI
110 M VLN
111 WPABE
112 UQHCF
113 ARRBBL0
114 ABCDE
115 FGHIJ
116 KLMNO
117 PQRS
118 TUVWX
119 AAA
120 LLLL0
121 ABCDE
122 FGHIJ
123 KLMNO
124 PQRS
125 TUVWX
126 AAAAABBRRRLL0
127 Z
128 */

 

UVa - 227 - Puzzle

标签:

原文地址:http://www.cnblogs.com/nicetomeetu/p/5277192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!