Black and white games
Time Limit: 10000 ms memory limit: 65536 K
Total submit: 9 accepted: 5
Case time limit: 1000 ms
Description
[Problem description]
The Board of the black and white games is composed of 4x4 square arrays. Each square of the Board contains one chess piece, eight white games and eight black ones. Each placement scheme of these 16 pieces constitutes a game state. Two squares with one public edge on the checker are called adjacent squares. A single square can have up to four adjacent squares. When playing a black and white game, each step can swap any two pieces in the adjacent square. For a given initial game status and target game status, It is programmed to calculate the shortest sequence from the initial game status change to the target game status.
[Input]
There are eight input files. The first four rows are the initial game status, and the last four rows are the target game status. The four numbers in each row represent the color of the pawns placed in the row. "0" indicates white games; "1" indicates black games.
[Output]
The first line of the output file is the number of moves n. In the next n rows, the four numbers in each row represent the positions of the two adjacent squares in this step. For example, ABCD indicates that the pawns at (a, B) on the board are replaced with those at (c, d.
Input
1111
0000
1110
0010
1010
0101
1010
0101
Output
4
1222
1424
3242
4344
Question .. Direct BFS
The following is the AC code:
#include<cstdio>#include<cstring>#include<queue>using namespace std;const int maxn = 5;char s_pos[maxn][maxn];char e_pos[maxn][maxn];int n;bool vis[140000];int dx[]= {1,-1,0,0};int dy[]= {0,0,-1,1};struct node{ char m[maxn][maxn]; int step; char ans[100][5];} start,ans;bool cheack(int x,int y){ return x>=0&&x<4&&y>=0&&y<4;}bool equal(char m[maxn][maxn]){ for(int i=0; i<4; i++) for(int j=0; j<4; j++) if(m[i][j]!=e_pos[i][j]) return false; return true;}int hash(char m[maxn][maxn]){ int res=0; for(int i=0; i<4; i++) for(int j=0; j<4; j++) { // if(m[i][j]=='1') // res+= 1<<(i*4+j); res=(res<<1)+(m[i][j]-'0'); } return res;}void bfs(){ memset(vis,false,sizeof(vis)); queue<node > q; q.push(start); int t=hash(s_pos); vis[t]=true; while(!q.empty()) { node now = q.front(); q.pop(); if(equal(now.m)){ ans=now; return ; } //printf("4344\n"); for(int i=0; i<4; i++) for(int j=0; j<4; j++) for(int k=0; k<4; k++) { node next = now; next.step+=1; int x=i+dx[k],y=j+dy[k]; if(cheack(x,y)) { char temp=next.m[i][j]; next.m[i][j]=next.m[x][y]; next.m[x][y]=temp; next.ans[next.step-1][0]=i+'1'; next.ans[next.step-1][1]=j+'1'; next.ans[next.step-1][2]=x+'1'; next.ans[next.step-1][3]=y+'1'; next.ans[next.step-1][4]='\0'; if(equal(next.m)) { // printf("%d\n",ans.step); ans=next; return ; } t=hash(next.m); if(!vis[t]) { vis[t]=true; q.push(next); } } } }}int main(){ for(int i=0; i<4; i++) scanf("%s",s_pos[i]); for(int i=0; i<4; i++) scanf("%s",e_pos[i]); for(int i=0; i<4; i++) for(int j=0; j<4; j++) start.m[i][j]=s_pos[i][j]; start.step=0; bfs(); printf("%d\n",ans.step); for(int i=0; i<ans.step; i++) printf("%s\n",ans.ans[i]); return 0;}