HDU_1401——分步双向BFS,八进制乘权值压缩,map存放hash

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
  1 #include <iostream>
  2 #include <algorithm>
  3 #include <queue>
  4 #include <map>
  5 using namespace std;
  6 
  7 const int dir[4][2] = {1,0,-1,0,0,1,0,-1};
  8 const int pow[8] = {1,8,64,512,4096,32768,262144,2097152};
  9 
 10 struct point
 11 {
 12     int x,y;
 13 };
 14 struct node
 15 {
 16     point chess[4];
 17     
 18     bool check(int j)
 19     {
 20         if(chess[j].x>=0 && chess[j].x<=7 && chess[j].y>=0 && chess[j].y<=7)
 21         {
 22             for(int i=0;i<4;i++)
 23             {
 24                 if(i!=j && chess[i].x==chess[j].x && chess[i].y==chess[j].y)
 25                 {
 26                     return false;
 27                 }
 28             }
 29             return true;
 30         }
 31         return false;
 32     }
 33 }s,e;
 34 
 35 bool cmp(const struct point& a,const struct point& b)
 36 {
 37     if(a.x == b.x)
 38     {
 39         return a.y<b.y;
 40     }
 41     return a.x<b.x;
 42 }
 43 int gethash(node& a)
 44 {
 45     sort(a.chess, a.chess+4, cmp);
 46     int hash = 0;
 47     for(int i=0; i<4; i++)
 48     {
 49         hash += a.chess[i].x * pow[2*i];
 50         hash += a.chess[i].y * pow[2*i+1];
 51     }
 52     return hash;
 53 }
 54 
 55 map<int,int>mapint;
 56 map<int,int>::iterator it_1,it_2;
 57 
 58 bool BFS(int flag,node temp)
 59 {
 60     queue<node>que;
 61     que.push(temp);
 62     while(!que.empty())
 63     {
 64         temp = que.front();
 65         que.pop();
 66         
 67         it_1 = mapint.find(gethash(temp));
 68         
 69         if((it_1->second)%10 >= 4)    //移动步数超过4步 
 70         {
 71             continue;
 72         }
 73         for(int i=0;i<4;i++)
 74         {
 75             for(int j=0;j<4;j++)
 76             {
 77                 node next = temp;
 78                 next.chess[i].x += dir[j][0];
 79                 next.chess[i].y += dir[j][1];
 80                 
 81                 if(!next.check(i))    //重叠或者越界 
 82                 {
 83                     next.chess[i].x += dir[j][0];
 84                     next.chess[i].y += dir[j][1];
 85                     if(!next.check(i))        //重叠或者越界 
 86                     {
 87                         continue;
 88                     }
 89                 }
 90                 
 91                 int hash_next = gethash(next);
 92                 it_2 = mapint.find(hash_next);
 93                     
 94                 if(it_2 == mapint.end())
 95                 {
 96                     mapint[hash_next] = it_1->second + 1;
 97                     que.push(next);
 98                 }
 99                 else if(it_2->second/10 == 1-flag)
100                 {
101                     return true;
102                 }
103             }
104         }
105     }
106     return false;
107 }
108 
109 int main()
110 {
111     while(cin>>s.chess[0].x>>s.chess[0].y)
112     {
113         for(int i=1;i<4;i++)
114         {
115             cin>>s.chess[i].x>>s.chess[i].y;
116         }
117         for(int i=0;i<4;i++)
118         {
119             cin>>e.chess[i].x>>e.chess[i].y;
120         }
121         for(int i=0;i<4;i++)
122         {
123             s.chess[i].x--;s.chess[i].y--;
124             e.chess[i].x--;e.chess[i].y--;
125         }
126         mapint[gethash(s)] = 10*0+0;
127         mapint[gethash(e)] = 10*1+0;
128         
129         if(BFS(0,s) || BFS(1,e))
130         {
131             cout<<"YES"<<endl;
132         }
133         else
134         {
135             cout<<"NO"<<endl;
136         }
137         
138         mapint.clear();
139     }
140     return 0;
141 }

 

posted @ 2013-07-17 12:55  瓶哥  Views(255)  Comments(0Edit  收藏  举报