hdu 1010 Tempter of the Bone(dfs)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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.
'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.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std; char p[][];
int a[][] = {-,,,,,,,-}; // 方向数组
int flag; // 标记是否能够在第t秒时到达出口
int n,m,t;
int sx,sy,dx,dy; // 起始地点 和 出口的地点 void dfs(int x,int y,int k)
{
int i;
if (x< || y< || x>n || y>m) // 判断是否超出边界
return ;
if (k == &&x == dx && y==dy) // 如果刚好在第t秒时到达出口 flag = 1
{
flag = ;
return ;
}
int tmp = k - abs(x-dx)-abs(y-dy);
if (tmp < || tmp&) // 判断 1、是否剩余的最小步数 > 剩余的时间 2、剩余时间和剩余的最小步数的奇偶性是否相同
return ; for (i = ; i < ; i ++)
{
if (p[x+a[i][]][y+a[i][]] != 'X') // 如果时墙则不能通过
{
p[x+a[i][]][y+a[i][]] = 'X'; // 走过之后路就坍塌了(之后不能在通过)
dfs(x+a[i][],y+a[i][],k-);
if (flag) return ; // 如果满足条件直接退出
p[x+a[i][]][y+a[i][]] = '.'; // 当前搜的这条路可能不满足条件 下次再搜别的路时要恢复原样
}
}
return ;
}
int main ()
{
int i,j;
while (scanf("%d%d%d",&n,&m,&t)!=EOF)
{
getchar();
int sum = ;
if (n==&&m==&&t==)
break;
for (i = ; i <= n; i ++)
{
for (j = ; j <= m; j ++)
{
scanf("%c",&p[i][j]);
if (p[i][j] == 'S'){sx = i; sy = j;}
else if (p[i][j] == 'D'){dx = i; dy = j;}
else if (p[i][j] == 'X') sum ++;
}
getchar();
} if (n*m-sum <= t) // 如果可以走的路的步数小于t 直接pass
{
printf("NO\n");
continue;
}
p[sx][sy] = 'X'; // 出发之后不能再回到起点
flag = ;
dfs(sx,sy,t);
if (flag)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
最新文章
- Centos 7 安装 和 卸载 Mysql5.7(压缩包)
- mvc与三层结构
- 【转】 StringUtils中 isNotEmpty 和isNotBlank的区别
- JAVA多线程与多进程
- 给eclipse装一些插件
- Json序列反序列类型处理帮助类
- 2.7. 属性的各种设置选项(Core Data 应用程序实践指南)
- 13 用Css做下拉菜单
- StringBulider简单用法
- Qt 5.9.4 如何静态编译和部署?
- js回溯法计算最佳旅行线路
- Python语言中的按位运算
- 机器学习之KNN算法
- MVC 打印解决方案--SNF快速开发平台3.1
- English trip V1 - 24. Accommodations Teacher:Maple Key: make suggestions 提出建议
- HDU 3746 - Cyclic Nacklace &; HDU 1358 - Period - [KMP求最小循环节]
- Matchvs 使用记录
- [HAOI2015]按位或
- 通过TodoList案例对比Vue.js的MVVM设计模式与JQuery的MVP设计模式
- md5.digest()与md5.hexdigest()之间的区别及转换