HDU ACM 1010 Tempter of the Bone ->简单搜索

分析:搜索题,注意剪枝。

#include<iostream>
using namespace std;

int dfs(int si,int sj,int ei,int ej,int mt);
char map[8][8];
int m,n,fa;

int main()
{
	int t,i,j,wall,sti,stj,eni,enj;
	while(cin>>n>>m>>t &&(n||m||t))
	{
		wall=0;
		fa=0;
		for(i=1;i<=n;i++)
		{
			getchar();
			for(j=1;j<=m;++j)
			{
				cin>>map[i][j];
				if(map[i][j]=='X')
				{
					wall++;
				}
				else if(map[i][j]=='S')
				{
					sti=i;
					stj=j;
				}
				else if(map[i][j]=='D')
				{
					eni=i;
					enj=j;
				}
			}
		}
		if(wall>=n*m-t)
		{
			cout<<"NO"<<endl;
			continue;
		}
		map[sti][stj]='X';
		dfs(sti,stj,eni,enj,t);
		if(fa)
		{
			cout<<"YES"<<endl;
		}
		else
		{
			cout<<"NO"<<endl;
		}
	}
	return 0;
}

int dfs(int si,int sj,int ei,int ej,int mt)
{
	int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
	int temp,i;
	if(mt<0) return 0;
	if(si<1||sj<1||si>n||sj>m) return 0;
	temp=mt-(ei-si)-(ej-sj);
	if(temp<0 || temp&1) return 0;
	if(map[si][sj]==map[ei][ej] && mt==0)
	{
		fa=1;
		return 1;
	}
	for(i=0;i<4;i++)
	{
		if(map[si+dir[i][0]][sj+dir[i][1]]!='X')
		{
			map[si+dir[i][0]][sj+dir[i][1]]='X';
			if(!dfs(si+dir[i][0],sj+dir[i][1],ei,ej,mt-1))
			{
				map[si+dir[i][0]][sj+dir[i][1]]='.';
			}
			else
			{
				return 1;
			}
		}
	}
	return 0;
}
时间: 2024-12-26 15:13:31

HDU ACM 1010 Tempter of the Bone ->简单搜索的相关文章

杭电 HDU ACM 1046 Tempter of the Bone

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

【深搜加剪枝五】HDU 1010 Tempter of the Bone

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

HDU 1010 Tempter of the Bone heuristic 剪枝法

本题就是考剪枝法了. 应该说是比较高级的应用了.因为要使用heuristic(经验)剪枝法.要总结出这个经验规律来,不容易.我说这是高级的应用也因为网上太多解题报告都没有分析好这题,给出的程序也很慢,仅仅能过掉,由此看来很多人没有做好这道题. 这里我需要更正一下网上流行的说法:奇偶剪枝法. 其实本题使用奇偶剪枝法并不能太大提高速度,只能说仅仅让使用奇偶剪枝过掉.所以网上说本题使用奇偶剪枝的,其实并不能提高速度. 原因: 奇偶剪枝只能剪枝一次,不能在递归的时候剪枝,因为只要初始化位置符合奇偶性,那

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

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): 68206    Accepted Submission(s): 18719 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): 70665    Accepted Submission(s): 19487 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): 125945    Accepted Submission(s): 33969 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): 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(深搜)

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