hdu 1010 Tempter of the Bone
来源:互联网 发布:python启动服务器 编辑:程序博客网 时间:2024/05/22 02:05
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
后天就考试了,今天做个题攒个人品。
是道好题,dfs+奇偶剪枝。
之前听说过奇偶剪枝,觉得挺有意思,大意就是A点到B点的路程一定比A点到B点的横坐标之差和纵坐标之差之和大偶数步。
刚拿到题的时候觉得是BFS的题,认为只要从A点到B点的最短路程与比要求小并且与所要求的步数小并且差是偶数即可,
WA了几发后,发现自己理解有误,实际上每个点仅仅能走一次不是可以走过去再走回来,这样我就没法用BFS去实现了,就改成DFS+奇偶剪枝来实现了。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<ctype.h>#include<algorithm>#include<string>#define PI acos(-1.0)#define maxn 8#define INF 1<<25typedef long long ll;using namespace std;char ss[maxn][maxn];int vv[maxn][maxn];int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};int stx,sty,enx,eny;int xx,yy,lim,l;int rec=0;int cango(int x,int y){ if(x>=0&&x<xx&&y>=0&&y<yy&&vv[x][y]==0&&ss[x][y]!='X') return 1; return 0;}int jud=0;int dfs(int x,int y,int rec){ vv[x][y]=1; if(jud==1) return 0; if(x==enx&&y==eny) { if(lim==rec) jud=1; return 0; } if(lim-rec<(fabs(enx-x)+fabs(eny-y))) { return 0; }//不可能达到的点,剪掉 /*if((lim-rec-(int)(fabs(enx-x)+fabs(eny-y)))%2!=0) { return 0; }*/ if(rec>lim) { return 0; }//走的步骤比限制的多,剪掉 for(int i=0; i<4; i++) { if(cango(x+dir[i][0],y+dir[i][1])) { dfs(x+dir[i][0],y+dir[i][1],rec+1); vv[x+dir[i][0]][y+dir[i][1]]=0; } }}int main(){ while(scanf("%d%d%d",&xx,&yy,&lim)) { if(xx==0&&yy==0&&lim==0) return 0; memset(vv,0,sizeof(vv)); for(int i=0; i<xx; i++) { scanf("%s",ss[i]); for(int j=0; j<yy; j++) { if(ss[i][j]=='S') { stx=i; sty=j; } if(ss[i][j]=='D') { enx=i; eny=j; } } } l=fabs(enx-stx)+fabs(eny-sty); if((l-lim)%2!=0||l>lim) printf("NO\n"); //这是奇偶剪枝,不用再DFS过程中去剪,因为很容易理解到的就是起点如何不符合那么走的每一步都不符合,但第一步符合,之后的也一定符合 else { jud=0; dfs(stx,sty,0); if(jud==1) printf("YES\n"); else printf("NO\n"); } }}
1 0
- HDU 1010 Tempter of the Bone
- 迷宫 hdu 1010 Tempter of the Bone
- HDU 1010-Tempter of the Bone
- hdu 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- hdu 1010 Tempter of the Bone(深搜)
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- hdu 1010 Tempter of the Bone
- HDU-1010-Tempter of the Bone
- hdu 1010 Tempter of the Bone
- HDU 1010-Tempter of the Bone
- HDU 1010 Tempter of the Bone (DFS)
- hdu 1010 Tempter of the Bone
- HDU 1010 - Tempter of the Bone
- hdu 1010 Tempter of the Bone
- 深入分析S3C2440启动代码中大小端问题
- C~数组对角线和之比
- 图像Filter之--层的滤镜
- Delphi下的文件夹遍历函数
- imsdroid
- hdu 1010 Tempter of the Bone
- java开源调度调度框架
- Delphi中的窗体创建与销毁
- 第一步
- 查看某一职责下对应的菜单&功能&请求
- 取一个整数的前几位
- spec中内容格式
- 我多变的2013
- 学习Ejoy2D引擎--配置环境