HDU 1010 dfs

参考链接:
参考代码
剪枝方法

错误原因:将图形四周都设置为墙的时候,位置设置错,将图像内的设置为墙了

#include <iostream>
#include <stack>
#include <cmath>
using namespace std;
char Line[8][8];
int Ti[8][8];

struct node {
	int x;
	int y;
	int t;
};

int X[] = {0, 0, 1, -1};
int Y[] = {1, -1, 0, 0};

void Show() {
	for (int i = 1; i <= 5; ++i) {
		for (int j = 1; j <= 5; ++j) {
			cout << Line[i][j];
		}
		cout << endl;
	}

}

bool dfs(node ch, int x2, int y2) {
	//cout << "-----"  << endl;
	//cout << ch.t << " " << ch.x << " " << ch.y << endl;
	node temp;
	bool flag;
	//Show();
	if (ch.t < 0 || (ch.t == 0 && ch.x != x2 && ch.y != y2)) {
		//cout << "111" << endl;
		return false;
	}

	for (int i = 0; i < 4; ++i) {
		temp.x = ch.x + X[i];
		temp.y = ch.y + Y[i];
		temp.t = ch.t - 1;

		if (temp.t < 0) {
			//cout << "222" << endl;
			return false;
		}

		if (temp.x == x2 && temp.y == y2) {
			if (temp.t == 0)
				return true;
		}
		if (Line[temp.x][temp.y] == '.') {
			//cout << "(" << temp.x << " " << temp.y << " " << temp.t << ")" << endl;
			Line[temp.x][temp.y] = 'X';
			flag = dfs(temp, x2, y2);
			Line[temp.x][temp.y] = '.';
			if (flag)
				return true;
		}
	}
	return false;
}

int main() {
	int N, M, T;
	int x1, y1, x2, y2;
	while (cin >> N >> M >> T) {
		if (N == 0 && M == 0 && T == 0) {
			break;
		}
		for (int i = 1; i <= N; ++i) {
			for (int j = 1; j <= M; ++j) {
				cin >> Line[i][j];
				Ti[i][j] = 0;
				if (Line[i][j] == 'S') {
					x1 = i;
					y1 = j;
				} else if (Line[i][j] == 'D') {
					x2 = i;
					y2 = j;
				}
			}
		}
		for (int i = 0; i < N; ++i) {
			Line[i][0] = 'X';
			Line[i][M + 1] = 'X';
		}
		for (int i = 0; i <= M; ++i) {
			Line[0][i] = 'X';
			Line[N + 1][i] = 'X';
		}
		node ch;
		ch.x = x1;
		ch.y = y1;
		ch.t = T;
		stack<node> Nodes;
		Nodes.push(ch);
		if ((T - abs(ch.x - x2) - abs(ch.y - y2)) % 2 != 0) {
			cout << "NO" << endl;
			continue;
		}
		bool flag = dfs(ch, x2, y2);
		if (flag)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值