이 제출은 이전 버전의 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>> st(r+1);
for (int i=1; i<=r; ++i) for (int j=1; j<=c; ++j) st[i].emplace_hint(st[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, st[i.first].erase(i.second);
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;
st[x].erase(y);
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;
for (auto &i:roots){
int u=i.first, v=i.second;
for (int du=-n+1; du<n; ++du){
int x=u+du;
if (x<1 || x>r) continue;
for (auto it=st[x].lower_bound(v-n+1); it!=st[x].end();){
int y=*it;
if (abs(y-v)>=n) break;
dist[x][y]=distance;
found.emplace_back(x, y);
it=st[x].erase(it);
}
}
}
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... |