제출 #766949

#제출 시각아이디문제언어결과실행 시간메모리
766949sysiaMaze (JOI23_ho_t3)C++17
67 / 100
2072 ms110524 KiB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;

void solve(){
    int n, m, k; cin >> n >> m >> k;
    T s, e; cin >> s.first >> s.second;
    cin >> e.first >> e.second;
    vector a(n+1, vector<char>(m+1));
    for (int i = 1; i<=n; i++){
        for (int j = 1; j<=m; j++){
            cin >> a[i][j];
        }
    }
    auto in = [&](T ss){
        return ss.first >= 1 && ss.second >= 1 && ss.first <= n && ss.second <= m;
    };
    queue<T>q, q2;
    vector<set<int>>tab(n+1);
    for (int i = 1; i<=n; i++){
        for (int j = 1; j<=m; j++){
            tab[i].insert(j);
        }
    }
    vector<int>X = {-1, 1, 0, 0};
    vector<int>Y = {0, 0, -1, 1};
    vector dist(n+1, vector<int>(m+1, oo));
    dist[s.first][s.second] = 0;
    q.push(s);
    tab[s.first].erase(s.second);
    while (dist[e.first][e.second] == oo){
        while ((int)q.size()){
            auto v = q.front(); q.pop();
            for (int ck = 0; ck < 4; ck++){
                T x = {v.first + X[ck], v.second + Y[ck]};
                if (!in(x)) continue;
                if (a[x.first][x.second] == '.'){
                    if (dist[x.first][x.second] == oo){
                        dist[x.first][x.second] = dist[v.first][v.second];
                        tab[x.first].erase(x.second);
                        q.push(x);
                    }
                } 
            }
            q2.push(v);
        }
        auto v = q2.front(); q2.pop();
        debug(v);
        //?opt - set aktywnych wierszy?
        for (int i = max(1ll, v.first - k); i<=min(n, v.first+k); i++){
            int from = (i == v.first - k || i == v.first + k ? max(1ll, v.second-k+1) : max(1ll, v.second-k));
            int to = (i == v.first - k || i == v.first + k ? min(m, v.second+k-1) : min(m, v.second+k));
            for (auto now = tab[i].lower_bound(from); now != tab[i].end() && *now <= to; now++){
                dist[i][*now] = dist[v.first][v.second]+1;
                q.push({i, *now});
            }
            while (1){
                auto it2 = tab[i].lower_bound(from);
                if (it2 == tab[i].end() || *it2 > to) break;
                tab[i].erase(it2);
            }
        }
    }
    // for (int i = 1; i<=n; i++){
    //     for (int j = 1; j<=m; j++){
    //         cerr << dist[i][j] << " ";
    //     }
    //     cerr << "\n";
    // }
    cout << dist[e.first][e.second] << "\n";
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...