#include <bits/stdc++.h>
using namespace std;
//loops (warning : long long)
#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define ROF(i, r, l) for(int i = (r - 1); i >= l; --i)
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
//pairs, tuples
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
//vectors
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define pb push_back
#define eb emplace_back
#define sum_of(v) accumulate(all(v), 0ll)
#define sz(v) (int)v.size()
#define compact(v) v.erase(unique(all(v)), end(v))
//binary search
#define lwb lower_bound
#define upb upper_bound
//other stuffs
#define dbg(x) "[" #x " = " << (x) << "]"
#define file(task) if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
template<typename T>
bool minimize(T& a, const T& b){
if(a > b) return a = b, true;
return false;
}
template<typename T>
bool maximize(T& a, const T& b){
if(a < b) return a = b, true;
return false;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using db = double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void testcase(int ntestcase){
int N, M, K;
cin >> N >> M >> K;
int sx, sy, tx, ty;
cin >> sx >> sy >> tx >> ty;
--sx, --sy, --tx, --ty;
vector<vector<char>> a(N, vector<char>(M));
for(int i = 0; i < N; ++i){
for(int j = 0; j < M; ++j){
cin >> a[i][j];
}
}
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
vector<vector<int>> dist(N, vector<int>(M, -1));
dist[sx][sy] = 0;
deque<pi> q1;
deque<tuple<int, int, int, int>> q2;
q1.push_back(mp(sx, sy));
while(!q1.empty()){
//white nodes
while(!q1.empty()){
int x, y; tie(x, y) = q1.front(); q1.pop_front();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i], ny = y + dy[i];
if(0 <= nx && nx < N && 0 <= ny && ny < M && dist[nx][ny] == -1){
if(a[nx][ny] == '.'){
dist[nx][ny] = dist[x][y];
q1.push_back(mp(nx, ny));
} else{
dist[nx][ny] = dist[x][y] + 1;
q2.push_back(mt(nx, ny, 0, 0));
}
}
}
}
//black nodes
while(!q2.empty()){
int x, y, p, q; tie(x, y, p, q) = q2.front(); q2.pop_front();
if(p == K-1 & q == K-1) q1.push_back(mp(x, y));
for(int i = 0; i < 4; ++i){
int nx = x + dx[i], ny = y + dy[i];
if(0 <= nx && nx < N && 0 <= ny && ny < M && dist[nx][ny] == -1){
if(p + abs(dx[i]) < K && q + abs(dy[i]) < K){
dist[nx][ny] = dist[x][y];
q2.push_back(mt(nx, ny, p + abs(dx[i]), q + abs(dy[i])));
}
}
}
}
}
cout << dist[tx][ty] << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
#endif // LOCAL
int T = 1;
// cin >> T;
FOR(i, 0, T) testcase(i);
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... |