This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)|| dist[u][v] != -1) continue;
int newVer = sizeVer - (dy[i] != 0), newHor = sizeHor - (dx[i] != 0);
if (newVer < 0 || newHor < 0) continue;
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 (stderr)
Main.cpp: In function 'int main()':
Main.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(task".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... |