hdu 1010 启发式搜索+奇偶剪枝

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
#define INF 10000000
int n,m,t;

struct point{
	int x,y;
};
point p;
int vis[10][10];
int vs[10][10];

char a[10][10];
int v[4][2] = {0,1,1,0,-1,0,0,-1};
int fun(point s){
	queue<point> que;
	vs[s.x][s.y] =1;
	int cc[10][10];
	que.push(s);
	cc[s.x][s.y] = 0;
	while(!que.empty()){
		point u = que.front();
		que.pop();
		for(int i = 0;i < 4;i++){
			point tmp;
			tmp.x = v[i][0] + u.x;
			tmp.y = v[i][1] + u.y;
			if(tmp.x < n &&tmp.x >= 0 && tmp.y < m && tmp.y >= 0){
				if(vis[tmp.x][tmp.y] != 1 && vs[tmp.x][tmp.y] != 1){
					if(a[tmp.x][tmp.y] != 'X'){
						if(a[tmp.x][tmp.y] == 'D'){
							return cc[u.x][u.y]+1;
						}
						else{
							vs[tmp.x][tmp.y] = 1;
							cc[tmp.x][tmp.y] = cc[u.x][u.y]+1;
							que.push(tmp);
						}
					}
				}
			}
		}
	}
	return INF;
}
int dfs(point s,int x,int q){
	if(a[s.x][s.y] == 'D'){
		if(x == 0){
			return true;
		}
		else{
			return false;
		}
	}
	else if(x < 0){
		return false;
	}
	else if(x > q){
		return false;
	}else{
		memset(vs,0,sizeof(vs));

		int now = fun(s);
		if(x == now){
			return true;
		}
		if(x < now){
			return false;
		}
		if((x-now)%2){
			return false;
		}
		int t = 0;
		for(int i = 0;i < 4;i++){
			point tmp;
			tmp.x = v[i][0] + s.x;
			tmp.y = v[i][1] + s.y;
			if(tmp.x < n &&tmp.x >= 0 &&tmp.y < m &&tmp.y >= 0){
				if(!vis[tmp.x][tmp.y]){
					if(a[tmp.x][tmp.y] != 'X'){
						vis[tmp.x][tmp.y] = 1;
						t |= dfs(tmp,x-1,q-1);
						vis[tmp.x][tmp.y] = 0;
						if(t){
							return true;
						}
					}
				}
			}

		}
		return t;
	}
}
int main(){
	//freopen("ou.txt","w",stdout);

	while(cin >> n >> m >> t,n||m||t){
		getchar();
		point s;
		int sum = 0;
		for(int i = 0;i< n;i++){
			for(int j = 0;j < m;j++){
				a[i][j] = getchar();
 				if(a[i][j] == 'S'){
 					s.x = i;
 					s.y = j;
 				}
 				if(a[i][j] == 'X'){
 					sum ++;
 				}
			}
 			getchar();
		}

		vis[s.x][s.y] = 1;

		if(dfs(s,t,n*m-sum-1)){
			printf("YES\n");
		}
		else{
			printf("NO\n");
		}
		vis[s.x][s.y] = 0;
	}
	return 0;
} 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 02:36:06

hdu 1010 启发式搜索+奇偶剪枝的相关文章

HDU 1010 (搜索+奇偶剪枝)

题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep>t剪枝 ②搜到一个解后剪枝 ③当前走到终点最少步数>满足条件还需要走的步数剪枝(关键) ③奇偶剪枝(关键):当前走到终点步数的奇偶性应该与满足条件还需要走的步数奇偶性一致. 其中三四两步放在一步中写:remain=abs(x-ex)+abs(y-ey)-abs(dep-t) 奇偶剪枝的原理:abs(

hdu 1010 深搜+剪枝

深度搜索 剪枝 还不是很理解 贴上众神代码 //http://blog.csdn.net/vsooda/article/details/7884772#include<iostream> #include<math.h> using namespace std; char map[10][10]; int N,M,T; int di,dj,escape; int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; void dfs(int x,int y,

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经典题+奇偶剪枝详解】

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

HDU 1010 深搜+奇偶剪枝

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1010 贴个资料: http://acm.hdu.edu.cn/forum/read.php?tid=6158 奇偶剪枝: 对于从起始点 s 到达终点 e,走且只走 t 步的可达性问题的一种剪枝策略. 如下列矩阵 : 从任意 0 到达 任意 0,所需步数均为偶数,到达任意 1 ,均为奇数.反之亦然 所以有,若s走且只走 t 步可到达e,则必有t >= abs(s.x - e.x) + abs(s.y -

hdu 1010(迷宫搜索,奇偶剪枝)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 144191    Accepted Submission(s): 38474 Problem Description The doggie fou

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 &amp;&amp; ZOJ 2110 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 desp

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