hdu1401(双广搜)
2014-01-30 20:02
134 查看
已知开始状态和目标状态,并且具有可逆性,采用双广搜;
用八维hash数组记录四个棋子的位置(要对四个棋子的坐标排序,迫使棋子一一对应)。
下面的代码是参考大神写的,自己刚学双搜。
用八维hash数组记录四个棋子的位置(要对四个棋子的坐标排序,迫使棋子一一对应)。
下面的代码是参考大神写的,自己刚学双搜。
#include<iostream> #include<algorithm> #include<string> #include<stack> #include<queue> #include<map> #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<time.h> #include<math.h> #define eps 1e-9 #define P system("pause") using namespace std; struct point { int x,y; friend bool operator<(point a,point b) { if(a.x!=b.x) return a.x<b.x; return a.y<b.y; } }; struct node { point s[4]; //记录四个棋子的位置 int step; }; char hash[8][8][8][8][8][8][8][8]; //如果是1则q1中的节点经过了,2则q2中的节点经过了,0表示为经过 queue<node> q1,q2; int d[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; void setflag(node a,char k) { hash[a.s[0].x][a.s[0].y][a.s[1].x][a.s[1].y][a.s[2].x][a.s[2].y][a.s[3].x][a.s[3].y]=k; } char getflag(node a) { return hash[a.s[0].x][a.s[0].y][a.s[1].x][a.s[1].y][a.s[2].x][a.s[2].y][a.s[3].x][a.s[3].y]; } bool judge(node &t,int i,int j,int m) //这个写的很巧妙,自己也写了个,总是wa,最后还是跟着答案写了 { if( m==1 ) { if( t.step >=4 )//最多移动4步 return false; t.step ++; } t.s[i].x += d[j][0]; t.s[i].y += d[j][1]; if( t.s[i].x >= 0 && t.s[i].x < 8 && t.s[i].y >=0 && t.s[i].y<8 ) { int k; for(k=0; k<4 ; k++) { if(i!=k) { if( t.s[i].x==t.s[k].x&&t.s[i].y==t.s[k].y ) if(m == 1) return judge(t, i , j , 2); else return false; } } if( k>=4 ) { sort( t.s , t.s + 4 ); return true; } } return false; } bool dbfs() { int i,j; char k; node u; while(!q1.empty()||!q2.empty()) { if(!q1.empty()) { for(i=0;i<4;i++) for(j=0;j<4;j++) { u=q1.front(); if(judge(u,i,j,1)){ k=getflag(u); if(k==2) return 1; else if(k==0){ sort(u.s,u.s+4); setflag(u,1); q1.push(u); } } } q1.pop(); } if(!q2.empty()) { for(i=0;i<4;i++) for(j=0;j<4;j++) { u=q2.front(); if(judge(u,i,j,1)){ //判断可行,主要求u k=getflag(u); if(k==1) return 1; else if(k==0) { sort(u.s,u.s+4); setflag(u,2); q2.push(u); } } } q2.pop(); } } return 0; } int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout);cc int i,j; node A,B; while(scanf("%d%d",&i,&j)!=EOF) { memset(hash,0,sizeof(hash)); while(!q1.empty()) q1.pop(); while(!q2.empty()) q2.pop(); i--;j--; A.s[0].x=i; A.s[0].y=j; for(i=1;i<4;i++){ scanf("%d%d",&A.s[i].x,&A.s[i].y); A.s[i].x--;A.s[i].y--; } sort(A.s,A.s+4); setflag(A,1); for(i=0;i<4;i++){ scanf("%d%d",&B.s[i].x,&B.s[i].y); B.s[i].x--;B.s[i].y--; } sort(B.s,B.s+4); setflag(B,2); A.step=B.step=0; q1.push(A); q2.push(B); if(dbfs()) printf("YES\n"); else printf("NO\n"); } // P; return 0; }
相关文章推荐
- 2014第5周
- [转载]HTML5 Audio/Video 标签,属性,方法,事件汇总
- 《Thinking In Algorithm》07.Red-Black Trees(红黑树)
- Hadoop2.2.0三节点搭建
- C++普通线性查找与使用汇编优化的时间对比
- Hadoop2.2.0单节点伪分布式搭建
- PHP5的PHP.INI配置介绍, 解析文档
- Implementing MapReduce with multiprocessing
- sizeof与strlen的区别
- Ajax将数据封装为XML文件
- (转)转一份在 51testing 上的讨论——如何测试一个门户网站是否可以支持10万用户同时在线?
- 2012-2013年终总结
- glob
- pomelo 服务器开发常用术语
- java线程之Semaphore
- 10大网站运营人员必备定律
- 用Python来发送邮件
- Ajax返回XML数据
- 定制Sublime - 主题/快捷键
- python weak referance