难度:4
知识点:广度优先搜索
这个题只要状态想对了就很简单,状态再加上一维,就是到了这个点用过瞬移的次数,显然第三维只有0和1两种
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(x) ((int) x.size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
const int N = 1005;
struct node {
int x, y, t;
node(int a, int b, int c): x(a), y(b), t(c) {}
};
int n, m, d, r;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
string s[N];
void bfs() {
queue<node> q;
q.push(node(0, 0, 0));
int dist[N][N][2];
fill((int*) dist, (int*) dist + N * N * 2, -1);
dist[0][0][0] = 0;
while(!q.empty()) {
node now = q.front(); q.pop();
if (now.x == n - 1 && now.y == m - 1) {
cout << dist[now.x][now.y][now.t]; return;
}
for (int i = 0; i < 4; i++) {
int x = now.x + dx[i], y = now.y + dy[i];
if (x < 0 || y < 0 || x >= n || y >= m || dist[x][y][now.t] > -1 || s[x][y] == '#') continue;
q.push(node(x, y, now.t)); dist[x][y][now.t] = dist[now.x][now.y][now.t] + 1;
}
if (now.t == 0) {
int x = now.x + d, y = now.y + r;
if (x < 0 || y < 0 || x >= n || y >= m || dist[x][y][1] > -1 || s[x][y] == '#') continue;
q.push(node(x, y, 1)); dist[x][y][1] = dist[now.x][now.y][0] + 1;
}
}
cout << -1;
}
int main() {
cin >> n >> m >> d >> r;
for (int i = 0; i < n; i++) cin >> s[i];
bfs();
return 0;
}