[ABC213E] Stronger Takahashi

知识点:图论

难度:4

这个题和前面的一个abc的题很像,几乎一模一样,但是二者还是有区别的,那个题是只能走空地,不能走障碍,用了魔法也是,这个题是可以走障碍,导致了我一直理解不了这个题的题意,可以摧毁一个区域里面的障碍,那是不是用了魔法之后这个区域里面的障碍都没有了,还是仅仅走上去,现在想想好像觉得没有那么难以理解,但是还是有点别扭,

#include <bits/stdc++.h>

using namespace std;

const int N = 505;

struct node {
	int x, y, step;
	node() {}
	node(int a, int b, int c): x(a), y(b), step(c) {}
};

int n, m, h[N][N];
int dx[20] = {-1, 0, 1, 0, -2, -2, -2, -1, 0, 1, 2, 2, 2, 1, 0, -1, -1, -1, 1, 1};
int dy[20] = {0, 1, 0, -1, -1, 0, 1, 2, 2, 2, 1, 0, -1, -2, -2, -2, -1, 1, 1, -1};
char s[N][N];

void bfs() {
	deque<node> q;
	q.push_back(node(1, 1, 0));
	while (!q.empty()) {
		node now = q.front(); q.pop_front();
		if (now.x == n && now.y == m) {
			cout << now.step;
			return;
		}
		if (h[now.x][now.y]) continue;
		h[now.x][now.y] = 1;
		for (int i = 0; i < 4; i++) {
			int x1 = now.x + dx[i];
			int y1 = now.y + dy[i];
			if (x1 < 1 || x1 > n || y1 < 1 || y1 > m) continue;
			if (s[x1][y1] == '#') continue;
			q.push_front(node(x1, y1, now.step));
		}
		for (int i = 0; i < 20; i++) {
			int x1 = now.x + dx[i];
			int y1 = now.y + dy[i];
			if (x1 < 1 || x1 > n || y1 < 1 || y1 > m) continue;
			q.push_back(node(x1, y1, now.step + 1));
		}
	}
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		scanf("%s", s[i] + 1);
	}
	bfs();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值