uva 227 Puzzle
https://vjudge.net/problem/UVA-227
题意:
移空格的问题,ac的第一道final题,虽然是水题,但是还是有纪念意义。
特别要注意输出,是两个样例之间输出一个空行,最后一个样例之后是不输出空行的。pe了8次,自己体会。
代码:
1 #include <stdio.h> 2 #include <string.h> 3 4 char a[10][10]; 5 6 int main() 7 { 8 int cas = 0; 9 10 while (1) 11 { 12 13 14 gets(a[0]); 15 16 if (a[0][0] == 'Z') break; 17 18 for (int i = 1;i < 5;i++) 19 gets(a[i]); 20 21 22 23 char c; 24 25 int x,y; 26 27 for (int i = 0;i < 5;i++) 28 for (int j = 0;j < 5;j++) 29 if (a[i][j] == ' ') 30 { 31 x = i; 32 y = j; 33 break; 34 } 35 36 //printf("%d %d ** \n",x,y); 37 38 bool f = 0; 39 40 while (scanf("%c",&c) == 1) 41 { 42 if (c == '0') break; 43 44 if (f) continue; 45 46 if (c == 'A') 47 { 48 if (x - 1 < 0) 49 { 50 f = 1; 51 continue; 52 } 53 else 54 { 55 a[x][y] = a[x-1][y]; 56 a[x-1][y] = ' '; 57 x--; 58 } 59 } 60 61 if (c == 'B') 62 { 63 if (x + 1 >= 5) 64 { 65 f = 1; 66 continue; 67 } 68 else 69 { 70 a[x][y] = a[x+1][y]; 71 a[x+1][y] = ' '; 72 x++; 73 } 74 } 75 76 if (c =='L') 77 { 78 if (y - 1 < 0) 79 { 80 f = 1; 81 continue; 82 } 83 else 84 { 85 a[x][y] = a[x][y-1]; 86 a[x][y-1] = ' '; 87 y--; 88 } 89 } 90 91 if (c == 'R') 92 { 93 if (y+1 >= 5) 94 { 95 f = 1; 96 continue; 97 } 98 else 99 { 100 a[x][y] = a[x][y+1]; 101 a[x][y+1] = ' '; 102 y++; 103 } 104 } 105 } 106 107 if (cas) printf("\n"); 108 109 printf("Puzzle #%d:\n",++cas); 110 111 if (f) printf("This puzzle has no final configuration.\n"); 112 else 113 { 114 for (int i = 0;i < 5;i++) 115 { 116 for (int j = 0;j < 5;j++) 117 { 118 if (j == 0) printf("%c",a[i][j]); 119 else printf(" %c",a[i][j]); 120 } 121 122 printf("\n"); 123 } 124 125 } 126 127 getchar(); 128 } 129 130 131 132 return 0; 133 }
康复训练中~欢迎交流!