#include <bits/stdc++.h>
#define no "NO"
#define yes "YES"
#define F first
#define S second
#define vec vector
#define task "main"
#define ll long long
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maximize(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool minimize(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)2e5 + 9;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
int numRow, numCol, numSize;
vector<string> a;
vector<vector<int>> dist;
struct Data {
int x, y, sizeVer, sizeHor;
};
bool inside(int i, int j) {
return i >= 1 && i <= numRow && j >= 1 && j <= numCol;
}
void main_code() {
int sR, sC, eR, eC;
cin >> numRow >> numCol >> numSize >> sR >> sC >> eR >> eC;
a.assign(numRow + 1, "");
FOR(i, 1, numRow) {
cin >> a[i];
a[i] = " " + a[i];
}
dist.assign(numRow + 1, vector<int> (numCol + 1, -1));
queue<ii> whiteQueue;
queue<Data> blackQueue;
dist[sR][sC] = 0;
whiteQueue.push(make_pair(sR, sC));
while (whiteQueue.size() + blackQueue.size()) {
while (whiteQueue.size()) {
int x = whiteQueue.front().F, y = whiteQueue.front().S;
whiteQueue.pop();
FOR(i, 0, 3) {
int u = x + dx[i], v = y + dy[i];
if (!inside(u, v) || dist[u][v] != -1) continue;
if (a[u][v] == '.') {
dist[u][v] = dist[x][y];
whiteQueue.push(make_pair(u, v));
}
else {
dist[u][v] = dist[x][y] + 1;
blackQueue.push({u, v, numSize - 1, numSize - 1});
}
}
}
while (blackQueue.size()) {
auto [x, y, sizeVer, sizeHor] = blackQueue.front();
blackQueue.pop();
if (sizeVer == 0 || sizeHor == 0) {
whiteQueue.push(make_pair(x, y));
}
FOR(i, 0, 3) {
int u = x + dx[i], v = y + dy[i];
if (!inside(u, v)) continue;
int newVer = sizeVer + (dy[i] != 0), newHor = sizeHor + (dx[i] != 0);
if (newVer < 0 || newHor < 0) continue;
if (dist[u][v] == -1) {
dist[u][v] = dist[x][y];
blackQueue.push({u, v, newVer, newHor});
}
}
}
}
cout << dist[eR][eC];
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0;
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); cout << "\n"; }
}
/* Lak lu theo dieu nhac */
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |