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>
#include<fstream>
using namespace std;
ifstream fin("WINTER.inp");
ofstream fout("WINTER.out");
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 5e5, mxm = 1e5, sq = 400;
const int base = (1 << 18);
const ll inf = 1e9;
const ld pre = 1e-6;
int r, c, n, sti, stj, eni, enj;
vt<vt<char>>a;
vt<vt<int>>d;
vt<set<int>>row, col;
bool inside(int i, int j){
return(i >= 1 && j >= 1 && i <= r && j <= c);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> r >> c >> n;
a.resize(r + 1, vt<char>(c + 1));
d.resize(r + 1, vt<int>(c + 1));
cin >> sti >> stj >> eni >> enj;
row.resize(r + 1); col.resize(c + 1);
forr(i, 1, r + 1){
forr(j, 1, c + 1){
cin >> a[i][j]; d[i][j] = 1e9;
row[i].insert(j); col[j].insert(i);
}
}
deque<pair<int, int>>dq; dq.pb({sti, stj});
d[sti][stj] = 0;
while(!dq.empty()){
auto [i, j] = dq.front(); dq.pop_front();
if(a[i][j] == '.'){
for(int t = 0; t < 4; t++){
int nwi = i + x[t], nwj = j + y[t];
if(inside(nwi, nwj)){
if(d[nwi][nwj] > d[i][j]){
d[nwi][nwj] = d[i][j];
dq.push_front({nwi, nwj});
}
}
}
}
for(auto ii: {i - n, i + n}){
vt<int>rem;
if(!inside(ii, 1))continue;
auto it = row[ii].lower_bound(j - n + 1), it2 = row[ii].upper_bound(j + n - 1);
if(it == row[ii].end())continue;
for(auto iter = it; iter != it2; iter++){
int jj = *iter;
if(d[ii][jj] > d[i][j] + 1){
d[ii][jj] = d[i][j] + 1;
dq.push_back({ii, jj});
}
rem.pb(jj);
}
for(auto jj: rem){
row[ii].erase(jj); col[jj].erase(ii);
}
for(int jj = 1; jj < rem.size(); jj++){
assert(rem[jj] == rem[jj - 1]);
}
}
for(auto jj : {j - n, j + n}){
vt<int>rem;
if(!inside(1, jj))continue;
auto it = col[jj].lower_bound(i - n + 1), it2 = col[jj].upper_bound(i + n - 1);
if(it == col[jj].end())continue;
for(auto iter = it; iter != it2; iter++){
int ii = *iter;
if(d[ii][jj] > d[i][j] + 1){
d[ii][jj] = d[i][j] + 1;
dq.push_back({ii, jj});
}
rem.pb(ii);
}
for(auto ii: rem){
row[ii].erase(jj); col[jj].erase(ii);
}
}
}
int ans = 1e9;
for(int i = eni - n + 1; i <= eni + n - 1; i++){
for(int j = enj - n + 1; j <= enj + n - 1; j++){
if(inside(i, j)){
ans = min(ans, d[i][j] + 1);
}
}
}
ans = min(ans, d[eni][enj]);
cout << ans;
return(0);
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:51:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
51 | auto [i, j] = dq.front(); dq.pop_front();
| ^
Main.cpp:80:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int jj = 1; jj < rem.size(); jj++){
| ~~~^~~~~~~~~~~~
# | 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... |