DFS+奇偶剪枝
来源:互联网 发布:程序员的发展 编辑:程序博客网 时间:2024/05/18 00:00
hdu1010 Tempter of the Bone
Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES
题意:给出n*m的网格,起点为S,终点为D,问能否在t时间内从起点到终点(X不可走),每个(.)只能停留一秒
思路:一定要剪枝!不然tle到你哭着回家找妈妈!
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>using namespace std;char map[20][20];int n, m, t, flag[20][20], ans, ex, ey, sx, sy;void dfs(int i, int j, int tim){ flag[i][j]=1; if(tim==t&&map[i][j]=='D') {ans=1;return;} int as=fabs(i-ex)+fabs(j-ey); if((t-tim-as)%2||as+tim>t)return;//奇偶剪枝 if(ans)return;//这个不知道是什么剪枝一定是要的 if(i-1>=0&&flag[i-1][j]==0&&(map[i-1][j]=='.'||map[i-1][j]=='D')) {dfs(i-1, j, tim+1);flag[i-1][j]=0;} if(i+1<n&&flag[i+1][j]==0&&(map[i+1][j]=='.'||map[i+1][j]=='D')) {dfs(i+1, j, tim+1);flag[i+1][j]=0;} if(j-1>=0&&flag[i][j-1]==0&&(map[i][j-1]=='.'||map[i][j-1]=='D')) {dfs(i, j-1, tim+1);flag[i][j-1]=0;} if(j+1<m&&flag[i][j+1]==0&&(map[i][j+1]=='.'||map[i][j+1]=='D')) {dfs(i, j+1, tim+1);flag[i][j+1]=0;} return;}int main(){ int i, j; while(~scanf("%d%d%d", &n, &m, &t)) { ans=0; if(n==0&&m==0&&t==0) break; memset(flag, 0, sizeof(flag)); for(i = 0; i < n; i++) scanf("%s", map[i]); for(i = 0; i < n; i++) for(j = 0; j < m; j++) { if(map[i][j]=='S') {sx=i; sy=j;} if(map[i][j]=='D') {ex=i; ey=j;} } dfs(sx, sy, 0); if(ans==1)printf("YES\n"); else printf("NO\n"); } return 0;}
阅读全文
1 0
- DFS中的奇偶剪枝
- DFS中的奇偶剪枝
- HDU1010-奇偶剪枝(DFS)
- DFS--奇偶剪枝
- hdu1010 dfs奇偶剪枝
- HDU1010奇偶剪枝DFS
- HDU1010 DFS+奇偶剪枝
- DFS+奇偶剪枝
- HDU1010 dfs奇偶剪枝
- dfs 奇偶剪枝
- dfs【奇偶剪枝】
- DFS+奇偶剪枝
- HDU1010 DFS+奇偶剪枝
- DFS中的奇偶剪枝
- hdu1010(DFS + 奇偶剪枝)
- hdu1010(DFS+奇偶剪枝)
- hdu 1010 dfs+奇偶剪枝
- HDU 1010(dfs+奇偶剪枝)
- 嵌入式学习笔记(第九天)系统网络续
- Java基础--static关键字
- css中的浮动以及如何清除浮动
- javaWeb文档预览之OpenOfiice踩过的坑
- Guava(一)Lists与Maps
- DFS+奇偶剪枝
- 异常com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536
- C语言反汇编代码(三大结构)
- 消息发送机制
- 确定字符串互异--牛客
- SCP--服务器之间拷贝文件
- 如何用AI赋能阿里“智能客服”:打通售前售后环节的方法
- 大数据组件原理总结-Hadoop、Hbase、Kafka、Zookeeper、Spark
- 几个R语言画图小程序分享