Submission #1293668

#TimeUsernameProblemLanguageResultExecution timeMemory
1293668nasjesMaze (JOI23_ho_t3)C++20
100 / 100
1259 ms1785092 KiB
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#include <unordered_map>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

typedef long double ld;
const ll dim=1e6 + 10;
const ll mod=998244353;
const ll inf=1e18+77;
//#define endl "\n"
#define fi first
#define pb push_back
#define se second
#define vll vector<ll>

ll n,t;
vector<vll> g, vis;

ll a[dim];
ll  m;
ll r, c;
queue<pll> q1, q2;
struct state{
    ll val, h, v;
};
ll fl=0;
vector<vector<state> > dst;
vector<pll> buf, buf1;
void check(ll x, ll y, ll val, ll h, ll v, ll tp){
    if(x<1 || x>r || y<1 || y>c)return;
    if(dst[x][y].val<=val)return;

    dst[x][y]={val, h, v};
    if(tp==0){fl=1;  buf.pb({x, y}); q1.push({x, y});}
    else{buf1.pb({x, y}); q2.push({x, y});}
}

void check1(ll x, ll y, ll val){
    if(x<1 || x>r || y<1 || y>c)return;
    if(g[x][y]==1){
        check(x, y, val+1, n-1, n-1, 1);
    }
    else{
        check(x, y, val, 0, 0, 0);
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //cin>>t;
    t=1;
    while(t--){
        cin>>r>>c>>n;
        ll p1, p2, x, y;
        //ll u1, u2;
        char ch;
        cin>>p1>>p2;
        cin>>x>>y;
        g.assign(r+6, vll(c+6, 0));
        vis.assign(r+6, vll(c+6, 0));
        dst.assign(r+6, vector<state>(c+6, {r*c+100,0, 0}));
        for(int i=1; i<=r; i++) {
            for(int j=1; j<=c; j++){
                cin>>ch;
                g[i][j]=(ch=='#');
                // cout<<g[i][j]<<" ";
            }
            //cout<<endl;
        }
        check1(p1, p2, 0);

        while(q1.size()>0){
            fl=0;
            auto [u1, u2]=q1.front();
            //  cout<<u1<<" "<<u2<<endl;
            state cur=dst[u1][u2];
            //  cout<<cur.val<<" "<<cur.h<<" "<<cur.v<<endl;
            q1.pop();
            if(cur.v>0) {
                check(u1 - 1, u2, cur.val, cur.h, cur.v - 1, 0);
                check(u1 + 1, u2, cur.val, cur.h, cur.v - 1, 0);
            }
            if(cur.h>0) {
                check(u1, u2 - 1, cur.val, cur.h - 1, cur.v, 0);
                check(u1, u2 + 1, cur.val, cur.h - 1, cur.v, 0);
            }
            if(q1.size()==0){
                for(int i=0; i<buf.size(); i++){
                    u1=buf[i].fi; u2=buf[i].se;
                    cur=dst[u1][u2];
                    check1(u1 - 1, u2, cur.val);
                    check1(u1 + 1, u2, cur.val);
                    check1(u1, u2 - 1, cur.val);
                    check1(u1, u2 + 1, cur.val);
                }
                q1=q2;
                buf=buf1;
                buf1.clear();
                while(q2.size()>0)q2.pop();
            }
        }
        cout<<dst[x][y].val<<endl;

    }

    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...