知识点:图论
难度: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;
}