hdoj 1010 Tempter of the Bone 【DFS】+【奇偶剪枝】

题意:从S出发到D停止,并且要在指定的时间t走到(不早不晚)。注意,每个‘.’只能走一次。

分析:DFS,但是用普通的dfs,TL, 所以要剪枝。我们可以想到,如果可以早到D点,但是D点周围有其他可以踩的点,并且可以观察到,从一个点(不是D点)到D点的距离(abs(x-dx)+abs(y-dy))是奇数的话,就要走奇数步,偶数的就走偶数步,()

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define M 10
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {1, -1, 0, 0};
char map[M][M];
int n, m, t;
int sx, sy, ex, ey;
int limit(int x, int y)
{
	return (map[x][y] != 'X'&&x>=0&&x<n&&y>=0&&y<m);
}
int dfs(int x, int y, int step)
{
	if(x == ex&&y == ey&&step == t) return 1;
	int nx, ny;
	int temp = t-step-abs(x-ex)-abs(y-ey);//t-step是剩下的要走的,abs(。。)是距离D 点的距离,相减肯定是偶数,
	if(temp < 0||temp&1) return 0;
	int i;
	for(i = 0; i < 4; i ++){
		nx = x+dx[i]; ny = y+dy[i];
		if(limit(nx, ny)){
			map[nx][ny] = 'X';
			if(dfs(nx, ny, step+1)) return 1;
			map[nx][ny] = '.';
		}
	}
	return 0;
}
int main()
{
	int i, j;
	while(scanf("%d%d%d", &n, &m, &t), n||m||t){
		memset(map, 0, sizeof(map));
		for(i = 0; i < n; i++){
			scanf("%s", map[i]);
		}
		for(i = 0; i < n; i ++){
			for(j = 0; j < m; j ++){
				if(map[i][j] == 'S'){
					sx = i; sy = j;
				}
				if(map[i][j] == 'D'){
					ex = i; ey = j;
				}
			}
		}
		map[sx][sy] = 'X';  //这里一定要注意,要初始化
		int ans = dfs(sx, sy, 0);
		printf("%s\n", ans?"YES":"NO");
	}
	return 0;
}

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010

时间: 2024-08-06 16:50:28

hdoj 1010 Tempter of the Bone 【DFS】+【奇偶剪枝】的相关文章

HDU 1010 Tempter of the Bone (DFS 奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 75141    Accepted Submission(s): 20531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 82702    Accepted Submission(s): 22531 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu 1010 Tempter of the Bone DFS+奇偶剪枝,入门题

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 78390    Accepted Submission(s): 21395 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

Tempter of the Bone(dfs奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 92175    Accepted Submission(s): 25051 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) &amp;&amp; hdu-1015 Safecracker(简单搜索)

http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心外,还需要强力的剪枝. 奇偶性剪枝:参考 http://www.cppblog.com/Geek/archive/2010/04/26/113615.html 1 #include <iostream> 2 #include <cstring> 3 #include <cstdi

HDU 1010 Tempter of Bone DFS + 奇偶剪枝

奇偶剪枝: 对于从起始点 s 到达 终点 e,走且只走 t 步的可达性问题的一种剪枝策略. 如下列矩阵 : 从任意 0 到达 任意 0,所需步数均为偶数,到达任意 1 ,均为奇数.反之亦然 所以有,若s走且只走 t 步可到达e,则必有t >= abs(s.x - e.x) + abs(s.y - e.y),且 (t&1) == ((abs(s.x - e.x) + abs(s.y - e.y))&1). 否则必不可达. #include <iostream> #inclu

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c

hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 89317    Accepted Submission(s): 24279 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone(DFS剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 129289    Accepted Submission(s): 34906 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

Tempter of the Bone (奇偶剪枝?DFS)

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 despe