hdu记录状态bfs

来源:互联网 发布:java aes算法 编辑:程序博客网 时间:2024/06/08 06:56

Solitaire

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4726    Accepted Submission(s): 1424


Problem Description
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.

There are four identical pieces on the board. In one move it is allowed to:

> move a piece to an empty neighboring field (up, down, left or right),

> jump over one neighboring piece to an empty field (up, down, left or right). 



There are 4 moves allowed for each piece in the configuration shown above. As an example let's consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right.

Write a program that:

> reads two chessboard configurations from the standard input,

> verifies whether the second one is reachable from the first one in at most 8 moves,

> writes the result to the standard output.
 

Input
Each of two input lines contains 8 integers a1, a2, ..., a8 separated by single spaces and describes one configuration of pieces on the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece - the row number and the column number respectively. Process to the end of file.
 

Output
The output should contain one word for each test case - YES if a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word NO otherwise.
 

Sample Input
4 4 4 5 5 4 6 52 4 3 3 3 6 4 6
 

Sample Output
YES
 

Source
Southwestern Europe 2002
#include<stdio.h>#include<iostream>#include<string.h>#include<queue>//八维数组记录每步的状态 的宽搜 using namespace std;struct node{int x[4],y[4];int step;}s,e;bool vis[8][8][8][8][8][8][8][8];bool map[10][10];int to[4][2]={1,0,  -1,0,  0,1,  0,-1};int empty(node a,int k){for(int i=0;i<4;i++){if(i!=k&&a.x[i]==a.x[k]&&a.y[i]==a.y[k])return 0;}return 1;}int judge(node tt){for(int i=0;i<4;i++){if(tt.x[i]<0||tt.x[i]>=8||tt.y[i]<0||tt.y[i]>=8)return 1;}if(vis[tt.x[0]][tt.y[0]][tt.x[1]][tt.y[1]][tt.x[2]][tt.y[2]][tt.x[3]][tt.y[3]])return 1;return 0;}int check(node a){for(int i=0;i<4;i++){if(!map[a.x[i]][a.y[i]])return 0;}return 1;}int bfs(){memset(vis,false,sizeof(vis));queue<node> q;node t,tt;t.step=0;for(int i=0;i<4;i++){t.x[i]=s.x[i];t.y[i]=s.y[i];}q.push(t);vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]][t.x[2]][t.y[2]][t.x[3]][t.y[3]]=1;while(!q.empty()){t=q.front();q.pop();if(t.step>=8)return 0;if(check(t))return 1;for(int i=0;i<4;i++)for(int j=0;j<4;j++){tt=t;tt.x[i]+=to[j][0];tt.y[i]+=to[j][1];tt.step++;if(judge(tt))continue;if(empty(tt,i)){if(check(tt))return 1;vis[tt.x[0]][tt.y[0]][tt.x[1]][tt.y[1]][tt.x[2]][tt.y[2]][tt.x[3]][tt.y[3]]=true;q.push(tt);}else{tt.x[i]+=to[j][0];tt.y[i]+=to[j][1];if(judge(tt)||!empty(tt,i)) continue;if(check(tt)) return 1;vis[tt.x[0]][tt.y[0]][tt.x[1]][tt.y[1]][tt.x[2]][tt.y[2]][tt.x[3]][tt.y[3]]=true;q.push(tt);}}}return 0;}int main(){int i;while(scanf("%d%d",&s.x[0],&s.y[0])!=EOF){s.x[0]--;s.y[0]--;for(i=1;i<4;i++){scanf("%d%d",&s.x[i],&s.y[i]);s.x[i]--;s.y[i]--;}memset(map,false,sizeof(map));for(i=0;i<4;i++){scanf("%d%d",&e.x[i],&e.y[i]);e.x[i]--;e.y[i]--;map[e.x[i]][e.y[i]]=true;}int flag=bfs();if(flag) puts("YES");else puts("NO");}return 0;}


原创粉丝点击