UVa 227 - Puzzle

来源:互联网 发布:java开发的经典书籍 编辑:程序博客网 时间:2024/05/02 06:47

题目:给你一个字母组成的矩阵,其中有一个空位,给你移动的规则(类似平面的一种拼图游戏);

            问是否操作合法,合法时输出移动后的结果。

说明:模拟。直接利用二位数组模拟即可。

分析:年后的第一题(⊙v⊙)。

#include <algorithm>#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath>using namespace std;char maps[5][7];char cmd[1001];int main(){int cases = 0;while (gets(maps[0])) {if (maps[0][0] == 'Z') break;for (int i = 1 ; i < 5 ; ++ i)gets(maps[i]);int b_x = 0,b_y = 0;for (int i = 0 ; i < 5 ; ++ i)for (int j = 0 ; j < 5 ; ++ j)if (maps[i][j] == ' ') {b_x = i;b_y = j;break;}int count = 0;while (~scanf("%c",&cmd[count]))if (cmd[count] != '0') count ++;else break;cmd[count] = 0;getchar();int flag = 0,x = b_x,y = b_y;for (int i = 0 ; cmd[i] ; ++ i) {switch(cmd[i]) {case 'A':x = b_x-1;y = b_y; break;case 'B':x = b_x+1;y = b_y; break;case 'L':x = b_x;y = b_y-1; break;case 'R':x = b_x;y = b_y+1; break;}if (x < 0 || x > 4 || y < 0 || y > 4) {flag = 1;break;}else {maps[b_x][b_y] = maps[x][y];maps[x][y] = ' ';b_x = x; b_y = y;}}if (cases ++) printf("\n");printf("Puzzle #%d:\n",cases);if (flag)printf("This puzzle has no final configuration.\n");else {for (int i = 0 ; i < 5 ; ++ i) {printf("%c",maps[i][0]);for (int j = 1 ; j < 5 ; ++ j)printf(" %c",maps[i][j]);printf("\n");}}}    return 0;}


6 0
原创粉丝点击