Submission #645134

#TimeUsernameProblemLanguageResultExecution timeMemory
645134googleRobots (APIO13_robots)C++17
30 / 100
23 ms10452 KiB
#include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int N,W,H; vector<string> G; vector<vector<vector<int>>> dist; vector<vector<vector<pair<int,int>>>> ma; bool legal(int i, int j, int d){ if (d == 0 && (i+1 == H || G[i+1][j] == 'x')) return 0; if (d == 1 && (j+1 == W || G[i][j+1] == 'x')) return 0; if (d == 2 && (i == 0 || G[i-1][j] == 'x')) return 0; if (d == 3 && (j == 0 || G[i][j-1] == 'x')) return 0; return 1; } /* 2 3 4 ... .2. xxx .1. */ pair<int,int> move(int i, int j, int d){ int oi = i,oj = j,od = d; while (1){ if (ma[i][j][d].first != -1) { return ma[oi][oj][od] = ma[i][j][d]; } if (G[i][j] == 'A') { d++; if (d == 4) d = 0; } if (G[i][j] == 'C'){ d--; if (d == -1) d = 3; } if (!legal(i,j,d)) return ma[oi][oj][od] = {i,j}; if (d == 0) i++; if (d == 1) j++; if (d == 2) i--; if (d == 3) j--; } } void calc(int i, int j, int l){ queue<pair<pair<int,int>,int>> q; q.push({{i,j},0}); while (!q.empty()){ auto [t,tt] = q.front();q.pop(); if (dist[l][t.first][t.second] < tt) continue; dist[l][t.first][t.second] = tt; for (int ii = 0;ii<4;ii++)q.push({{ma[t.first][t.second][ii].first,ma[t.first][t.second][ii].second},tt+1}); } } int main(){ cin.tie(0)->sync_with_stdio(0); cin >> N >> W >> H; G.resize(H); ma.resize(H,vector<vector<pair<int,int>>>(W,vector<pair<int,int>>(4,{-1,-1}))); dist.resize(N+1,vector<vector<int>>(H,vector<int>(W,INF))); for (int i = 0;i<H;i++) cin >> G[i]; for (int i = 0;i<H;i++){ for (int j = 0;j<W;j++){ move(i,j,0); move(i,j,1); move(i,j,2); move(i,j,3); } } for (int l = 1;l<=N;l++){ for (int i = 0;i<H;i++){ for (int j = 0;j<W;j++){ if (G[i][j] == l+'0') calc(i,j,l); } } } if (N == 2){ int ans = INF; for (int i = 0;i<H;i++){ for (int j = 0;j<W;j++){ ans = min(ans,dist[1][i][j]+dist[2][i][j]); } } if (ans == INF) ans = -1; cout << ans; return 0; } else cout << 5; 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...