Submission #1215483

#TimeUsernameProblemLanguageResultExecution timeMemory
1215483Zero_OPMaze (JOI23_ho_t3)C++20
8 / 100
44 ms13452 KiB
#include <bits/stdc++.h>

using namespace std;

//loops (warning : long long)
#define FOR(i, l, r) for(int i = (l); i < (r); ++i)
#define ROF(i, r, l) for(int i = (r - 1); i >= l; --i)
#define rep(i, l, r) for(int i = (l); i < (r); ++i)

//pairs, tuples
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second

//vectors
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define pb push_back
#define eb emplace_back
#define sum_of(v) accumulate(all(v), 0ll)
#define sz(v) (int)v.size()
#define compact(v) v.erase(unique(all(v)), end(v))

//binary search
#define lwb lower_bound
#define upb upper_bound

//other stuffs
#define dbg(x) "[" #x " = " << (x) << "]"
#define file(task) if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }

template<typename T>
bool minimize(T& a, const T& b){
    if(a > b) return a = b, true;
    return false;
}

template<typename T>
bool maximize(T& a, const T& b){
    if(a < b) return a = b, true;
    return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using db = double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

void testcase(int ntestcase){
    int N, M, K;
    cin >> N >> M >> K;

    int sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;
    --sx, --sy, --tx, --ty;

    vector<vector<char>> a(N, vector<char>(M));
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < M; ++j){
            cin >> a[i][j];
        }
    }


    deque<tuple<int, int, int>> q;
    const int dx[4] = {-1, 1, 0, 0};
    const int dy[4] = {0, 0, 1, -1};

    vector<vector<int>> dist(N, vector<int>(M, -1));
    dist[sx][sy] = 0;
    q.push_back(mt(sx, sy, 0));

    while(!q.empty()){
        int x, y, free; tie(x, y, free) = q.front(); q.pop_front();
        // cout << x << ' ' << y << ' ' << free << ' ' << dist[x][y] << '\n';
        if(x == tx && y == ty){
            cout << dist[x][y];
            return;
        }

        for(int i = 0; i < 4; ++i){
            int nx = x + dx[i], ny = y + dy[i];
            if(0 <= nx && nx < N && 0 <= ny && ny < M && dist[nx][ny] == -1){
                if(a[nx][ny] == '.'){
                    dist[nx][ny] = dist[x][y];
                    q.push_front(mt(nx, ny, max(free-1, 0)));
                } else{
                    if(free > 0){
                        dist[nx][ny] = dist[x][y];
                        q.push_front(mt(nx, ny, free-1));
                    } else{
                        dist[nx][ny] = dist[x][y] + 1;
                        q.push_back(mt(nx, ny, K-1));
                    }
                }
            }
        }
    }
}

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

#ifdef LOCAL
    freopen("task.inp", "r", stdin);
#endif // LOCAL

    int T = 1;
//    cin >> T;
    FOR(i, 0, T) testcase(i);

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