This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 6000005;
const int dirr[] = {1, 0, -1, 0}, dirc[] = {0, 1, 0, -1};
int r, c, n;
int sr, sc, gr, gc;
string s[MAXN];
vi d[MAXN];
vii nd[MAXN];
queue<ii> bfs, nbfs;
vector<bool> vis[MAXN];
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> r >> c >> n;
cin >> sr >> sc >> gr >> gc;
sr--; sc--; gr--; gc--;
REP (i, 0, r) {
cin >> s[i];
}
REP (i, 0, r) {
d[i] = vi(c, INF);
nd[i] = vii(c, {INF, INF});
vis[i] = vector<bool>(c);
}
bfs.push({sr, sc});
int curdist = 0;
while (!bfs.empty()) {
vii pot;
while (!bfs.empty()) {
auto [ur, uc] = bfs.front(); bfs.pop();
d[ur][uc] = curdist;
pot.pb({ur, uc});
REP (k, 0, 4) {
int vr = ur + dirr[k], vc = uc + dirc[k];
if (vr < 0 || vr >= r || vc < 0 || vc >= c) {
continue;
}
if (s[vr][vc] == '#') {
continue;
}
if (mnto(d[vr][vc], d[ur][uc])) {
bfs.push({vr, vc});
}
}
}
for (auto [ur, uc] : pot) {
REP (k, 0, 4) {
int vr = ur + dirr[k], vc = uc + dirc[k];
if (vr < 0 || vr >= r || vc < 0 || vc >= c) {
continue;
}
if (d[vr][vc] != INF || vis[vr][vc]) {
continue;
}
vis[vr][vc] = 1;
nd[vr][vc] = {1, 1};
nbfs.push({vr, vc});
}
}
cerr << curdist << '\n';
while (!nbfs.empty()) {
auto [ur, uc] = nbfs.front(); nbfs.pop();
cerr << ' ' << ur << ' ' << uc << '\n';
bfs.push({ur, uc});
REP (k, 0, 4) {
int vr = ur + dirr[k], vc = uc + dirc[k];
if (vr < 0 || vr >= r || vc < 0 || vc >= c) {
continue;
}
if (d[vr][vc] != INF || vis[vr][vc]) {
continue;
}
ii tmp = {nd[ur][uc].FI + abs(dirr[k]),
nd[ur][uc].SE + abs(dirc[k])};
if (tmp.FI > n || tmp.SE > n) {
continue;
}
if (mnto(nd[vr][vc], tmp)) {
vis[vr][vc] = 1;
nbfs.push({vr, vc});
}
}
}
curdist++;
}
cout << d[gr][gc] << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |