hdu 1010 Tempter of the Bone 简单带回溯的dfs+奇偶剪枝
来源:互联网 发布:window.open js 编辑:程序博客网 时间:2024/05/18 19:37
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
题意:
有一只小狗需要从N*M迷宫里逃离,出发点标记为'S','X'为不可走,'D'为出口,出口是一扇门,只有在T时刻才打开。问小狗能否逃离迷宫?
T (1 < N, M < 7; 0 < T < 50)
简单带回溯的dfs,很久以前做过,时隔两年,为了复习算法又遇此题。结果还超时了,一脸懵逼,然后果断看以前的代码(我好菜),加了一个奇偶剪枝,AC。
#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])#define mem(a,x) memset(a,x,sizeof a)#define ysk(x) (1<<(x))typedef long long ll;//const int INF = ;const int maxn= 7 ;int n,m,T,endX,endY;char a[maxn+2][maxn+2];bool vis[maxn+2][maxn+2];int dir[4][2]={ {-1,0},{0,+1},{+1,0},{0,-1} };bool in(int x,int y){return 0<=x&&x<=n-1&&0<=y&&y<=m-1;}bool dfs(int x,int y,int time){if(abs(x-endX)+abs(y-endY)>T-time || (T-time-abs(x-endX)+abs(y-endY))%2 ) return false;for0(j,4){int tx=x+dir[j][0],ty=y+dir[j][1];if(!in(tx,ty)||vis[tx][ty]||a[tx][ty]=='X') continue;if(a[tx][ty]=='D'&&time+1==T)return true;vis[tx][ty]=1;if(dfs(tx,ty,time+1)) return true;vis[tx][ty]=0;}return false;}int main(){int startX,startY;while(~scanf("%d%d%d",&n,&m,&T)&& n+m+T!=0){for0(i,n) for0(j,m) {scanf(" %c",&a[i][j]);if(a[i][j]=='S'){startX=i,startY=j;}else if(a[i][j]=='D'){endX=i,endY=j;}}mem(vis,0);vis[startX][startY]=1;puts(dfs(startX,startY,0)?"YES":"NO");}return 0;}
阅读全文
0 0
- hdu 1010 Tempter of the Bone 简单带回溯的dfs+奇偶剪枝
- HDU 1010Tempter of the Bone(奇偶剪枝回溯dfs)
- HDU 1010 Tempter of the Bone - (DFS) 奇偶剪枝
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
- HDU--1010 -- Tempter of the Bone [DFS+经典奇偶剪枝]
- Hdu 1010 Tempter of the Bone (DFS 经典奇偶剪枝)
- hdu 1010 Tempter of the Bone (dfs+奇偶剪枝)
- HDU 1010 Tempter of the Bone( DFS + 奇偶剪枝 )
- hdu 1010 Tempter of the Bone(dfs + 奇偶剪枝)
- HDU 1010 Tempter of the Bone ( dfs + 奇偶剪枝 )
- HDU 1010 Tempter of the Bone (DFS+奇偶剪枝)
- hdu 1010 Tempter of the Bone(DFS+奇偶剪枝)
- HDU 1010-Tempter of the Bone(DFS+奇偶剪枝)
- hdu 1010 Tempter of the Bone (dfs+奇偶剪枝)
- hdu 1010 Tempter of the Bone (dfs+奇偶剪枝)
- hdu 1010 Tempter of the Bone题解(dfs+奇偶剪枝)
- Maven项目报错解决方案
- 欢迎使用CSDN-markdown编辑器
- JS之事件委托
- Apache安装错误 APR not found解决方法
- 互联网名词大全
- hdu 1010 Tempter of the Bone 简单带回溯的dfs+奇偶剪枝
- DFA算法的简单说明与案例实现
- HDU2089[不要62]
- 高中数学(理科思维)不是特别特别好,未来能做成优秀程序员吗?(转载)
- apt-get 使用详解
- Thinking in java个人笔记2
- BZOJ 2463 (博弈论)
- JS-Date对象
- HOG Matlab代码 2