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 ...
随机推荐
- Windows下memcached.exe的安装与配置
D:\PHP\Memcached\memcached.exe -d install D:\PHP\Memcached\memcached.exe –m 1024 -d start 假设安装在:D: ...
- inux中fork()函数详解(原创!!实例讲解)
转载自原创博客,欢迎继续转载 点击跳转到原文
- 使用ajax技术实现txt弹出在页面上
使用ajax技术实现txt弹出在页面上 使用ajax技术实现点击按钮,将TXT文本里的内容通过弹出框显示到页面上 /*事件会在页面加载完成后触发.*/ <script> window. ...
- Python自动化之django的ORM
django ORM操作 1.什么是ORM? ORM,即Object-Relational Mapping(对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,这样,我们在具体的操作 ...
- from collections import OrderedDict
在python中,dict这个数据结构由于hash的特性,是无序的,这在有时候会给我们带来一些麻烦,幸运的是, collections模块为我们提供了OrderdDict,当你要获取一个有序的字典对象 ...
- python基础三
多级菜单 多级菜单 可依次选择进入各子菜单 所需新知识点:列表.字典 #!/usr/bin/env python # -*- coding: utf-8 -*- menu = { '北京':{ '海淀 ...
- Python操作rabbitmq 实践笔记
发布/订阅 系统 1.基本用法 生产者 import pika import sys username = 'wt' #指定远程rabbitmq的用户名密码 pwd = ' user_pwd = p ...
- RobotFrameWork(四)变量运算与Evaluate
一.特殊变量运算: 执行结果: 二.Evaluate使用 函数释义:Evaluate是执行Python表达式,并返回执行结果 示例1: 执行结果: 示例2: 执行结果:
- Oracle 过程中检查数据表存在与否
在过程中,尤其是每天执行的任务,通常要检查查询的数据表存在不存在,如果不存在则等待一段时间在进行执行,以下代码实现了这个功能,如果表不存在,抛出异常,交给异常处理代码,确保数据完整性 使用方法:p_C ...
- 两个已排序数组进行合并后的第K大的值--进军硅谷
我看到此题时,首先想到一个一个比较遍历过去,这是最暴力的方法,后面我想到了已经排序,那么对每个数组进行二分,然后比较这两个值.此书第三种解法,挺不错,只对那个长度较小的数组进行二分查找,保证i+j-1 ...