hdu 1401 Solitaire(双向bfs)
2014-05-23 17:04
246 查看
Solitaire
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3141 Accepted Submission(s): 976
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).
![](http://acm.hdu.edu.cn/data/images/1401-1.gif)
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
Southwestern Europe 2002
题意:给定8*8的棋盘,上面有4个棋子,给定初始态和结束态,问是否可以在8步之内到达
题解:明显每一种状态有16种转移所得的状态,若普通的bfs的复杂度就是16的8次方,即2的32次方。。。无法接受
当是考虑到给定初始态和结束态,可以2边同时进行4步bfs,仅需判断是否相交就行了,复杂度就是2*16的4次方,即2的17次方,完全可以接受
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> #include<map> using namespace std; typedef unsigned long long ll; struct point { int x,y; }; struct node { point p[4]; int step; } sta,fin; int cou=0; int dx[]= {0,0,1,-1}; int dy[]= {1,-1,0,0}; int check(point p1) { if(p1.x<0||p1.x>=8||p1.y<0||p1.y>=8) return 0; return 1; } ll gethash(node now) { ll tmp=0; for(int i=0; i<4; i++) tmp|=((ll)1)<<(now.p[i].x*8+now.p[i].y); return tmp; } map<ll,int>mp; int bfs(int type,node now) { queue<node>q; if(type==2) { if(mp.find(gethash(now))!=mp.end()) return 1; } mp[gethash(now)]=type; now.step=0; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); if(now.step>=4) continue; for(int i=0; i<4; i++) { for(int j=0,k; j<4; j++) { cou++; node temp=now; temp.step++; temp.p[j].x+=dx[i]; temp.p[j].y+=dy[i]; if(!check(temp.p[j])) continue; ll tmp=gethash(temp); for(k=0; k<4; k++) { if(k==j) continue; if(temp.p[j].x==temp.p[k].x&&temp.p[j].y==temp.p[k].y) break; } if(k==4) { if(mp.find(tmp)==mp.end()) { mp[tmp]=type; q.push(temp); } else if(mp[tmp]!=type) return 1; } else { temp.p[j].x+=dx[i]; temp.p[j].y+=dy[i]; tmp=gethash(temp); if(!check(temp.p[j])) continue; for(k=0; k<4; k++) { if(k==j) continue; if(temp.p[j].x==temp.p[k].x&&temp.p[j].y==temp.p[k].y) break; } if(k<4) continue; if(mp.find(tmp)==mp.end()) { mp[tmp]=type; q.push(temp); } else if(mp[tmp]!=type) return 1; } } } } return 0; } int main() { while(scanf("%d%d",&sta.p[0].x,&sta.p[0].y)>0) { for(int i=1; i<4; i++) scanf("%d%d",&sta.p[i].x,&sta.p[i].y); for(int i=0; i<4; i++) scanf("%d%d",&fin.p[i].x,&fin.p[i].y); for(int i=0; i<4; i++) sta.p[i].x--,sta.p[i].y--; for(int i=0; i<4; i++) fin.p[i].x--,fin.p[i].y--; mp.clear(); bfs(1,sta); int temp=bfs(2,fin); printf("%s\n",temp?"YES":"NO"); } return 0; }
相关文章推荐
- HDU 1401 Solitaire(双向BFS)
- HDU 1401 Solitaire [双向BFS]
- hdu 1401 Solitaire 双向bfs
- HDU 1401 Solitaire 双向BFS
- HDU 1401 Solitaire 双向BFS
- HDU 1401 Solitaire(双向搜索)
- HDU 1401 Solitaire(双向广度优先搜索)
- HDOJ 题目1401 Solitaire(双向BFS)
- HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash
- hdu 1401 Solitaire(bfs)
- hdu 1401/poj 1198 Solitaire(BFS,剪枝)
- HDU 1401 双向BFS !!!
- HDU 1401 Solitaire (双向搜索)
- hdu 1401 双向bfs
- HDU_1401——分步双向BFS,八进制乘权值压缩,map存放hash
- HDU 1401 Solitaire(棋盘状态BFS)
- hdu 1401 Solitaire 双向广度搜索
- HDU_1401——分步双向BFS,八进制位运算压缩,map存放hash
- hdu 1401 Solitaire(双向宽搜)
- hdu 1401 双向搜索(bfs)