题目地址: http://vj.rainng.com/vjudge/problem/viewProblem.action?id=25625

题意:有一个由24个字母和1个空格的5*5字符矩阵,然后输入操作(可能不止一行),对空格位置进行操作直到输入0为止,“A,B,L,R”分别为“上,下,左,右”,与相邻字符进行交换,若出现非法操作,则输出"This puzzle has no final configuration."结束。

一道老题,但各种跳坑。已知坑点:

1.给出的样例空格若在末尾,需要自己手动添加;

2.输出格式要求严格,从第二个样例输出开始必须与前面的输出相隔一行,具体看下图;

3.如果输入操作的过程中检测到非法,则退出后需要对剩余输入数据进行处理,以免影响后续输入操作;

4.注意各部分数据输入前回车字符的处理;

 

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define closeio std::ios::sync_with_stdio(false)
 
int main()
{
	char a[6][6],t,s;
	int x,y,i,j,k,flag,n=0;
	while(1)
	{
		for(i=0;i<5;i++)
		{
			for(j=0;j<5;j++)
			{
				scanf("%c",&a[i][j]);
				if(a[0][0]=='Z')
				{
					//cout<<i<<" "<<j<<endl;
					return 0;
				}
				if(a[i][j]==' ')
				{
					x=i;
					y=j;
				}
			}
			getchar();
		}
		flag=0;
		int xx=x,yy=y;
		while(cin>>s&&s!='0')
		{
			if(s=='0')
				break;
			if(s=='A')
			{
				xx=x-1;
				yy=y;
			}
			else if(s=='B')
			{
				xx=x+1;
				yy=y;
			}
			else if(s=='L')
			{
				xx=x;
				yy=y-1;
			}
			else if(s=='R')
			{
				xx=x;
				yy=y+1;
			}
			if(xx<0||xx>4||yy<0||yy>4) 
				flag=1;
			else
			{
				a[x][y]=a[xx][yy];
				a[xx][yy]=' ';
				x=xx;
				y=yy;
			}
		}
		getchar();
		if(n++)
			cout<<endl;
		cout<<"Puzzle #"<<n<<":"<<endl;
		if(flag)
			cout<<"This puzzle has no final configuration."<<endl;
		else
		{
			for(i=0;i<5;i++)
			{
				for(j=0;j<5;j++)
				{
					cout<<a[i][j];
					if(j<4)
						cout<<" ";
				}	
				cout<<endl;
			}	
		}
	}
	return 0;
}