hdoj1010 奇偶剪枝+DFS
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 151082 Accepted Submission(s): 40265
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
分析:这个题目用一般的搜索无法完成,因为题目要求在指定的时间内完成,所以只好一步一步来,用DFS解决。
但是如果这样结果会超时,网上说是用一种奇偶剪枝的方法来间断搜索时间,下面是剪枝的简单理论,一看就懂:
把map看作
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 需要奇数步
从 0->0 需要偶数步
那么设所在位置 (px,py) 与 目标位置 (ppx,ppy)
如果abs(px-ppx)+abs(py-ppy)为偶数,则说明 abs(x-y) 和 abs(dx-dy)的奇偶性相同,需要走偶数步
如果abs(x-y)+abs(dx-dy)为奇数,那么说明 abs(x-y) 和 abs(dx-dy)的奇偶性不同,需要走奇数步
理解为 abs(si-sj)+abs(di-dj) 的奇偶性就确定了所需要的步数的奇偶性!!
而 (t-setp)表示剩下还需要走的步数,由于题目要求要在 ti时 恰好到达,那么 (t-step) 与 abs(x-y)+abs(dx-dy) 的奇偶性必须相同
因此 t+abs(px-ppx)-abs(py-ppy) 必然为偶数!!!
下面是AC代码:
#include<iostream>
#include<cstdlib>
#include<string.h>
using namespace std;
int n,m,t; // 行n列m时间t
int flag; //标记是否可以survive
int dir[4][2]={1,0,-1,0,0,1,0,-1}; //用来表示下,上,右,左
int visit[8][8]; //用来标识地图每一点是否被经过
char map[8][8]; //记录地图每一点的属性
int px,py,ppx,ppy; //分别表示‘S’的坐标和‘D’的坐标
void dfs(int x,int y,int count){
if(count>t)
return;
else if(map[x][y]=='D'){
if(count==t)
flag=1;
return;
}
for(int i=0;i<4;i++){
int xx=x+dir[i][0]; //移动
int yy=y+dir[i][1];
if(map[xx][yy]!='X'&&xx>=0&&xx<n&&yy>=0&&yy<m)
if(visit[xx][yy]==0){
visit[xx][yy]=1;
dfs(xx,yy,count+1);
if(flag)
return;
visit[xx][yy]=0;
}
}
}
int main(){
while(cin>>n>>m>>t&&(m+n+t)){
flag=0;
for(int i=0;i<n;i++){
cin>>map[i];
for(int j=0;j<m;j++){
if(map[i][j]=='S'){
px=i;
py=j;
}
if(map[i][j]=='D'){
ppx=i;
ppy=j;
}
}
}
if((abs(px-ppx)+abs(py-ppy)+t)&1){ //奇偶剪枝
cout<<"NO"<<endl;
continue;
}
memset(visit,0,sizeof(visit));
visit[px][py]=1; //将‘S’处标记为已经过
dfs(px,py,0);
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
体会:学习了奇偶剪枝技巧和DFS算法。
hdoj1010 奇偶剪枝+DFS的更多相关文章
- HDOJ1010 (DFS+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdoj--1010<dfs+奇偶剪枝>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...
- <TLE>奇偶剪枝hdoj1010
(奇偶剪枝)转自百度百科,讲的非常棒: http://baike.baidu.com/link?url=3g5bW7LszRVOVvFDFp6cL0ZZnAaOLUdpaNjc2leHoxkKU9Eh ...
- 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 ...
- 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 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
- hdoj 1010 Tempter of the Bone【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 ...
随机推荐
- Zabbix二次开发_02获取数据
最近准备写一个zabbix二次页面的呈现.打算调用zabbix api接口来进行展示. 具体流程以及获取的数据. 1. 获得认证密钥 2. 获取zabbix所有的主机组 3. 获取单 ...
- springMVC学习(3)-springMVC和mybatis整合
一.需求:使用springmvc和mybatis完成商品列表查询. 二.整合思路:springMVC+mybaits的系统架构: 1步):整合dao层 mybatis和spring整合,通过sprin ...
- shell 7echo命令
echo用于字符串的输出 1. 显示普通字符串 #shell #!/bin/sh echo "Hello world" echo Hello world Hello world H ...
- 定义function的层级
不知道标题拟的对不对,今天犯了个错误,图一是正确的写法. 图一 为了代码可以重复利用,我把其中两个方法独立出来,如图二. 图二 后来发现运行错误,说Gxrc未定义,百思不得其解,后来琢磨了好久,才发现 ...
- Leetcode 之Simplify Path @ python
Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...
- ThinkPHP 5使用 Composer 组件名称可以从https://packagist.org/ 搜索到
http://www.phpcomposer.com/ 1 这个是国内的composer网站 thinkphp5自带了composer.phar组件,如果没有安装,则需要进行安装 以下命令全部在项目目 ...
- C# 控制台程序(Console Application )启动后隐藏
背景 前段时间给项目编写了数据适配器,读取其他系统的数据后推送到MQ上,我们的系统通过订阅MQ的方式来获取.由于其他系统支持C#编程,且为了一时方便,选择了C#的控制台程序. 最近用户在使用中,总是不 ...
- API网关Kong系列(三)添加服务
进入之前部署好的kong-ui,默认第一次登陆需要配置kong服务的地址 进入API菜单,点击+号 按照要求填入相关信息 至此完成,可以使用诸如 https://your.domain.com:208 ...
- 关于raid5的一系列问题
前几天我的一个同事在对计划采购的存储进行测试,期间聊到了raid5的话题,我和他的意见产生了分歧.他的说法是raid5不能挂太多盘是因为如果挂太多盘写惩罚会非常严重导致性能下降.而我的观点则是对于ra ...
- express有中间件的增删改查
var express = require('express');引入express框架 var router = express.Router();引入router路由级中间件 var data = ...