이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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)
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<set<int>> str(r+1), stc(c+1);
for (int i=1; i<=r; ++i) for (int j=1; j<=c; ++j) str[i].emplace_hint(str[i].end(), j);
for (int i=1; i<=c; ++i) for (int j=1; j<=r; ++j) stc[i].emplace_hint(stc[i].end(), j);
for (int i=1; i<=r; ++i) for (int j=1; j<=c; ++j) cin >> a[i][j];
auto bfs_white=[&](vector<pair<int, int>> roots, int distance) -> vector<pair<int, int>> {
queue<pair<int, int>> q;
vector<pair<int, int>> found;
for (auto &i:roots) q.emplace(i.first, i.second), dist[i.first][i.second]=distance, str[i.first].erase(i.second), stc[i.second].erase(i.first);
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;
str[x].erase(y);
stc[y].erase(x);
q.emplace(x, y);
}else{
found.emplace_back(x, y);
}
}
}
return found;
};
auto bfs_black=[&](vector<pair<int, int>> roots, int distance) -> vector<pair<int, int>> {
vector<pair<int, int>> found;
queue<pair<pair<int, int>, int>> q;
for (auto &i:roots) q.push({i, 0});
while (q.size()){
int u=q.front().first.first, v=q.front().first.second, d=q.front().second;
q.pop();
if (d==2) continue;
for (auto it=str[u].lower_bound(v-n+1); it!=str[u].end();){
int x=u, y=*it;
if (y-v>=n) break;
q.push({{x, y}, d+1});
dist[x][y]=distance;
found.emplace_back(x, y);
it=str[u].erase(it);
stc[y].erase(x);
}
for (auto it=stc[v].lower_bound(u-n+1); it!=stc[v].end();){
int x=*it, y=v;
if (x-u>=n) break;
q.push({{x, y}, d+1});
dist[x][y]=distance;
found.emplace_back(x, y);
it=stc[v].erase(it);
str[x].erase(y);
}
}
return found;
};
int cur=0;
vector<pair<int, int>> roots{{sr, sc}};
while (dist[gr][gc]>=1e9){
bfs_white(roots, cur).swap(roots);
bfs_black(roots, ++cur).swap(roots);
}
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;
}
# | 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... |