Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏
Tempter of the Bone
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 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.
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
NO
YES
#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) 收藏的更多相关文章
- 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 ...
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- python爬虫_简单使用百度OCR解析验证码
百度技术文档 首先要注册百度云账号: 在首页,找到图像识别,创建应用,选择相应的功能,创建 安装接口模块: pip install baidu-aip 简单识别一: 简单图形验证码: 图片: from ...
- PS快捷键大全,记住这些就够了!
希望能帮到大家更好的学习.
- Python中的类方法、实例方法、静态方法
类方法 @classmethod 在python中使用较少,类方法传入的第一个参数是 cls,是类本身: 类方法可以通过类直接调用或者通过实例直接调用,但无论哪种调用方式,最左侧传入的参数一定是类本身 ...
- 设计师都爱用的UI标注软件有哪些?
UI标注软件现在是设计师(UI.PM.前端等)必备的一款软件.设计稿是UI设计师日常工作中的产出物之一,当然,做出了高保真设计稿并不意味着你的工作结束了,因为你还得与下游的开发工程师进行对接. 我们经 ...
- Split Array Largest Sum LT410
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- c#对dataset和list集合压缩和解压,能提高访问速度
public class YS { public static byte[] Decompress(byte[] data) { byte[] bData; MemoryStream ms = new ...
- 前端js数据排序
销量统计系统中国地图热力分布图需要显示一个各省区销量列表,并按从多到少排序.本着轻易不修改后台数据源的原则,决定在前端进行修改实现.其实也容易实现,将数据存放一个数据<省区名称,销量>,然 ...
- mybatis学习 十三 resultMap标签 一对一
1 .<resultMap>标签 写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系. 在写<select>标签中,有一个resultType属性,此时s ...
- 全球IT经理一致喜爱的ITSM系统、it服务台软件
- python学习 day20 (3月27日)----(单继承多继承c3算法)
继承: 提高代码的重用性,减少了代码的冗余 这两个写法是一样的 Wa('青蛙').walk() #青蛙 can walk wa = Wa('青蛙') wa.walk() #青蛙 can walk 1. ...