题目连接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=163
解题思路:
简单模拟,每走一步,交换一次,如果越界,则退出。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
typedef vector<int> vi;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e5+5;
int T;
string st;
vector<string> mpp;
int n=5,m=5;
int cas=0;
string dd;
int stx,sty;
int main()
{
while(getline(cin,st))
{
if(st[0]=='Z')
break;
cas++;
if(cas!=1)
{
cout << endl;
}
cout << "Puzzle #"<<cas<<":"<<endl;
dd.clear();
mpp.clear();
if(st.size()==3)
{
st+=" ";
}
mpp.push_back(st);
for(int i=0;i<4;i++)
{
getline(cin,st);
if(st.size()==3)
{
st+=" ";
}
mpp.push_back(st);
}
while(getline(cin,st))
{
dd+=st;
if(st[st.size()-1]=='0')
break;
}
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(mpp[i][j]==' ')
{
stx = i;
sty = j;
break;
}
}
}
//cout << stx<<" "<<sty<<mpp[stx][sty]<<"@@"<<endl;
int ff= 1;
int curx=stx, cury=sty;
for(int i=0;i<dd.size()-1;i++)
{
int d1;
if(dd[i]=='A')
d1 = 0;
else if(dd[i]=='B')
d1 = 1;
else if(dd[i]=='L')
d1 = 2;
else if(dd[i]=='R')
d1 = 3;
int xx = curx + dir[d1][0];
int yy = cury + dir[d1][1];
if(xx>=0&&xx<5&&yy>=0&&yy<5)
{
char ch ;
ch = mpp[xx][yy];
mpp[xx][yy]=mpp[curx][cury];
mpp[curx][cury] = ch;
curx = xx;
cury = yy;
}
else
{
ff = 0;
break;
}
}
if(ff)
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout << mpp[i][j];
if(j!=4) cout <<" ";
}
cout <<endl;
}
}
else
{
cout <<"This puzzle has no final configuration."<<endl;
}
}
return 0;
}