题目链接:https://vjudge.net/problem/UVA-227
题目解析:此题需要考虑以下几方面
1.每次循环后需要加入getchar();
2.操作步骤那个地方要等它全部处理完再break
代码:
```cpp
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
char ch[7][7];//分布
int fl[7][7];//标志变量,表格
char c;//中间变量
int k;//中间变量
int flag;//如果非法移动,标志变量
int ex,ey;
int cou=0;//判断是否需要空格
void shuru()
{
c=(char)k;
ch[1][1]=c;
for(int i=2; i<=5; i++)
scanf("%c",&ch[1][i]);
getchar();
for(int j=2; j<=4; j++)
{
for(int i=1; i<=5; i++)
scanf("%c",&ch[j][i]);
getchar();
}
for(int i=1; i<=5; i++)
scanf("%c",&ch[5][i]);
}
void A()
{
if(fl[ex-1][ey]==0)
{
flag=0;
return;
}
char ck;
ck=ch[ex-1][ey];
ch[ex-1][ey]=ch[ex][ey];
ch[ex][ey]=ck;
ex--;
}
void B()
{
if(fl[ex+1][ey]==0)
{
flag=0;
return;
}
char ck;
ck=ch[ex+1][ey];
ch[ex+1][ey]=ch[ex][ey];
ch[ex][ey]=ck;
ex++;
}
void R()
{
if(fl[ex][ey+1]==0)
{
flag=0;
return;
}
char ck;
ck=ch[ex][ey+1];
ch[ex][ey+1]=ch[ex][ey];
ch[ex][ey]=ck;
ey++;
}
void L()
{
if(fl[ex][ey-1]==0)
{
flag=0;
return;
}
char ck;
ck=ch[ex][ey-1];
ch[ex][ey-1]=ch[ex][ey];
ch[ex][ey]=ck;
ey--;
}
int main()
{
memset(fl,0,sizeof(fl));
for(int i=1; i<=5; i++)
for(int j=1; j<=5; j++)
fl[i][j]=1;
while((k=getchar())!=EOF)
{
cou++;
char w=(char)k;
if(w=='Z')
break;
if(cou!=1)
cout<<endl;
cout<<"Puzzle #"<<cou<<":"<<endl;
shuru();
for(int i=1; i<=5; i++)
{
for(int j=1; j<=5; j++)
if(ch[i][j]==' ')
{
ex=i;
ey=j;
break;
}
}
flag=1;
for(;;)
{
char cc;
scanf("%c",&cc);
if(cc=='0')
break;
if(flag)
{
if(cc=='A')
A();
else if(cc=='R')
R();
else if(cc=='B')
B();
else if(cc=='L')
L();
}
}
if(!flag)
cout<<"This puzzle has no final configuration."<<endl;
else
{
for(int i=1; i<=5; i++)
{
for(int j=1; j<=5; j++)
{
if(j!=1)
cout<<" ";
cout<<ch[i][j];
}
cout<<endl;
}
}
getchar();
}
return 0;
}