HDU 1010 Tempter of the Bone
来源:互联网 发布:淘宝网怎么看卖家电话 编辑:程序博客网 时间:2024/04/28 16:18
http://acm.hdu.edu.cn/showproblem.php?pid=1010
题目读完一开始以为是BFS的题目,求一个最短路然后计算时间...
后来发现不对= =一定要正好到达出口的时候时间也吻合才能出去
所以是DFS...
一道比较基础的DFS,但是需要剪枝= =(一开始TLE了两次,才觉得需要剪枝,蠢哭)
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <string>#include <cmath>using namespace std;char map[10][10];int ex,ey,sx,sy;int n,m,t,wall;bool flag;int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};void dfs(int x,int y,int cnt){ if(cnt==t) { if(ex==x&&ey==y) flag=true; return; } if(flag) return; int temp=abs(x-ex)+abs(y-ey)-abs(cnt-t);//奇偶性剪枝,计算当前的离出口的距离 if(temp>0||temp&1)//位运算判断奇偶性 return; for(int i=0;i<4;i++) { int fx=x+dir[i][0]; int fy=y+dir[i][1]; if(fx>=0&&fx<n&&fy>=0&&fy<m&&map[fx][fy]!='X') { map[fx][fy]='X'; dfs(fx,fy,cnt+1); map[fx][fy]='.';//回溯 } }}int main(){ //freopen("in.txt","r",stdin); while(scanf("%d%d%d",&n,&m,&t)!=EOF) { flag=false; wall=0; if(m==0&&n==0&&t==0) break; getchar(); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='S') { sx=i; sy=j; map[i][j]='X'; } else if(map[i][j]=='D') { ex=i; ey=j; } else if(map[i][j]=='X') wall++; } getchar(); } if(m*n-wall<=t) flag=false; else dfs(sx,sy,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0;}
0 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
- JavaScript字符串转日期并比较大小
- App原型设计工具使用心得(下)
- 判断点在任意多边形内部最简单的算法
- 文件上传Excel
- 用 Python 脚本实现对 Linux 服务器的监控
- HDU 1010 Tempter of the Bone
- 一个Date对象引发的诡异bug
- Java获取当前操作系统文件路径分隔符
- HDU 4535 吉哥系列故事——礼尚往来
- JBPM-4.3所需的最小依赖库列表,JDK1.5十分需要
- 嵌入式Linux之我行——S3C2440上LCD驱动(FrameBuffer)实例开发讲解(一)
- php 技术笔记
- !!! FAILED BINDER TRANSACTION !!!
- SQL模糊查询注意事项