tempter of the bone(搜索+剪纸)
来源:互联网 发布:淘宝怎样退货退款申请 编辑:程序博客网 时间:2024/05/17 06:36
题意:给出一个图,一个起点,终点,判断在指定时间内是否能从起点到达终点。
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
思路:dfs搜索,但不加剪枝的话会超时,主要一个判断是如果从起点到终点的最短时间都要大于指定时间的话,一定不可以,另外就是从网上参考:
如果当前的狗所在的坐标与D的坐标奇偶性不一样,那么狗需要走奇数步。同理,如果狗所在坐标与D的坐标奇偶性一样,那么狗需要走偶数步数。也就是说,狗的坐标x、y和对2取余是它的奇偶性,Dxy和对2取余是D的奇偶性。两个奇偶性一加再对2取余,拿这个余数去与剩下时间对2取余的余数作比较即可。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <string>using namespace std;char maze[10][10];int next1[4] = {0,1,0,-1};int next2[4] = {1,0,-1,0};int vis[10][10];int n,m,t,flag;void dfs(int x, int y, int cnt){ if(maze[x][y] == 'D') { if(cnt == t) //注意是在该时间恰好到达 flag = 1; return ; } if(cnt >= t) return ; for(int i=0; i<4; i++) { int dx = x + next1[i]; int dy = y + next2[i]; if(dx >=0 && dx < n && dy >=0 && dy <m) { if(!vis[dx][dy] && maze[dx][dy] != 'X') { vis[dx][dy] = 1; dfs(dx,dy,cnt+1); vis[dx][dy] = 0; if(flag) //注意:已经找到了的话就不需要在搜下去了 return ; } } }}int main(){ while(cin >> n >> m >> t) { if(!n && !m && !t) break; int a,b,c,d; for(int i=0; i<n; i++) { getchar(); for(int j=0; j<m; j++) { scanf("%c", &maze[i][j]); if(maze[i][j] == 'S') { a = i; b = j; } if(maze[i][j] == 'D') { c = i; d = j; } } } getchar(); if(abs(a-c) + abs(b-d) > t || (t+ a+b + c+d)%2 == 1) //注意 { cout << "NO" << endl; continue; } memset(vis, 0, sizeof vis); vis[a][b] = 1; //注意起点从一开始就访问过了 flag = 0; dfs(a,b,0); if(flag) cout << "YES" << endl; else cout << "NO" << endl; } return 0;}
0 0
- tempter of the bone(搜索+剪纸)
- Tempter of the Bone(搜索)
- Tempter of the Bone(深度搜索)
- hdu 1010 Tempter of the Bone(搜索)
- 杭电1010Tempter of the Bone(搜索)
- hdu-1010-Tempter of the Bone(搜索 优化)
- HDU 1010 Tempter of the Bone(搜索经典题)
- HDOJ 题目1010Tempter of the Bone(搜索,dfs)
- HDOJ 1010 Tempter of the Bone(搜索,奇偶剪枝)
- HDU1010 - Tempter of the Bone (搜索+剪枝)
- hdoj--1010--Tempter of the Bone(搜索+奇偶剪枝)
- HDU 1010 Tempter of the Bone (搜索 + 奇偶剪枝)
- Q1010 Tempter of the Bone C++ (深度优先搜索)
- 搜索之Tempter of the Bone
- HDU 1010 Tempter of the Bone (搜索)
- hdu 1010 Tempter of the Bone 搜索
- Tempter of the Bone(路径搜索)
- zoj2110 Tempter of the Bone 搜索dfs
- MVPHelper更新日志 --- 新增常规分包模式
- 求积分
- listview设置条目点击的时候不变色(让状态选择器不起作用)
- java.se.io.9.字节数组节点流
- Java网络编程
- tempter of the bone(搜索+剪纸)
- BestCoder Round #861001Price List(数学)
- map与unordered_map
- response 设置响应头的常用几种方法
- Codeforces Round #366 (Div. 2) A和B
- Codeforces Round #366 (Div. 2)
- 常用端口总结
- java.se.io.10.data处理流
- 欧拉函数