HDU1010 DFS+剪枝
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 110290 Accepted Submission(s): 29967
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 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.
//很明显是一道dfs,但普通的dfs会超时,看了题解才明白原来可以奇偶剪枝。从起点到终点的距离如果是奇数t也是奇数才能到达,
//从起点到终点的距离如果是偶数t是偶数才能到达。从起点到终点的最短距离是两点的坐标之差,如果不走这条最短路,只有多走的步数是偶数
//步时才能到达终点。因此可以排除很多情况。可以自己画图看看。
//如果地图上可走的点少于t也不行。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,m,t;
int ans,tim;
int dir[][]={,,-,,,,,-};
bool vis[][];
char map[][];
void dfs(int sx,int sy)
{
if(tim>t)
return;
if(map[sx][sy]=='D')
{
if(tim==t)
ans=;
return;
}
for(int i=;i<;i++)
{
int x=sx+dir[i][],y=sy+dir[i][];
if(x<||x>=n||y<||y>=m) continue;
if(vis[x][y]) continue;
if(map[x][y]=='X') continue;
vis[x][y]=;
tim++;
dfs(x,y);
vis[x][y]=;
tim--;
if(ans==) return;
}
}
int main()
{
int sx,sy,ex,ey;
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
if(n==&&m==&&t==) break;
int cnt=;
for(int i=;i<n;i++)
{
scanf("%s",map[i]);
for(int j=;j<m;j++){
if(map[i][j]=='S')
{
sx=i;sy=j;
}
if(map[i][j]=='D')
{
ex=i;ey=j;
}
if(map[i][j]=='.')
cnt++;
}
}
int tem1=fabs(sx+sy-ex-ey);
ans=;
if(tem1%==t%&&cnt+>=t){
memset(vis,,sizeof(vis));
vis[sx][sy]=;
tim=;
dfs(sx,sy);
}
if(ans==) printf("YES\n");
else printf("NO\n");
}
return ;
}
HDU1010 DFS+剪枝的更多相关文章
- hdu-1010 dfs+剪枝
思路: 剪枝的思路参考博客:http://www.cnblogs.com/zibuyu/archive/2012/08/17/2644396.html 在其基础之上有所改进 题意可以给抽象成给出一个 ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- LA 6476 Outpost Navigation (DFS+剪枝)
题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...
随机推荐
- c标签遍历List<Map<String, Object>> 数据格式
<c:forEach varStatus="loop" var="dataMap" items="${dataMap}"> &l ...
- FMDB线程安全
//打开数据库 如果没有就创建 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUse ...
- [Hadoop] 在Ubuntu系统上一步步搭建Hadoop(单机模式)
1 Hadoop的三种创建模式 单机模式操作是Hadoop的默认操作模式,当首次解压Hadoop的源码包时,Hadoop无法了解硬件安装环境,会保守地选择最小配置,即单机模式.该模式主要用于开发调试M ...
- 【Go入门教程4】struct类型(struct的匿名字段)
struct Go语言中,也和C或者其他语言一样,我们可以声明新的类型,作为其它类型的属性或字段的容器.例如,我们可以创建一个自定义类型person代表一个人的实体.这个实体拥有属性:姓名和年龄.这样 ...
- Collection接口
Collection接口所定义的方法: clear:清空 retainAll 求一个Collection和另一个 Collection的交集. object[] toArray() 把里面的各个对象 ...
- Oracel基础知识
1.查看oracle环境变量命令 echo %path% 2.监听程序:Oracle服务器端的一种网络服务.监听程序创建在数据库的服务器端,主要作用监视客户的连接请求.因此在客户端创建监听毫无意义 ...
- OC编程之道-创建对象之单例模式
一 何为单例singleton模式?(what) 保证一个类只有一个实例,并提供一个访问它的全局访问点. 二 何时使用单例模式?(where) 1类只能有一个实例,而且必须从一个为人熟知的访问点对其访 ...
- java序列化
什么是java序列化,如何实现java序列化? 我们有时候将一个java对象变成字节流的形式传出去或者从一个字节流中恢复成一个java对象,例如,要将java对象存储到硬盘或者传送给网络上的其他计算机 ...
- java学习第三天 数组
java中数组一样存在多维,二维数组,三维数组..... 二维数组的定义 格式: 数据类型 [][] 数组名 = new 数据类型 [][]; 动态初始化 数据类型[][] 数组名 = new 数 ...
- Swift - UITableView里的cell底部分割线左侧靠边
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, ...