uva227puzzle

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
using namespace std;
char puzzle[7][7];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int r,c;
//读取并存入字符数组
void read(int row,string str)
{
	for(int j=0;j<5;j++)
	{
		puzzle[row][j]=str[j];
		if(str[j]==' ')
		{
			r=row;c=j;
		}
	}
}
bool move(char turn)
{
	int flag=1;
	int t;
	if(turn=='A')t=0;
	else if(turn=='B')t=1;
	else if(turn=='L')t=2;
	else if(turn=='R')t=3;
	int x,y;
	x=r+dx[t];y=c+dy[t];
	if(x>=0&&x<5&&y>=0&&y<5)
	{
		puzzle[r][c]=puzzle[x][y];
		puzzle[x][y]=' ';
		r=x;c=y;
	}
	else flag=0;
	if(flag) return true;
	else return false;
}
int main(int argc, char const *argv[])
{
	string ss,order;
	int kase=0;
	while(1)
	{
		int len;
		getline(cin,ss);//读取
		if(ss[0]=='Z')break;
		if(kase!=0)cout<<endl;
		read(0,ss);
		for (int i = 1; i < 5; i++)
		{
			getline(cin,ss);
			read(i,ss);
		}
		order.clear();
		//读取命令
		while(1)
		{
			string od;
			cin>>od;
			order+=od;
			len=od.length();
			if(od[len-1]=='0')break;
			else continue;
		}
		int ok=1;
		len=order.length();
		//移动
		for (int i = 0; i < len-1; i++)
		{
			if(move(order[i]))continue;
			else {ok=0;break;}
		}
		cout<<"Puzzle #"<<++kase<<":"<<endl;
		//输出
		if(ok)
		{
			int i,j;
			for (i = 0; i < 5; i++)
			{
				for(j=0;j<4;j++)
				{
					cout<<puzzle[i][j]<<" ";
				}
				cout<<puzzle[i][j]<<endl;
			}
		}else cout<<"This puzzle has no final configuration."<<endl;

	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42825058/article/details/84060592