hdu 1010(迷宫搜索,奇偶剪枝)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1010
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 144191 Accepted Submission(s): 38474
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.
YES
把矩阵看成如下形式:
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
从为 0 的格子走一步,必然走向为 1 的格子 。
从为 1 的格子走一步,必然走向为 0 的格子 。
即:
从 0 走向 1 必然是奇数步,从 0 走向 0 必然是偶数步。
所以当遇到从 0 走向 0 但是要求时间是奇数的或者 从 1 走向 0 但是要求时间是偶数的,都可以直接判断不可达!
#include<bits/stdc++.h>
char s[][];
int ax,ay,bx,by,n,m,k;
int t[][]={,,-,,,,,-};//方向引导数组
int vist[][],flag;
void dfs(int x,int y,int c)
{
int i,mx,my;
if(x==bx&&y==by)//找到终点
{
if(k==c)//恰好在规定时间找到终点则标志位置1
flag=;
return;
}
if(c>=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&&!vist[mx][my])//判断能不能往这个方向走
{
vist[mx][my]=;
dfs(mx,my,c+);
vist[mx][my]=;//回退
if(flag) //注意,在找到了目标之后,就不需要再找!以往编写dfs时,没有注意这点
return;
}
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&k)>&&(n+m+k))
{
int i,c;
for(i=;i<=n;i++)
{
getchar();
for(int j=;j<=m;j++)
{
scanf("%c",&s[i][j]);
if(s[i][j]=='S')
{
ax=i;//起点
ay=j;
}
if(s[i][j]=='D')
{
bx=i;//终点
by=j;
}
}
}
getchar();
memset(vist,,sizeof(vist));
if(abs(ax-bx)+abs(ay-by)>k||(ax+bx+ay+by+k)%==) // 最短距离都大于k的剪枝和奇偶剪枝
{
printf("NO\n");
continue;
}
vist[ax][ay]=;
flag=;
c=;
dfs(ax,ay,c);
if(flag==)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
hdu 1010(迷宫搜索,奇偶剪枝)的更多相关文章
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
- hdu 1010 dfs搜索
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 杭电1010(dfs + 奇偶剪枝)
题目: The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked ...
- hdu 1010 回溯加奇偶性剪枝
普通的剪枝会超时,必须加入奇偶性剪枝. 直接上图: AC代码: #include<cstdio> #include<cstring> #include<algorithm ...
- hdoj--1010--Tempter of the Bone(搜索+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1010 Tempter of the Bone 骨头诱惑(DFS+剪枝)
题意: 必须在第t秒走到格子D上,S为起点,D为终点,点就是可以走,X就是墙. 思路: 将迷宫外围四面都筑墙‘X’.深度搜索+奇偶剪枝,再加一个剪枝“无法在指定时间内到达”. #include < ...
- HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...
- hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- POJ 2955 Brackets 区间DP 最大括号匹配
http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...
- Springboot事务使用与回滚
Springboot中事务的使用: 1.启动类加上@EnableTransactionManagement注解,开启事务支持(其实默认是开启的). 2.在使用事务的public(只有public支持事 ...
- CSS3,3D效果轮播图
---恢复内容开始--- 大家还记得我昨天的3D拖拽立方体吗??我昨天还说过css还可以做轮播图,所以咱们今天就写一下,css的轮播图吧! ....这个轮播图主要是用CSS3里的transform的旋 ...
- Java从入门到精通——数据库篇Mongo DB 安装启动及配置详解
一.概述 Mongo DB 下载下来以后我们应该如何去安装启动和配置才能使用Mongo DB,本篇博客就给大家讲述一下Mongo DB的安装启动及配置详解. 二.安装 1.下载Mongo DB ...
- C# CRC16算法实现【转】
/// <summary> /// CRC校验 /// </summary> public class CRC { #region CRC16 public static by ...
- Git连接GitLab远程仓库
1.简介 远程仓库是指托管在网络上的项目仓库,现在互联网上有很多项目托管平台,比如github.gitlab等.为了不公开自己项目代码,可以在自己的服务器上搭建自己的项目仓库,最常见的是搭建GitLa ...
- Ubuntu 18.04 的网络配置
netplan简介 目前,ubuntu18.04上使用了netplan 作为网络配置工具:在终端上配置网络参数跟之前的版本有比较大的差别 Netplan工作流程如下图所示:通过读取 /etc/net ...
- ajax 请求调用问题
http://localhost/dev/list 和 http://127.0.0.1/dev/list 最近在架构整体常规通用系统的解决方案,遭遇AJAX请求不执行的问题,刚开始以为Spring ...
- Ganglia安装
一.rrdtool安装 1.1 安装依赖包 由于rrdtool依赖的包比较多,而且包之间也存在依赖,故使用yum安装由于服务器无法联网,故使用iso文件创建本地yum源,方法见下: (1)创建iso存 ...
- python 小词云
# Author:Alex.wang# Date:2017.06.02# Version:3.6.0 import matplotlib.pyplot as pltfrom wordcloud imp ...