HDU_1010——小狗走迷宫DFS
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.
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
YES
#include <stdio.h>
#include <math.h>
char map[][];
int N,M,S_i,S_j,D_i,D_j,Flag;
void found(int,int,int); int main()
{
int i,j,T;
while(scanf("%d%d%d",&N,&M,&T)!=EOF)
{
Flag=;
if(N== && M== && T==) break;
for(i=;i<N;i++)
scanf("%s",map[i]);
for(i=;i<N;i++)
for(j=;j<M;j++)
if(map[i][j]=='S')
{S_i=i;S_j=j;}
else if(map[i][j]=='D')
{D_i=i;D_j=j;}
map[D_i][D_j]='.';
if((int)fabs((double)((S_i+S_j)%-(D_i+D_j)%)) != T%) //奇偶性剪枝
{
puts("NO");
continue;
} //0->1或1->0 必然是奇数步
found(S_i,S_j,T);
printf("%s\n",Flag?"YES":"NO"); //0->0或1->1 必然是偶数步
}
}
void found(int x,int y,int T)
{
map[x][y]='*';
if(T== && x==D_i && y==D_j)
{
Flag=;
/*
printf("\nshow:\n");
for(int i=0;i<N;i++)
printf("%s\n",map[i]);
*/
}
if(T>)
{
if(x+<N&&map[x+][y]=='.') found(x+,y,T-);//这里只能
if(y+<M&&map[x][y+]=='.') found(x,y+,T-);
if(x->=&&map[x-][y]=='.') found(x-,y,T-);
if(y->=&&map[x][y-]=='.') found(x,y-,T-);
}
map[x][y]='.';
}
HDU_1010——小狗走迷宫DFS的更多相关文章
- NYOJ306 走迷宫(dfs+二分搜索)
题目描写叙述 http://acm.nyist.net/JudgeOnline/problem.php?pid=306 Dr.Kong设计的机器人卡多非常爱玩.它经常偷偷跑出实验室,在某个游乐场玩之不 ...
- luogu P1238 走迷宫--DFS模板好(水)题
题目描述 有一个m*n格的迷宫(表示有m行.n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件读入这m*n个数据和起始点.结束点(起始点和结束点都是用两个数据来描述的,分别表示 ...
- 我的第一篇博文:C++最初的路-经典的小游戏走迷宫
写在开始:这个博客建于大二下学期.2年多的学习,从网上借鉴的大牛经验,代码,指导数不胜数,而其中大部分来自别人的博客,于是期待有一天也能把自己在学习过程中的一些经验拿出来与大家分享. 其实我凝望了C+ ...
- openjudge走迷宫(DFS)
题目: 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...
- 走迷宫(DFS)
题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2449&cid=1181 目前dfs 里的递归还是不很懂,AC代码如下: #incl ...
- sdut1269 走迷宫(dfs)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1269 连着做了三个基本的dfs,终于弄懂了搜索 ...
- HDU1010 Tempter of the Bone【小狗是否能逃生----DFS奇偶剪枝(t时刻恰好到达)】
Tempter of the Bone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- sdut 2449走迷宫【最简单的dfs应用】
走迷宫 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_ 题目描述 一个由n * m 个格子组成的迷宫,起点是(1, 1), 终点是(n, m) ...
- HDU 2102 A计划(BFS/DFS走迷宫)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
随机推荐
- server的散热和Linux中温度的检測
当server被放在散热条件不好的条件下,这样会导致硬盘驱动过早损坏,而且server其它的组件也会非常快出现问题. 现代的server主板检測到CPU过热的时候,一般会限制CPU的频率,所以即使se ...
- MySQL定时备份之使用Linux下的crontab定时备份实例
这篇文章主要介绍了使用Linux下的crontab进行MySQL定时备份的例子,需要的朋友可以参考下 复制代码代码如下: ##################################### ...
- Java基础知识强化86:BigInteger类之BigInteger概述和构造方法
1. BigInteger类概述 可以让超过Integer范围内的数据进行运算 2. 构造方法: public BigInteger(String val) 3. 代码示例: package cn ...
- C语言二叉树的建立与遍历
二叉树的建立和遍历都要用到递归,先暂时保存一下代码,其中主要是理解递归的思想,其它的就都好理解了.这里是三种遍历方式,其实理解一种,其它的几个就都理解了,就是打印出来的顺序不一样而已.建立和遍历的方式 ...
- fork安全的gettid高效实现
进程有id,可以通过getpid()获得,线程也有id,但是glibc没有提供封装.需要自己发出系统调用.在关键路径,系统调用还是对性能有影响的.因此我们可以想到类似glibc对getpid做的cac ...
- 小学生之JAVA中的分层
三层架构 三层架构(3-tier application) 通常意义上的三层架构就是将整个业务应用划分为:表现层(UI).业务逻辑层(BLL).数据访问层(DAL). 区分层次的目的即为了“高内聚,低 ...
- 第1章 你真的了解C#吗?
什么是C#? C#是由微软公司开发的一种面向对象且运行于.Net Framework之上的高级程序设计语言,发布于2000年6月. 什么是.Net Framework 我们可以这样去理解.Net Fr ...
- HTML5和CSS3实例教程[总结一]
关于onclick的行为与内容分离 通过链接触发弹出窗口方式 (不推荐使用此方法!!!) <a href='#' onclcik = "window.open('holiday_pay ...
- sql数据库之间数据的转录
private void Form1_Load(object sender, EventArgs e) { BindDataBase(combDataBaseNew, , ""); ...
- TreeView 数据绑定及选中命令处理
昨天接近下班,一个群里面的网友,问treeView绑定后 选中命令怎么来处理,怎么没有效果,而且用MVVM的方式来写:快下班了本来想远程帮他看下,结果就说写个Demo给他看:再加上选中传参: 下面分 ...