Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 102071    Accepted Submission(s): 27649

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

———————————————————————————————
题目的意思是从地图的S点搜到D点,‘.’为路‘X’为墙,问能否正好在t秒时到达终点。
处理方法是直接DFS搜索,加上剪枝。

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std; int m, n, t;
char mp[10][10];
int dir[4][2] = { { 1, 0 }, { -1, 0 }, { 0, -1 }, { 0, 1 } };
bool escape; bool check(int x, int y)
{
if (x < 0 || x >= m||y < 0 || y >= n||mp[x][y] == 'X')
return 0;
return 1;
} void dfs(int si,int sj,int di,int dj,int cnt)
{
if (si == di&&sj == dj&&cnt == t)
{
escape = 1;
return;
}
int tmp = t-cnt-abs(di - si) - abs(dj - sj); if (tmp < 0 || tmp % 2)
return; for (int i = 0; i < 4; i++)
{
int xx = si + dir[i][0];
int yy = sj + dir[i][1];
if (check(xx, yy))
{
mp[xx][yy] = 'X';
dfs(xx, yy, di, dj, cnt + 1);
mp[xx][yy] = '.';
if (escape == 1)
return;
}
}
} int main()
{
int si, sj, di, dj;
while (~scanf("%d%d%d", &m, &n, &t))
{
if (m == 0 || n == 0 || t == 0)
break;
int wall = 0;
for (int i = 0; i < m; i++)
{
scanf("%s", &mp[i]);
for (int j = 0; j < n; j++)
{
if (mp[i][j] == 'S')
{
si = i;
sj = j;
}
if (mp[i][j] == 'D')
{
di = i;
dj = j;
}
if (mp[i][j] == 'X')
wall++;
} }
if (t > m*n - wall)
{
printf("NO\n");
continue;
}
escape = 0;
mp[si][sj] = 'X';
dfs(si, sj, di, dj, 0);
if (escape == 1)
printf("YES\n");
else
printf("NO\n"); }
return 0;
}












Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏的更多相关文章

  1. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  3. HDU1518 Square(DFS) 2016-07-24 15:08 49人阅读 评论(0) 收藏

    Square Problem Description Given a set of sticks of various lengths, is it possible to join them end ...

  4. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

  5. Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  6. A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏

    A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...

  7. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  9. hdu2602 Bone Collector(01背包) 2016-05-24 15:37 57人阅读 评论(0) 收藏

    Bone Collector Problem Description Many years ago , in Teddy's hometown there was a man who was call ...

随机推荐

  1. DtCMS 在IIS7.0 下之伪静态

    1)首先新建一个应用程序池,名称任意,比如:nettest,托管管道模式先暂时设置为集成模式,等下面的一系列设置完成之后再设置成经典模式: 2)部署好站点,并将此站点的应用程序池设置为nettest; ...

  2. 性能(js)

    1.避免全局查找: <script type="text/javascript"> function updateUI(){ var imgs=document.get ...

  3. less 官网讲解 ( http://www.bootcss.com/p/lesscss/ )

    变量 变量允许我们单独定义一系列通用的样式,然后在需要的时候去调用.所以在做全局样式调整的时候我们可能只需要修改几行代码就可以了. // LESS @color: #4D926F; #header { ...

  4. Luogu 2575 高手过招-SG函数

    Solution SG函数跑一遍就过了ouo Code #include<cstring> #include<cstdio> #include<algorithm> ...

  5. phpstudy报告80端口被占用

    将所有的80端口修改为81后重启,还是不行

  6. JSP属性的四种保存范围(page request session application)

    JSP提供了四种属性的保存范围,分别为page.request.session.application 其对应的类型分别为:PageContext.ServletRequest.HttpSession ...

  7. centos 6.5 搭建zookeeper集群

    为什么使用Zookeeper? 大部分分布式应用需要一个主控.协调器或控制器来管理物理分布的子进程(如资源.任务分配等)目前,大部分应用需要开发私有的协调程序,缺乏一个通用的机制协调程序的反复编写浪费 ...

  8. 【Web】Sublime Text 3 连接sftp/ftp(远程服务器)

    在 Win 下常用 Xftp 软件来和远程服务传递文件,但是要是在项目开发的时候频繁的将远程文件拖到本地编辑然后再传回远程服务器,那真是麻烦无比,但是Sublime中SFTP插件,它让这世界美好了许多 ...

  9. 20155312 2006-2007-2 《Java程序设计》第六周学习总结

    20155312 2006-2007-2 <Java程序设计>第六周学习总结 课堂笔记 学习进程 周一看视频-2h 周二以代码为中心看书-3h 课后选择题-5h 教材指导 应试 Linux ...

  10. 字符串方法 split() & replace()

    split() 语法:stringObject.split(separator) 功能:把一个字符串分割成字符串数组 返回值:Array 说明:separator 是必须的,分隔符. var str= ...