HDOJ 1010 Tempter of the Bone
来源:互联网 发布:js push 完删除 编辑:程序博客网 时间:2024/06/06 01:47
题意:一个N * M 的迷宫, 起点为S, 终点为D , 障碍为X, 问你是否恰好花费时间T 的时候到达终点D。
思路:DFS ,纯粹的搜索会直接超时, 所以需要通过剪枝, 也是在网上看到别人说奇偶剪枝,加进去直接AC了。
69234052012-10-15 15:09:01Accepted1010640MS256K1465BC++罗维
#include <iostream>#include <string>#include <vector>#include <math.h>using namespace std;struct point{int x, y;};int n, m, t;point spt, dpt;string maze[10];//vector<vecot<int> >used;bool escape;void dfs(point pt, int t){int tmp = t - abs(pt.x-dpt.x) - abs(pt.y - dpt.y);if(pt.x == dpt.x && pt.y == dpt.y && t == 0){escape = true;return ;}//剪枝if (escape || tmp < 0 || tmp&1 ) //tmp&1 如果是奇数则为1 否则为0return;maze[pt.x][pt.y] = 'X';if(pt.x + 1 <n && maze[pt.x + 1][pt.y] != 'X'){pt.x += 1;dfs(pt, t-1);pt.x -= 1;if(escape) return;}if(pt.x - 1 >=0 && maze[pt.x - 1][pt.y] != 'X'){pt.x -= 1;dfs(pt, t-1);pt.x += 1;if(escape) return;}if(pt.y + 1 < m && maze[pt.x][pt.y + 1] != 'X'){pt.y += 1;dfs(pt, t-1);pt.y -= 1;if(escape) return;}if(pt.y - 1 >= 0 && maze[pt.x][pt.y - 1] != 'X'){pt.y -= 1;dfs(pt, t-1);pt.y += 1;if(escape) return;}maze[pt.x][pt.y] = '.';}int main(){int i, j;while(cin>>n>>m>>t && n+m+t != 0){for (i=0; i<n; i++){cin>>maze[i];for (j=0; j<m; j++){if(maze[i][j] == 'S'){spt.x = i;spt.y = j;}else if(maze[i][j] == 'D'){dpt.x = i;dpt.y = j;}}}escape = false;dfs(spt, t);if(escape)cout<<"YES"<<endl;elsecout<<"NO"<<endl;}return 0;}
- hdoj 1010 Tempter of the Bone
- HDOJ 1010 Tempter of the Bone
- HDOJ 1010 Tempter of the Bone
- HDOJ 1010 Tempter of the Bone
- ACM HDOJ 1010 (Tempter of the Bone)
- HDOJ 1010 Tempter of the Bone
- HDOJ 1010 Tempter of the Bone
- HDoj-1010-Tempter of the Bone-DFS
- 【HDOJ 1010】Tempter of the Bone
- hdoj 1010 Tempter of the Bone
- hdoj 1010Tempter of the Bone【DFS】
- HDOJ 1010 Tempter of the Bone
- HDOJ 1010 Tempter of the Bone
- HDOJ 1010-Tempter of the Bone【dfs】
- HDOJ 1010 Tempter of the Bone
- HDOJ.1010 Tempter of the Bone (DFS)
- hdoj 1010 Tempter of the Bone
- HDOJ HDU 1010 Tempter of the Bone
- gridpanel 行双击事件,js带参数调用后台方法
- 男生的眼泪、女生永远不懂纯属伤感
- GetCurrentDirectory、SetCurrentDirectory和GetModuleFileName
- 谷歌的编码规范,先收着
- Javascript 之 Ext.form.ComboBox
- HDOJ 1010 Tempter of the Bone
- 后缀数组
- [Happy DSA] 图像的contour trace算法C++实现
- 如何查看谁在占用你的dataset
- 找个让自己变得不一样的爱好
- 程序员要如何提高数学水平
- ipc$连接失败的常见原因
- 代码重构技巧
- 情断的伤感QQ心情日志:可曾知否,有个人在思念着你