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>
using namespace std;
// #define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)(x).size())
#define sumof(x) accumulate(all(x), 0ll)
struct DisjointSetUnion{
vector<int> lab, val;
void init(int n){
lab.assign(n+1, -1);
val.assign(n+1, -1);
}
int find_set(int v){
return lab[v]<0?v:lab[v]=find_set(lab[v]);
}
bool union_sets(int a, int b){
a=find_set(a); b=find_set(b);
if (a!=b){
int new_val=val[a];
if (lab[a]>lab[b]) swap(a, b);
lab[a]+=lab[b];
lab[b]=a;
val[a]=new_val;
val[b]=-1;
return 1;
}
return 0;
}
int get_val(int x){
return val[find_set(x)];
}
};
struct LinkedList{
int n;
DisjointSetUnion prv, nxt;
void init(int _n){
n=_n;
prv.init(n+1);
nxt.init(n+1);
for (int i=0; i<=n+1; ++i) prv.val[i]=i-1, nxt.val[i]=i+1;
}
void erase(int x){
int t1=prv.get_val(x), t2=nxt.get_val(x);
nxt.union_sets(x, t1);
prv.union_sets(x, t2);
}
int get(int x){
x=max(x, 0);
return nxt.get_val(max(0, prv.get_val(x)));
}
};
template<class T>
struct Vec{
int sz;
T data[6000001];
void clear(){
sz=0;
}
int size() const {
return sz;
}
void push_back(const T &x){
data[sz++]=x;
}
auto & operator[](int x){
return data[x];
}
const auto & operator[](int x) const {
return data[x];
}
};
template<class T>
struct Queue{
int bq, eq;
T data[6000001];
int size() const {
return eq-bq+1;
}
void clear(){
bq=1; eq=0;
}
T & front(){
return data[bq];
}
void push(const T &x){
data[++eq]=x;
}
void pop(){
++bq;
}
};
const int dx[]={1, -1, 0, 0, 1, 1, -1, -1}, dy[]={0, 0, 1, -1, 0, 1, -1, 1, -1};
int n, r, c;
int sr, sc, gr, gc;
void solve(){
cin >> r >> c >> n;
cin >> sr >> sc >> gr >> gc;
vector<vector<char>> a(r+1, vector<char>(c+1));
vector<vector<int>> dist(r+1, vector<int>(c+1, 1e9));
vector<vector<int>> vis(r+1, vector<int>(c+1));
vector<LinkedList> lr(r+1), lc(c+1);
for (int i=1; i<=r; ++i) lr[i].init(c);
for (int i=1; i<=c; ++i) lc[i].init(r);
for (int i=1; i<=r; ++i) for (int j=1; j<=c; ++j) cin >> a[i][j];
Vec<pair<int, int>> roots;
roots.push_back({sr, sc});
Queue<pair<int, int>> q;
auto bfs_white=[&](int distance){
q.clear();
for (int i=0; i<roots.size(); ++i) q.push({roots[i].first, roots[i].second}), dist[roots[i].first][roots[i].second]=distance, lr[roots[i].first].erase(roots[i].second), lc[roots[i].second].erase(roots[i].first);
roots.clear();
while (q.size()){
int u=q.front().first, v=q.front().second;
q.pop();
for (int i=0; i<4; ++i){
int x=u+dx[i], y=v+dy[i];
if (x<1 || y<1 || x>r || y>c || dist[x][y]<1e9) continue;
if (a[x][y]=='.'){
dist[x][y]=distance;
lr[x].erase(y);
lc[y].erase(x);
q.push({x, y});
}else if (!vis[x][y]){
vis[x][y]=1;
roots.push_back({x, y});
}
}
}
};
Queue<pair<pair<int, int>, int>> qq;
auto bfs_black=[&](int distance){
qq.clear();
for (int i=0; i<roots.size(); ++i) qq.push({roots[i], 0});
roots.clear();
while (qq.size()){
int u=qq.front().first.first, v=qq.front().first.second, d=qq.front().second;
qq.pop();
if (d==3) continue;
if ((d&1)==0){
for (int y=lr[u].get(v-n+1); y!=c+1; y=lr[u].nxt.get_val(y)){
int x=u;
if (y-v>=n) break;
qq.push({{x, y}, d|1});
dist[x][y]=distance;
roots.push_back({x, y});
lc[y].erase(x);
lr[x].erase(y);
}
}
if ((d&2)==0){
for (int x=lc[v].get(u-n+1); x!=r+1; x=lc[v].nxt.get_val(x)){
int y=v;
if (x-u>=n) break;
qq.push({{x, y}, d|2});
dist[x][y]=distance;
roots.push_back({x, y});
lc[y].erase(x);
lr[x].erase(y);
}
}
}
};
int cur=0;
while (dist[gr][gc]>=1e9){
bfs_white(cur);
bfs_black(++cur);
}
cout << dist[gr][gc];
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ntests=1;
// cin >> ntests;
for (int i=1; i<=ntests; ++i) solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:73:11: warning: 'roots.Vec<std::pair<int, int> >::sz' may be used uninitialized in this function [-Wmaybe-uninitialized]
73 | data[sz++]=x;
| ~~~~^
Main.cpp:118:24: note: 'roots.Vec<std::pair<int, int> >::sz' was declared here
118 | Vec<pair<int, int>> roots;
| ^~~~~
# | 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... |