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 ...
随机推荐
- 建立企业内部mavenserver并使用Android Studio公布公共项目
由于Android Studio使用了Gradle构建工具,在library依赖的处理上是将被依赖的library作为一个module引入(拥有一份完整的library拷贝),而Eclipse的ADT ...
- 10 Questions To Make Programming Interviews Less Expensive--reference
Conducting Interview is not cheap and costs both time and money to a company. It take a lot of time ...
- [转] Makefile中调用Shell
1.在Makefile中只能在target中调用Shell脚本,其他地方是不能输出的.比如如下代码就是没有任何输出: VAR="Hello" echo "$(VAR)&q ...
- Java多线程——多线程的基本概念和使用
一.进程和线程的基础知识 1.进程和线程的概念 进程:运行中的应用程序称为进程,拥有系统资源(cpu.内存) 线程:进程中的一段代码,一个进程中可以有多段代码.本身不拥有资源(共享所在进程的资源) 在 ...
- java沙箱机制原理
参考文档如下: http://www.2cto.com/kf/201012/79578.html
- java.io.FileNotFoundException: class path resource [META-INF/xfire/services.xml] cannot be opened because it does not exist
解决办法: maven创建项目时: META-INF目录下面新建一个xfire文件夹,把services.xml文件放到这个文件夹里,再将整个META-INF拷贝到WEB-INF中 clean一下工程 ...
- 电厂MIS,SIS简介
MIS(Management Information System)管理信息系统,主要指的是进行日常事务操作的系统,它使管理人员及时了解公司现状和各种消息,它是电力企业管理现代化的重要标志. 一个典型 ...
- 修复 status 为 unusable 的 index
以DBA权限登陆,执行以下脚本即可. declare -- 指向所有 UNUSABLE 状态的 index 的游标 cursor c is select index_n ...
- securecrt简介
SecureCRT是最常用的终端仿真程序,简单的说就是Windows下登录UNIX或Liunx服务器主机的软件,本文主要介绍SecureCRT的使用方法和技巧 VanDyke CRT 和 VanDyk ...
- display:inline,display:inline-block,display:block 区别
之前一直迷惑于display:inline/inline-block/block的异同,在度娘谷哥的帮助下,突然有了一点思路. 按照网上的介绍,inline将对象转化为内联元素,block将对象转化为 ...