hdu2010(dfs+剪枝)
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 131619 Accepted Submission(s):
35432
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.
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.
doggie can survive, or "NO" otherwise.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
#include<iostream>
#include<string.h>
using namespace std;
#include<math.h>
char s[][];
int ax,ay,bx,by,n,m,k;
int t[][]={,,-,,,,,-},visit[][],flag;
void dfs(int x,int y,int count)
{
int mx,my,i;
if(x==bx&&y==by)
{
if(k==count){ flag=;
}
return;
}
if(count>=k)
return;
if(s[x][y]!='X')
{
for(i=;i<;i++)
{
mx=x+t[i][];
my=y+t[i][];
if(s[mx][my]!='X'&&mx>=&&mx<=n&&my>=&&my<=m&&!visit[mx][my])
{
visit[mx][my]=;
dfs(mx,my,count+);
visit[mx][my]=;
if(flag) //注意,在找到了目标之后,就不需要再找!以往编写dfs时,没有注意这点,就会超时
return;
}
}
}
}
int main()
{
while(cin>>n>>m>>k)
{
if(n==&&m==&&k==)
return ;
int i,count=;
flag=;
for(i=;i<=n;i++)
{
getchar();
for(int j=;j<=m;j++)
{
cin>>s[i][j];
if(s[i][j]=='S')
{
ax=i;ay=j;
}
if(s[i][j]=='D')
{
bx=i;by=j;
} }
}
getchar();
memset(visit,,sizeof(visit));
if((abs(ax-bx)+abs(ay-by))>k||(ax+ay+bx+by+k)%==)//剪枝干
{
//cout<<"*"<<endl;
cout<<"NO"<<endl;
continue;
}
visit[ax][ay]=;
count=;
dfs(ax,ay,count);
if(flag==)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}
hdu2010(dfs+剪枝)的更多相关文章
- *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 ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
随机推荐
- python while循环与for循环
今天刚看了一下python的while和for循环,所以打算记录一下: while语句是python中的循环条件语句,while 判断条件 : pass break 例如: i = 1 sum = 1 ...
- SpringBoot学习10:springboot整合mybatis
需求:通过使用 SpringBoot+SpringMVC+MyBatis 整合实现一个对数据库中的 t_user 表的 CRUD 的操作 1.创建maven项目,添加项目所需依赖 <!--spr ...
- Vue源码学习三 ———— Vue构造函数包装
Vue源码学习二 是对Vue的原型对象的包装,最后从Vue的出生文件导出了 Vue这个构造函数 来到 src/core/index.js 代码是: import Vue from './instanc ...
- 前端之jquery函数库
jquery介绍 jQuery是目前使用最广泛的javascript函数库.据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库.微软公司甚至把jQuery作为他们的官方库. ...
- 简单了解一下oracle中的显示游标和存储过程
游标 游标主要分两类动态和静态游标,静态游标是编译时知道明确的select语句的游标,静态游标分类两种,显示游标和静态游标,这里只说显示游标 显示游标 declare name emp.ename%t ...
- linux 安装mysql5.6 yum
安装mysql: 查看mysql: rpm -qa | grep -i mysql 安装必要的环境 yum -y install gcc gcc-c++ ncurses-devel perl 查看环境 ...
- linux-shell——03
mkdir 创建一个新目录格式: mkdir [选项-p][路径]目录名 -p 递归创建多级目录 mkdir -p b/c/e/f/g rmdir 删除一个空目录 touch 创建一个空文件,更新文件 ...
- 17-比赛2 F - Fox And Two Dots (dfs)
Fox And Two Dots CodeForces - 510B ================================================================= ...
- 2015 acm taipei L-Reward the Troop(uvalive 7465)(找规律)
原题链接 就大概说的是一个将军要给部下发勋章,他的部下以和别人不一样的勋章为荣,但是他没这么多钱,所以问你最少要多少钱 要求是每个人的上司是他的上两级,他的下两级是他的部下,每个人的勋章不能和他的上司 ...
- (洛谷)P2709 小B的询问
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...