hdu 1401
2014-04-10 08:16
239 查看
Solitaire
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3077 Accepted Submission(s): 954
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
4 4 4 5 5 4 6 5
2 4 3 3 3 6 4 6
[align=left]Sample Output[/align]
YES
[align=left]Source[/align]
Southwestern Europe 2002
[align=left]Recommend[/align]
[align=left]四个点的哈希,由于这四个点没有什么区分,哈希的时候注意一下。[/align]
[align=left]由于的用set哈希,用*10的方法。所以要先排序。很容易理解的。[/align]
[align=left]bfs的代码,有点意思。[/align]
/** 4 4 4 5 5 4 6 5 2 4 3 3 3 6 4 6 **/ #include<iostream> #include<stdio.h> #include<cstring> #include<cstdlib> #include<queue> #include<algorithm> #include<set> using namespace std; struct node { int x[4]; int y[4]; }; struct node start,end1; queue<node>Q[2]; set<int>hxl[2]; bool flag; int map1[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; bool fun(node &t,int i) { int j; if(t.x[i]>=1&&t.x[i]<=8 && t.y[i]>=1&&t.y[i]<=8) { for(j=0;j<4;j++) { if(j==i)continue; if(t.x[i]==t.x[j] && t.y[i]==t.y[j]) return true; } return false; } return true; } void pai(node &t) { int i,j,x; for(i=0;i<4; i++) { x=i; for(j=i+1;j<4; j++) if(t.x[x]>t.x[j]) x=j; else if(t.x[x]==t.x[j] && t.y[x]>t.y[j]) x=j; swap(t.x[i],t.x[x]); swap(t.y[i],t.y[x]); } } int serch(node &t) { int i,sum=0; for(i=0;i<4;i++) sum=sum*100+t.x[i]*10+t.y[i]; return sum; } void bfs(int x) { int i,j,size1,k; node cur,t; size1=Q[x].size(); while(size1--) { cur=Q[x].front(); Q[x].pop(); for(i=0;i<4;i++)/** every four point **/ { for(j=0;j<4;j++) /** n s w e**/ { t=cur; t.x[i]=t.x[i]+map1[j][0]; t.y[i]=t.y[i]+map1[j][1]; if(fun(t,i)==true) { t.x[i]=t.x[i]+map1[j][0]; t.y[i]=t.y[i]+map1[j][1]; if(fun(t,i)==true) continue; } pai(t); k=serch(t); if(hxl[x].count(k)>0)continue; if(hxl[x^1].count(k)>0) { flag=true; return; } hxl[x].insert(k); Q[x].push(t); } } } } void dbfs() { int ans=0,k; pai(start); k=serch(start); hxl[0].insert(k); Q[0].push(start); pai(end1); Q[1].push(end1); k=serch(end1); hxl[1].insert(k); while(true) { if(Q[0].size()<Q[1].size()) bfs(0); else bfs(1); ans++; if(ans==8)break; if(flag==true) return; } } int main() { int i; while(scanf("%d%d",&start.x[0],&start.y[0])>0) { for(i=1;i<4;i++) scanf("%d%d",&start.x[i],&start.y[i]); for(i=0;i<4;i++) scanf("%d%d",&end1.x[i],&end1.y[i]); while(!Q[0].empty()){ Q[0].pop(); } while(!Q[1].empty()){ Q[1].pop(); } hxl[0].clear(); hxl[1].clear(); flag=false; dbfs(); if(flag==true) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- poj 1198 hdu 1401 搜索+剪枝 Solitaire
- hdu 1401 bfs
- poj 1198 hdu 1401 搜索+剪枝 Solitaire
- HDU_1401——分步双向BFS,八进制位运算压缩,map存放hash
- HDU 1401 Solitaire
- hdu 1401
- HDU 1401 Solitaire (双向搜索)
- HDU 1401 Solitaire 双向BFS
- hdu 1401 Solitaire(双向宽搜)
- hdu 1401
- HDU 1401 Solitaire(棋盘状态BFS)
- HDU 1401 Solitaire(双向BFS)
- hdu 1401 双向广搜+8进制压缩状态
- HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash
- HDU 1401 Solitaire(双向搜索)
- hdu 1401 Solitaire 双向bfs
- hdu 1401 Solitaire (双向广搜)
- hdu 1401
- hdu1401 Solitaire双向bfs
- HDU 1401 Solitaire [双向BFS]