poj 2697 A Board Game(bfs+hash)

Description

Dao was a simple two-player board game designed by Jeff Pickering and Ben van Buskirk at 1999. A variation of it, called S-Dao, is a one-player game. In S-Dao, the game board is a 4 * 4 square with 16 cells. There are 4 black stones and 4 white stones placed on the game board randomly in the beginning. The player is given a final position and asked to play the game using the following rules such that the final position is reached using the minimum number of moves:
1. You first move a white stone, and then a black stone. You then alternatively move a white stone and a black stone. 

2. A stone can be moved horizontally, vertically or diagonally. A stone must be moved in a direction until the boarder or another stone is encountered. There is no capture or jump. 

3. During each move, you need to move a stone of the right color. You cannot pass.

An example of a sequence of legal moves is shown in the following figure. This move sequence takes 4 moves. This is not a sequence of legal moves 

using the least number of moves assume the leftmost board is the initial position and the rightmost board is the final position. A sequence of moves using only 3 moves is shown below. 

Given an initial position and a final position, your task is to report the minimum number of moves from the initial position to the final position.

Input

The first line contains the number of test cases w, w <= 6. Then the w test cases are listed one by one. Each test case consists of 8 lines, 4 characters per line. The first 4 lines are the initial board position. The remaining 4 lines are the final board position. The i-th line of a board is the board at the i-th row. A character ‘b‘ means a black stone, a character ‘w‘ means a white stone, and a ‘*‘ means an empty cell.

Output

For each test case, output the minimum number of moves in one line. If it is impossible to move from the initial position to the final position, then output -1.

Sample Input

2
w**b
*wb*
*bw*
b**w
w**b
*wb*
*bw*
bw**
w**b
*b**
**b*
bwww
w**b
*bb*
****
bwww

Sample Output

1
3

Hint

Doing simple exhaustive search without planning ahead will most likely get you into troubles.

Source

Taiwan 2004

写了快200行的题目,跟前面一道题目 poj2046 很相似,用hash来保存整个图的状态,然后剩下的就是bfs的控制能力了。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<queue>
  5 #include<algorithm>
  6 #include<stdlib.h>
  7 using namespace std;
  8 #define ll long long
  9 #define M 1000007
 10 ll hash[M];
 11 ll goal[5][5];
 12 ll base[17]={1};
 13 ll aimNum;
 14 ll flag;
 15 ll white;
 16 ll black;
 17 struct Node{
 18     ll x,y;
 19     ll mp[5][5];
 20     ll step;
 21     ll whitePx[5];
 22     ll whitePy[5];
 23     ll blackPx[5];
 24     ll blackPy[5];
 25
 26 }st;
 27 bool inserNum(ll ans){//hash的插入,看看是否跟之前的状态相同,其实跟vis数组标记一个意思
 28     ll val=ans%M;
 29     while(hash[val]!=-1 && hash[val]!=ans){
 30         val=(val+10)%M;
 31     }
 32     if(hash[val]==-1){
 33         hash[val]=ans;
 34         return true;//可以插入返回true
 35     }
 36     return false;//否则返回false
 37 }
 38
 39 bool work(Node cnt){
 40     ll ans=0;
 41     for(ll i=0;i<4;i++){
 42         for(ll j=0;j<4;j++){
 43             ans=ans+cnt.mp[i][j]*base[i*4+j];//ans为整张图的hash值
 44         }
 45     }
 46     if(ans==aimNum){
 47         flag=1;
 48     }
 49     if(inserNum(ans))
 50         return true;
 51     return false;
 52 }
 53 ll dirx[]={0,0,-1,1,-1,-1,1,1};
 54 ll diry[]={-1,1,0,0,-1,1,-1,1};
 55 ll bfs(){
 56     queue<Node>q;
 57     q.push(st);
 58     Node t1,t2;
 59     while(!q.empty()){
 60         t1=q.front();
 61         q.pop();
 62         if(t1.step%2==0){
 63                for(ll i=0;i<4;i++){
 64
 65                  ll sx=t1.whitePx[i];
 66                    ll sy=t1.whitePy[i];
 67                    //printf("%I64d %I64d\n",sx,sy);
 68                    for(ll j=0;j<8;j++){
 69                        t2=t1;
 70                        t2.x=sx+dirx[j];
 71                        t2.y=sy+diry[j];
 72                        if(t2.mp[t2.x][t2.y]==1 || t2.mp[t2.x][t2.y]==2) continue;
 73                        t2.step=t1.step+1;
 74                        while(t2.x>=0 && t2.y>=0 && t2.x<4 && t2.y<4 && t2.mp[t2.x][t2.y]==3){
 75                            t2.x=t2.x+dirx[j];
 76                            t2.y=t2.y+diry[j];
 77                       }
 78                 t2.x=t2.x-dirx[j];
 79                 t2.y=t2.y-diry[j];
 80                 swap(t2.mp[t2.x][t2.y],t2.mp[sx][sy]);
 81                 if(work(t2)){
 82                     t2.whitePx[i]=t2.x;
 83                     t2.whitePy[i]=t2.y;
 84                     q.push(t2);
 85                     if(flag){
 86                         return t2.step;
 87                     }
 88                 }
 89                    }
 90              }
 91         }
 92         else{
 93               for(ll i=0;i<4;i++){
 94                  ll sx=t1.blackPx[i];
 95                    ll sy=t1.blackPy[i];
 96                    for(ll j=0;j<8;j++){
 97                        t2=t1;
 98                        t2.x=sx+dirx[j];
 99                        t2.y=sy+diry[j];
100                        if(t2.mp[t2.x][t2.y]==1 || t2.mp[t2.x][t2.y]==2) continue;
101                        t2.step=t1.step+1;
102                        while(t2.x>=0 && t2.y>=0 && t2.x<4 && t2.y<4 && t2.mp[t2.x][t2.y]==3){
103                            t2.x=t2.x+dirx[j];
104                            t2.y=t2.y+diry[j];
105                       }
106                 t2.x=t2.x-dirx[j];
107                 t2.y=t2.y-diry[j];
108                 swap(t2.mp[t2.x][t2.y],t2.mp[sx][sy]);
109                 if(work(t2)){
110                     t2.blackPx[i]=t2.x;
111                     t2.blackPy[i]=t2.y;
112                     q.push(t2);
113                     if(flag){
114                         return t2.step;
115                     }
116                 }
117                    }
118              }
119         }
120     }
121     return -1;
122 }
123 ll calGoal(){
124     ll ans=0;
125     for(ll i=0;i<4;i++){
126         for(ll j=0;j<4;j++){
127             ans=ans+goal[i][j]*base[i*4+j];
128         }
129     }
130     return ans;
131 }
132
133
134
135 int main()
136 {
137     for(ll i=1;i<17;i++){
138           base[i]=base[i-1]*2;
139      }
140     ll t;
141     scanf("%I64d",&t);
142     while(t--){
143         memset(hash,-1,sizeof(hash));
144         white=0;
145         black=0;
146         char s[10];
147         for(ll i=0;i<4;i++){
148             scanf("%s",s);
149             for(ll j=0;j<4;j++){
150                 if(s[j]==‘w‘){
151                     st.x=i;
152                     st.y=j;
153                     st.mp[i][j]=1;
154                     st.whitePx[white]=i;
155                     st.whitePy[white++]=j;
156                 }
157                 else if(s[j]==‘b‘){
158                     st.x=i;
159                     st.y=j;
160                     st.mp[i][j]=2;
161                     st.blackPx[black]=i;
162                     st.blackPy[black++]=j;
163                 }
164                 else if(s[j]==‘*‘){
165                     st.mp[i][j]=3;
166                 }
167             }
168         }
169         for(ll i=0;i<4;i++){
170             scanf("%s",s);
171             for(ll j=0;j<4;j++){
172                 if(s[j]==‘w‘){
173                     goal[i][j]=1;
174                 }
175                 else if(s[j]==‘b‘){
176                     goal[i][j]=2;
177                 }
178                 else if(s[j]==‘*‘){
179                     goal[i][j]=3;
180                 }
181             }
182         }
183
184         aimNum=calGoal();
185         flag=0;
186         st.step=0;
187         work(st);
188         if(flag){
189             printf("0\n");
190         }
191         else{
192             printf("%I64d\n",bfs());
193         }
194
195     }
196     return 0;
197 }

时间: 2024-11-06 07:37:43

poj 2697 A Board Game(bfs+hash)的相关文章

POJ 3131 Cubic Eight-Puzzle 双向BFS + HASH

超赞的一道搜索题,麻烦到没朋友,不过思路还是很清楚的. 双向搜索的时候需要注意一个地方就是起始状态只有一个,目标状态最多的时候感觉会有512个,所以要控制两个方向搜索的层数.第一次知道双向搜索还能控制层数,简直点赞. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue&

POJ 1101 The Game(BFS+判方向)

    The Game Description One morning, you wake up and think: "I am such a good programmer. Why not make some money?'' So you decide to write a computer game. The game takes place on a rectangular board consisting of w * h squares. Each square might o

poj 1915 Knight Moves (bfs搜索)

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21919   Accepted: 10223 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast

HDU 1043 Eight八数码解题思路(bfs+hash 打表 IDA* 等)

题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原为初始状态 思路: 参加网站比赛时拿到此题目,因为之前写过八数码问题,心中暗喜,于是写出一套暴力bfs+hash,结果TLE呵呵 思路一:bfs+hash(TLE) 1 #include <cstdio> 2 #include <cstring> 3 #include <queu

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

poj 3087 Shuffle&#39;m Up (bfs)

Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled wit

POJ 1200 Crazy Search (字符串hash)

题目大意: 分析长度为n的子串有多少种. 思路分析: 对于没出现的字符,将其分配一个数字. 然后将子串看做一个nc进制的数. 然后hash判断. #include <cstdio> #include <iostream> #include <algorithm> #include <map> #include <cstring> #include <string> using namespace std; bool vis[26666

11198 - Dancing Digits(BFS + hash判重)

题目:11198 - Dancing Digits 题目大意:每组数据给出8个数字,可能正可能负.要求最后将这8个数字按照数字绝对值从小到大的排序.排序的规则是让某个数字a邀请另一个数字b跳舞,这样a就可以插到b的左边或是右边,a能邀请b跳舞,则a* b <0 ,且a+b要是素数.题目问给出一组数据问能否通过邀请跳舞来排序,能的话就输出最少的邀请次数,否则输出-1. 解题思路:这题一开始竟然想着dfs,但是后面发现,这样的判断树可以是无限大,因为可以a邀请完b,然后b在邀请a,这样一来一回有可能