DFS——HDU1010(经典的奇偶剪枝)
来源:互联网 发布:手机移动数据怎么关闭 编辑:程序博客网 时间:2024/06/07 17:26
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.
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.
'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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
Sample Output
NOYES
大致意思:
大致题意:给一幅图,有起点有墙有终点,问能不能在刚好t秒的时间走到终点
代码:
#include<stdio.h>#include<string.h>int flag,sx,sy,ex,ey,num;int n,m,t,vis[10][10];int dx[]={-1,0,1,0};int dy[]={0,-1,0,1};char map[10][10];int abc(int p){ return p>=0?p:-p;}void dfs(int x,int y,int sum){ int i,xx,yy; if(flag==1) return; if(x==ex&&y==ey&&sum==t) { flag=1; return; } int min=abc(x-ex)+abc(y-ey); if(min>t-sum||(min+ t-sum )%2!=0) return; for(i=0;i<4;i++) { xx=x+dx[i]; yy=y+dy[i]; if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&map[xx][yy]!='X') /*在map范围内且可以继续搜下去*/ { vis[xx][yy]=1; dfs(xx,yy,sum+1); vis[xx][yy]=0; } }}int main(){ int i,j; while(~scanf("%d%d%d",&n,&m,&t)) { if(n==0&&m==0&&t==0) break; num=0; for(i=0;i<n;i++) { scanf("%s",map[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; /*记录终点坐标*/ if(map[i][j]=='X') num++; /*记录墙的数量*/ } } if(n*m-num-1<t) { printf("NO\n"); continue; } flag = 0; memset(vis, 0, sizeof(vis)); vis[sx][sy] = 1; dfs(sx, sy, 0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0;} }
0 0
- DFS——HDU1010(经典的奇偶剪枝)
- hdu1010 经典的DFS+奇偶剪枝
- hdu1010 (dfs+奇偶剪枝)
- hdu1010(dfs+奇偶剪枝)
- HDU1010(dfs+奇偶剪枝)
- HDU1010-奇偶剪枝(DFS)
- hdu1010 dfs奇偶剪枝
- HDU1010奇偶剪枝DFS
- HDU1010 DFS+奇偶剪枝
- HDU1010 dfs奇偶剪枝
- HDU1010 DFS+奇偶剪枝
- hdu1010(DFS + 奇偶剪枝)
- hdu1010(DFS+奇偶剪枝)
- hdu1010 dfs(奇偶剪枝,路径剪枝)
- hdu1010 Tempter of the Bone(dfs+奇偶剪枝)
- hdu1010 Tempter of the Bone (dfs)奇偶剪枝
- HDU1010 Tempter of the Bone (DFS & 奇偶剪枝)
- HDU1010 奇偶剪枝 + 预先筛选 + DFS搜索
- iOS类别可以扩展类的属性
- 统计学习方法笔记(8)——提升方法之前向分布算法和提升树
- 欢迎使用CSDN-markdown编辑器
- JSP(1)
- dpdk 在VMware 中的安装部署
- DFS——HDU1010(经典的奇偶剪枝)
- 转载:关于函数返回值的几种情况
- L3-005. 垃圾箱分布
- Linux下安装jdk(openSUSE为例)
- 5.CAS增加验证码
- hdu1010 dfs+剪枝
- window 命令行运行的java 和 javac 显示的版本不同
- Quartz入门例子简介 从入门到菜鸟(四)
- MySQL 手动设置自动递增伟某一值