HDU 1010

#include <iostream>
using namespace std;
int fgo[4][2] = {
	0,1,
	1,0,
	0,-1,
	-1,0,
};
int ok = 0;
char maze[10][10];
void fago( int n, int m, int x, int y, int dx,int dy,int t,int _t)
{
	if (x == dx && y == dy && _t == t)
	{
		ok = 1;return;
	}
	if (x == dx && y == dy && _t != t)return;
	if (ok == 1||_t>=t)return;
	int tx, ty;
	for (int i = 0;i < 4;i++)
	{
		tx = x + fgo[i][0], ty = y + fgo[i][1],_t++;
		if (tx >= 0 && tx <= n - 1 && ty >= 0 && ty <= m - 1 && _t <= t)
		{
			if (maze[tx][ty] != 'X')
			{
				maze[tx][ty] = 'X';	
				fago( n, m, tx, ty, dx, dy, t, _t);
				maze[tx][ty] = '.';
			}
		}
		_t--;
	}
}
int main()
{
	int n, m, t;
	while (cin >> n >> m >> t, n != 0 && m != 0 && t != 0)
	{
		int dx, dy, sx, sy;
		for (int i = 0;i < n;i++)
		{
			for (int i1 = 0;i1 < m;i1++)
			{
				cin >> maze[i][i1];
				if (maze[i][i1] == 'D')
					dx = i, dy = i1;
				if (maze[i][i1] == 'S')
					sx = i, sy = i1;
			}
		}
		int xx, yy;
		xx = dx - sx;
		if (xx < 0)
			xx = -xx;
		yy = dy - sy;
		if (yy < 0)
			yy = -yy;
		if ((t - xx - yy) % 2 != 0)
			cout << "NO" << endl;
		else
		{
			int _t = 0;
			maze[0][0] = 'X';
			fago(n, m, sx, sy, dx, dy, t, _t);
			if (ok == 1)
				cout << "YES" << endl;
			else cout << "NO" << endl;
		}
		ok = 0;
	}
}

dfs+剪枝(搞了半天就是一种能通过起点终点的坐标和时间就能直接判断能否到达的操作),然后就没什么了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值