HDOJ 1010 Tempter of the Bone

题意:一个N * M 的迷宫, 起点为S, 终点为D , 障碍为X, 问你是否恰好花费时间T 的时候到达终点D。
思路:DFS ,纯粹的搜索会直接超时, 所以需要通过剪枝, 也是在网上看到别人说奇偶剪枝,加进去直接AC了。


69234052012-10-15 15:09:01Accepted1010640MS256K1465BC++罗维

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
using namespace std;

struct point
{
	int x, y;
};
int n, m, t;
point spt, dpt;
string maze[10];
//vector<vecot<int> >used;
bool escape;

void dfs(point pt, int t)
{
	int tmp = t - abs(pt.x-dpt.x) - abs(pt.y - dpt.y);
	if(pt.x == dpt.x && pt.y == dpt.y && t == 0)
	{
		escape = true;	
		return ;
	}
	//剪枝
	if (escape || tmp < 0 || tmp&1 )  //tmp&1 如果是奇数则为1 否则为0
		return;
	maze[pt.x][pt.y] = 'X';
	if(pt.x + 1 <n && maze[pt.x + 1][pt.y] != 'X')
	{
		pt.x += 1;
		dfs(pt, t-1);
		pt.x -= 1;
		if(escape) return;
	}
	if(pt.x - 1 >=0 && maze[pt.x - 1][pt.y] != 'X')
	{
		pt.x -= 1;
		dfs(pt, t-1);
		pt.x += 1;
		if(escape) return;
	}
	if(pt.y + 1 < m && maze[pt.x][pt.y + 1] != 'X')
	{
		pt.y += 1;
		dfs(pt, t-1);
			pt.y -= 1;
		if(escape) return;
	}
	if(pt.y - 1 >= 0 && maze[pt.x][pt.y - 1] != 'X')
	{
		pt.y -= 1;
		dfs(pt, t-1);
			pt.y += 1;
		if(escape) return;
	}
	maze[pt.x][pt.y] = '.';
}

int main()
{
	int i, j;
	while(cin>>n>>m>>t && n+m+t != 0)
	{
		for (i=0; i<n; i++)
		{
			cin>>maze[i];
			for (j=0; j<m; j++)
			{
				if(maze[i][j] == 'S')
				{
					spt.x = i;
					spt.y = j;
				}
				else if(maze[i][j] == 'D')
				{
					dpt.x = i;
					dpt.y = j;
				}
			}
		}

		escape = false;
		dfs(spt, t);
		if(escape)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值