hdu 1010 回溯加奇偶性剪枝
普通的剪枝会超时,必须加入奇偶性剪枝。
直接上图:
AC代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=10; char mp[maxn][maxn]; int d[maxn][maxn]; int n,m,T; const int dx[]={0,0,1,-1}; const int dy[]={1,-1,0,0}; int sx,sy,dox,doy; //难点:加入奇偶性剪枝 bool dfs(int x,int y,int t){ if(t==T&&x==dox&&y==doy) return true; else if(t==T||x==dox&&y==doy) return false; //cut if(abs(dox-x)+abs(doy-y)>T-t) return false; d[x][y]=1; for(int i=0;i<4;++i){ int nx=x+dx[i],ny=y+dy[i]; if(nx<0||nx>=n||ny<0||ny>=m) continue; if(d[nx][ny]==0){ if(dfs(nx,ny,t+1)) return true; } } d[x][y]=0; return false; } int main(){ while(scanf("%d%d%d",&n,&m,&T)==3&&n!=0){ memset(d,0,sizeof(d)); for(int i=0;i<n;++i) scanf("%s",mp[i]); //find start and door for(int i=0;i<n;++i) for(int j=0;j<m;++j){ if(mp[i][j]=='X') d[i][j]=1; else if(mp[i][j]=='S') {sx=i;sy=j;} else if(mp[i][j]=='D') {dox=i;doy=j;} } int l=abs(dox-sx)+abs(doy-sy); if(l%2!=T%2) { printf("NO\n"); continue; } if(dfs(sx,sy,0)) printf("YES\n"); else printf("NO\n"); } return 0; }
如有不当之处欢迎指出!
hdu 1010 回溯加奇偶性剪枝的更多相关文章
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
- hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...
- hdu 1010 走到终点时刚好花掉所有时间 (DFS + 奇偶性剪枝 )
题意:输入一个n*m的迷宫,和一个T:可以在迷宫中生存的最大时间.S为起点,D为终点.并且,每个格子只能踩一次,且只能维持一秒,然后该块地板就会塌陷.所以你必须每秒走一步,且到D点时,所用时间为T.用 ...
- hdu 1010 Tempter of the Bone (奇偶性剪枝)
题意:有一副二维地图'S'为起点,'D'为终点,'.'是可以行走的,'X'是不能行走的.问能否只走T步从S走到D? 题解:最容易想到的就是DFS暴力搜索,,但是会超时...=_=... 所以,,要有其 ...
- 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(深度+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次. 首先先来介绍一下奇偶性 ...
- hdu 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 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- HDU 1010 Tempter of the Bone 骨头诱惑(DFS+剪枝)
题意: 必须在第t秒走到格子D上,S为起点,D为终点,点就是可以走,X就是墙. 思路: 将迷宫外围四面都筑墙‘X’.深度搜索+奇偶剪枝,再加一个剪枝“无法在指定时间内到达”. #include < ...
随机推荐
- linux_文件类型
linux中文件类型? 在linux系统中一切(目录.普通文件.设备文件等)皆文件 d 目录 - 普通文件 l 链接文件 b 快设备和其他外网设备 普通文件: - ...
- js_10_dom表单
事件的优先级? 先执行事件,后执行标签内置事件,如果事件返回false不执行后面的事件或标签内置事件 如何通过js提交表单? 任意标签定义onclick事件 函数中写入:document.getEle ...
- ssm web.xml配置解析
以下为web.xml的配置<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=& ...
- Struts2实现文件上传和下载,多文件上传,限制文件大小,限制文件类型
文件上传使用的包:commons-upload-xx.jar commons-io-xx.jar 一.实现文件上传: 1.在表单空间中添 ...
- Java NIO 之 Buffer
Java NIO 之 Buffer Java NIO (Non Blocking IO 或者 New IO)是一种非阻塞IO的实现.NIO通过Channel.Buffer.Selector几个组件的协 ...
- History对象和location对象
history对象 History对象包含用户在浏览器窗口中访问过的url.不是所有浏览器都支持该对象. 属性length 返回浏览器历史列表中的URL数量. 方法:back() 加载histor ...
- python selenium 鼠标悬停
#鼠标悬停 chain = ActionChains(driver) implement = driver.find_element_by_link_text() chain.move_to_elem ...
- Python selenium 一个节点两个关联input
HTML代码: 一个节点两个关联input 多出现于密码框 先需要模拟点击进入第一个input,才能激活第二个input. 代码: driver.find_element_by_name('Text ...
- c#基础(一)
一. C#与.Net的关系 C#是一种相当新的编程语言.C#的重要性体现在以下两个方法: 1).它是专门为Microsoft的.net FrameWork一起使用而设计的 (.net FrameWor ...
- 04_VMware虚拟机网络配置
占位占位占位占位占位占位占位占位