HDU1401 BFS

Solitaire

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


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 5
2 4 3 3 3 6 4 6
 
Sample Output
YES
 
Source
题意:
问在8步之内能否从前一个状态到后一个状态,棋子只能上下左右走或者如图中的方式走(跳过相邻的一个棋子)。
代码:
  1 /*
  2 用bool开一个8维数组vis记录状态是否走过,很容易超内存,尽量少开数组减少内存。
  3 */
  4 #include<iostream>
  5 #include<cstring>
  6 #include<queue>
  7 using namespace std;
  8 bool vis[8][8][8][8][8][8][8][8];
  9 bool mp[8][8];
 10 int dir[4][2]={1,0,0,1,-1,0,0,-1};
 11 struct Lu
 12 {
 13     int x[4],y[4],cnt;
 14 }L3;
 15 bool chak(Lu L)
 16 {
 17     for(int i=0;i<4;i++){
 18         if(!mp[L.x[i]][L.y[i]])
 19             return 0;
 20     }
 21     return 1;
 22 }
 23 bool nice(Lu L)
 24 {
 25     for(int i=0;i<4;i++){
 26         if(L.x[i]>7||L.x[i]<0||L.y[i]>7||L.y[i]<0)
 27             return 0;
 28     }
 29     if(vis[L.x[1]][L.y[1]][L.x[2]][L.y[2]][L.x[3]][L.y[3]][L.x[4]][L.y[4]])
 30         return 0;
 31     return 1;
 32 }
 33 bool empt(Lu L,int k)
 34 {
 35     for(int i=0;i<4;i++){
 36         if(i==k) continue;
 37         if(L.x[k]==L.x[i]&&L.y[k]==L.y[i])
 38             return 0;
 39     }
 40     return 1;
 41 }
 42 bool bfs()
 43 {
 44     Lu L2;
 45     queue<Lu>q;
 46     q.push(L3);
 47     while(!q.empty()){
 48         L2=q.front();
 49         q.pop();
 50         if(chak(L2)) return 1;
 51         if(L2.cnt>=8) continue;
 52         for(int i=0;i<4;i++){
 53             for(int j=0;j<4;j++){
 54                 L3=L2;
 55                 L3.x[i]=L2.x[i]+dir[j][0];
 56                 L3.y[i]=L2.y[i]+dir[j][1];
 57                 if(!nice(L3)) continue;
 58                 if(empt(L3,i)){   
 59                     vis[L3.x[1]][L3.y[1]][L3.x[2]][L3.y[2]][L3.x[3]][L3.y[3]][L3.x[4]][L3.y[4]]=true;
 60                     L3.cnt++;
 61                     q.push(L3);
 62                 }
 63                 else{
 64                     L3.x[i]=L3.x[i]+dir[j][0];
 65                     L3.y[i]=L3.y[i]+dir[j][1];
 66                     if(!nice(L3)) continue;
 67                     if(empt(L3,i)){
 68                         vis[L3.x[1]][L3.y[1]][L3.x[2]][L3.y[2]][L3.x[3]][L3.y[3]][L3.x[4]][L3.y[4]]=true;
 69                         L3.cnt++;
 70                         q.push(L3);
 71                     }
 72                 }
 73             }
 74         }
 75     }
 76     return 0;
 77 }
 78 int main()
 79 {
 80     int x,y;
 81     while(cin>>x>>y){
 82         x--;y--;
 83         memset(vis,false,sizeof(vis));
 84         memset(mp,false,sizeof(mp));
 85         L3.x[0]=x;L3.y[0]=y;
 86         for(int i=1;i<4;i++){
 87             cin>>x>>y;
 88             x--;y--;
 89             L3.x[i]=x;L3.y[i]=y;
 90         }
 91         vis[L3.x[1]][L3.y[1]][L3.x[2]][L3.y[2]][L3.x[3]][L3.y[3]][L3.x[4]][L3.y[4]]=true;
 92         for(int i=0;i<4;i++){
 93             cin>>x>>y;
 94             x--;y--;
 95             mp[x][y]=true;
 96         }
 97         L3.cnt=0;
 98         int flag=bfs();
 99         if(flag) cout<<"YES\n";
100         else cout<<"NO\n";
101     }
102     return 0;
103 }

 

posted @ 2016-12-27 20:35  luckilzy  阅读(269)  评论(0编辑  收藏  举报