题目链接:Puzzle UVA - 227
题意:
模拟题,给定一个5*5的字母矩阵,按照给定的指令,移动字母,成功输出最后指令即可,否则输出This puzzle has no final configuration.
ac代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<iomanip>
//#define ll long long
using namespace std;
const int maxn = 10000;
int x,y;
int main()
{
string s[5],r;
int caset = 0;
while(1){
x = -1;
y = -1;
getline(cin,s[0]);
if(s[0][0] == 'Z') break;
for(int i = 1; i < 5; i++)
getline(cin,s[i]);
for(int i = 0; i < 5; i++)
{
if(s[i].length() == 4) {
s[i] += ' ';
x = i;
y = 4;
}
else{
for(int j = 0; j < 5; j++)
if(s[i][j] == ' ') {
x = i;
y = j;
break;
}
}
if(x!=-1) break;
}
cin >> r;
bool f = true;
int l = r.length();
for(int i = 0; i < l-1; i++)
{
if(r[i] == 'A')
{
x--;
if(x<0||x>4){
f = false;
break;
}
swap(s[x][y],s[x+1][y]);
} else if(r[i] == 'B'){
x++;
if(x<0||x>4){
f = false;
break;
}
swap(s[x][y],s[x-1][y]);
} else if(r[i] == 'L') {
y--;
if(y<0||y>4){
f = false;
break;
}
swap(s[x][y+1],s[x][y]);
} else if(r[i] == 'R') {
y++;
if(y<0||y>4){
f = false;
break;
}
swap(s[x][y-1],s[x][y]);
}
}
printf("Puzzle #%d:\n",++caset);
if(!f) puts("This puzzle has no final configuration.");
else {
for(int i = 0; i < 5; i++)
{
printf("%c",s[i][0]);
for(int j = 1; j < 5; j++)
printf(" %c",s[i][j]);
cout << endl;
}
}
}
return 0;
}