UVa227——Puzzle

来源:互联网 发布:linux root登录 编辑:程序博客网 时间:2024/06/05 04:05

题目的意思,按照指令移动空格到相应的位置。非法操作输出特殊的字符串。

题目可以模拟过,暴力过。

代码比较冗杂,还需改进。

下面的AC的代码:

#include <iostream>#include <cstdio>using namespace std;int main(){//freopen("277.txt", "r", stdin);char str[5][6];char s;int i, j, x, y, count = 1;bool tag = false;while(scanf("%[^\n]%*c", str[0]) != EOF)   //输入比较麻烦,用gets比较简单。{if(str[0][0] == 'Z')break;for(i = 1; i < 5; i++)scanf("%[^\n]%*c", str[i]);for(i = 0; i < 5; i++)   //找到空格的位置,如果空格在一行的最后一个,则没有输入,所以是‘\0’.{for(j = 0; j < 5; j++){if(str[i][j] == '\0')str[i][j] = ' ';if(str[i][j] == ' '){x = i; y = j;}}}bool flag = true;while(scanf("%c", &s))   //下面的是模拟{if(s == '0')break;if(!flag)continue;if(s == 'A'){if(x - 1 >= 0 && x - 1 < 5){str[x][y] = str[x - 1][y];x = x - 1;}elseflag = false;}else if(s == 'B'){if(x + 1 >= 0 && x + 1 < 5){str[x][y] = str[x + 1][y];x = x + 1;}elseflag = false;}else if(s == 'L'){if(y - 1 >= 0 && y - 1 < 5){str[x][y] = str[x][y - 1];y = y - 1;}elseflag = false;}else if(s == 'R'){if(y + 1 >= 0 && y + 1 < 5){str[x][y] = str[x][y + 1];y = y + 1;}elseflag = false;}else if(s != '\n')flag = false;}getchar();if(tag)cout << endl;tag = true;cout << "Puzzle #" << count++ << ':' << endl;if(!flag)cout << "This puzzle has no final configuration." << endl;else{str[x][y] = ' ';for(i = 0; i < 5; i++){for(j = 0; j < 5; j++)j != 4 ? cout << str[i][j] << ' ' : cout << str[i][j] << endl;}}}return 0;}


0 0
原创粉丝点击