http://codeforces.com/problemset/problem/877/D
题意:就是一般的迷宫问题的模型,但不过有一个条件就是一次可以往一个方向最多走k步
做法:错得原因可能是vis标记上面,正确的做法应该是记录x,y这个点的四个方向来的情况,如果他在东方面的点来过了,下次就再从东方面来,就不需要了。
#include "bits/stdc++.h"
using namespace std;
const double eps = 1e-8;
#define lowbit(x) x&-x
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fi first
#define se second
#define makp make_pair
int dcmp(double x) {
if (fabs(x) < eps) return 0;
return (x > 0) ? 1 : -1;
}
typedef long long ll;
typedef unsigned long long ull;
const ull hash1 = 201326611;
const ull hash2 = 50331653;
const int N = 1000000 + 10;
const int M = 1000 + 10;
const int inf = 0x3f3f3f3f;
const ll mod = 1000000000 + 7;
int n, m, k, sx, sy, ex, ey;
char mp[M][M];
int vis[M][M], dis[M][M];
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
int check(int tx, int ty) {
if (tx < 1 || tx > n || ty < 1 || ty > m)
return 0;
if (mp[tx][ty] == '#')
return 0;
return 1;
}
int bfs() {
queue<pii > qu;
qu.push(makp(sx, sy));
vis[sx][sy] = (1 << 4) - 1;
dis[sx][sy] = 0;
while (!qu.empty()) {
pii now = qu.front();
qu.pop();
if (now.fi == ex && now.se == ey) return dis[ex][ey];
for (int i = 0; i < 4; i++) {
int tmp = k, tx = now.fi, ty = now.se;
while (tmp--) {
tx += dx[i], ty += dy[i];
if (!check(tx, ty)) break;
if ((vis[tx][ty] >> i) & 1) break;
vis[tx][ty] |= (1 << i);
if (dis[tx][ty] != 0) dis[tx][ty] = min(dis[tx][ty], dis[now.fi][now.se] + 1);
else dis[tx][ty] = dis[now.fi][now.se] + 1;
qu.push(makp(tx, ty));
}
}
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
cin >> mp[i] + 1;
}
cin >> sx >> sy >> ex >> ey;
cout << bfs() << endl;
return 0;
}
|