#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = numeric_limits<ll>::max() / 8;
void Main()
{
int R,C, jump; cin >> R >> C >> jump;
pi start; int ox, oy;
cin >> ox >> oy; start = pi(ox, oy);
cin >> ox >> oy;
vector<vi> a(R + 1, vi(C + 1));
FOR(i, 1, R){
FOR(j, 1, C){
char c; cin >> c;
a[i][j] = (c == '#' ? 1 : 0);
}
}
// 0: vertical, 1: horizontal
const int dx[4] = {0, 1, -1, 0};
const int dy[4] = {1, 0, 0, -1};
auto init = [&](int x, int y) -> bool{
return (0 < x && x <= R) && (0 < y && y <= C);
};
vector<vi> dist(R + 1, vi(C + 1, 0));
vector<vector<vi>> can(2, vector<vi>(R + 1, vi(C + 1, 0)));
dist[start.fi][start.se] = 1;
// white -> expand with 0 distance
// black -> expand until we can't and will increase the distance
queue<pi> expand; expand.push(start);
while(sz(expand)){
// find all nodes that are available
queue<pi> operation;
while(sz(expand)){
pi e = expand.front(); expand.pop();
int x = e.fi, y = e.se;
FOR(i, 0, 3){
int nx = x + dx[i];
int ny = y + dy[i];
if(init(nx, ny) && !dist[nx][ny]){
if(a[nx][ny]){
dist[nx][ny] = dist[x][y] + 1;
can[0][nx][ny] = can[1][nx][ny] = jump - 1;
operation.emplace(nx, ny);
}
else{
dist[nx][ny] = dist[x][y];
expand.emplace(nx, ny);
}
}
}
}
while(sz(operation)){
pi e = operation.front(); operation.pop();
int x = e.fi, y = e.se;
if(!can[0][x][y] || !can[1][x][y]){
expand.emplace(x, y);
}
FOR(i, 0, 3){
int nx = x + dx[i];
int ny = y + dy[i];
if(min(can[0][x][y] - abs(dx[i]), can[1][x][y] - abs(dy[i])) < 0) continue;
if(init(nx, ny) && !dist[nx][ny]){
dist[nx][ny] = dist[x][y];
can[0][nx][ny] = can[0][x][y] - abs(dx[i]);
can[1][nx][ny] = can[1][x][y] - abs(dy[i]);
operation.emplace(nx, ny);
}
}
}
}
cout << dist[ox][oy] - 1 << el;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; while(t--) Main();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int32_t main()':
Main.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |