题目链接:https://odzkskevi.qnssl.com/c142bcbb43aac35f2fe53a8b2d7dd133?v=1510619218
题意就是给你一个5*5的迷宫,有一个空格块,一共有4种指
令:A, B, L, R,分别表示把空格上、下、左、右的相邻字母移到空格中。输入初始网格和指
令序列(以数字0结束),输出指令执行完毕后的网格
记住,网格是5*5
指令是遇到数字0截止
My ugly code
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
string s[6];
int posx,posy;
int cas=1;
void sear(){
posx=0;
posy=0;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(s[i][j]==' '){
posx=i;
posy=j;
return ;
}
}
}
return ;
}
int main(){
/*freopen("input.txt","r",stdin);*/
while(getline(cin,s[0]) && s[0][0]!='Z'){
getline(cin,s[1]);
getline(cin,s[2]);
getline(cin,s[3]);
getline(cin,s[4]);
/*cout << s[0] << endl;
cout << s[1] << endl;
cout << s[2] << endl;
cout << s[3] << endl;
cout << s[4] << endl;*/
sear();
string ope;
string ppp;
while(getline(cin,ppp) && ppp[ppp.size() - 1] != '0'){
ope+=ppp;
}
ope+=ppp;
int len=ope.size();
int flag=1;
for(int i=0;i<len;i++){
if(ope[i] == 'A'){
if(posx-1 < 0){
flag=0;
break ;
}
char tmp=s[posx][posy];
s[posx][posy]=s[posx-1][posy];
s[posx-1][posy]=tmp;
posx--;
}
else if(ope[i] == 'B'){
if(posx+1 >= 5){
flag=0;
break ;
}
char tmp=s[posx][posy];
s[posx][posy]=s[posx+1][posy];
s[posx+1][posy]=tmp;
posx++;
}
else if(ope[i] == 'R'){
if(posy+1 >= 5){
flag=0;
break ;
}
char tmp=s[posx][posy];
s[posx][posy]=s[posx][posy+1];
s[posx][posy+1]=tmp;
posy++;
}
else if(ope[i] == 'L'){
if(posy-1 < 0){
flag=0;
break ;
}
char tmp=s[posx][posy];
s[posx][posy]=s[posx][posy-1];
s[posx][posy-1]=tmp;
posy--;
}
}
if(cas != 1){
printf("\n");
}
printf("Puzzle #%d:\n",cas++);
if(flag)
for(int i=0;i<5;i++){
printf("%c",s[i][0]);
for(int j=1 ; j<5 ; j++){
printf(" %c",s[i][j]);
}
printf("\n");
}
else
printf("This puzzle has no final configuration.\n");
}
return 0;
}