Puzzle
UVA - 227
题目传送门
注意点:每两个输出点间有一个换行,但最后一个输出无换行
恶心模拟题,很卡输入输出!!!
AC代码1:(自己的代码,提交时需要选择C++11)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
char mapp[1000][1000];
bool xian(int x,int y)
{if(x<0||y<0||x>=5||y>=5)return false;return true;
}
int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int cnt=0;while(1) {ms(mapp);gets(mapp[0]);if(mapp[0][0]=='Z')break;cnt++;if(cnt!=1) printf("\n");for(int i=1;i<5;i++)gets(mapp[i]);int sx,sy;for(int i=0;i<5;i++){for(int j=0;j<5;j++){if(mapp[i][j]==' '){sx=i;sy=j;}}}char c;bool ju=false;while((c=getchar())!='0'){if(c=='\n')continue;if(ju)continue;int dx,dy;switch(c){case 'A':{dx=sx-1;dy=sy;if(!xian(dx,dy))ju=true;else {mapp[sx][sy]=mapp[dx][dy];mapp[dx][dy]=' ';sx=dx;sy=dy;break;}}case 'B':{dx=sx+1;dy=sy;if(!xian(dx,dy))ju=true;else {mapp[sx][sy]=mapp[dx][dy];mapp[dx][dy]=' ';sx=dx;sy=dy;break;}}case 'R':{dx=sx;dy=sy+1;if(!xian(dx,dy))ju=true;else {mapp[sx][sy]=mapp[dx][dy];mapp[dx][dy]=' ';sx=dx;sy=dy;break;}}case 'L':{dx=sx;dy=sy-1;if(!xian(dx,dy))ju=true;else {mapp[sx][sy]=mapp[dx][dy];mapp[dx][dy]=' ';sx=dx;sy=dy;break;}}}}c=getchar();printf("Puzzle #%d:\n",cnt);if(ju)printf("This puzzle has no final configuration.\n");else {for(int i=0;i<5;i++){for(int j=0;j<4;j++){printf("%c ",mapp[i][j]);}printf("%c\n",mapp[i][4]);}}}return 0;
}
AC代码2:(大佬的代码。。。)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
char block[5][5];
int x,y;
int input()
{for(int i=0;i<5;i++) {for(int j=0;j<5;j++) {reput:scanf("%c",&block[i][j]);if(i==0&&j==0&&block[0][0]=='Z')return 1;if(block[i][j]==13||block[i][j]==10)goto reput;if(block[i][j]==32){x=i;y=j;}}}return 0;
}
int move()
{char ch;int flag=1;while((ch=getchar())!='0') {switch(ch) {case '\n':continue;case 'A':{if(x!=0) {block[x][y]=block[x-1][y];block[x-1][y]=' ';x--;}else {flag=0;}break;}case 'B':{if(x!=4) {block[x][y]=block[x+1][y];block[x+1][y]=' ';x++;}else {flag=0;}break;}case 'L':{if(y!=0) {block[x][y]=block[x][y-1];block[x][y-1]=' ';y--;} else {flag=0;}break;}case 'R':{if(y!=4) {block[x][y]=block[x][y+1];block[x][y+1]=' ';y++;}else {flag=0;}break;}}}return flag;
}
void output(int flag)
{static int count=0;count++;if(count!=1)printf("\n");printf("Puzzle #%d:\n",count);if(flag){for(int i=0;i<5;i++){for(int j=0;j<4;j++){printf("%c ",block[i][j]);}printf("%c\n",block[i][4]);}}elseprintf("This puzzle has no final configuration.\n");
}
int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);for(;;){int flag1=input();if(flag1)break;int flag2=move();output(flag2);}return 0;
}