hdoj1010 Tempter of the Bone(迷宫问题dfs,奇偶剪枝)
来源:互联网 发布:淘宝为什么没有比价了 编辑:程序博客网 时间:2024/06/07 20:24
迷宫问题。
这类问题接触的少,但仔细审题就能看出要用dfs。欢天喜地写出代码,结果超时了。
百度了一下,惊讶的发现差了从没听过的奇偶剪枝操作。
代码有借鉴的地方。
#include<iostream>#include <cstdio>#include <cstdlib>using namespace std;int n,m,t;char maze[8][8];int v[8][8];int d[4][2]={-1,0,1,0,0,-1,0,1};int b_x,b_y,e_x,e_y;bool dfs(int r,int c,int s) {int x,y;if(r==e_x&&c==e_y&&s==t)return true;if(s>=t)return false;int temp=t-s-abs(r-e_x)-abs(c-e_y); if (temp<0 || temp%2!=0)return false; for(int i = 0; i < 4; i++) { x = r + d[i][0]; y = c + d[i][1]; //出界了 if(x<0||x>=n||y<0||y>=m) continue; //此路不通 if(maze[x][y]=='X')continue; //该位置已经走过了。。 if(v[x][y]==1)continue; v[x][y] = 1; if(dfs(x,y,s+1))return true; v[x][y] = 0; } return false;}int main(){ int wall; while(cin>>n>>m>>t&&n!=0&&m!=0&&t!=0) { wall = 0; //初始化,标记为未走过 for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) v[i][j] = 0; for(int i = 0; i < n; i++) { scanf("%s",maze+i); for(int j = 0; j < m;j++) if(maze[i][j]=='S') { b_x = i; b_y = j; }else if(maze[i][j]=='D'){ e_x = i; e_y = j; }else if(maze[i][j]=='X')wall++; } //剪枝一 if(n*m-wall<=t) { printf("NO\n"); continue; } v[b_x][b_y]=1; if(dfs(b_x,b_y,0)) printf("YES\n"); else printf("NO\n"); } return 0;}
阅读全文
0 0
- hdoj1010 Tempter of the Bone(迷宫问题dfs,奇偶剪枝)
- HDOJ1010 Tempter of the Bone(DFS+奇偶剪枝)
- HDOJ1010 Tempter of the Bone(DFS,奇偶剪枝)
- HDU-Tempter of the Bone-小狗走迷宫(DFS&&剪枝||奇偶剪枝)
- Tempter of the Bone DFS+奇偶剪枝
- Tempter of the Bone (dfs + 奇偶剪枝)
- Tempter of the Bone-dfs+奇偶剪枝
- Tempter of the Bone(dfs, 奇偶剪枝)
- Tempter of the Bone(DFS + 奇偶剪枝,好题)
- 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+奇偶剪枝)
- 剑指offer——把数组排成最小的数_____
- 第五次上机实验
- (四)项目结构介绍
- Reactor
- Java命名规范
- hdoj1010 Tempter of the Bone(迷宫问题dfs,奇偶剪枝)
- python数据挖掘包Sklearn
- FFmpeg SDL_AudioSpec 详解
- 照片处理文章
- U盘装系统
- Kotlin是什么
- 第十五天1
- 用xml.dom解析xml文件
- 2017-05-18知识分享