Submission #977333

#TimeUsernameProblemLanguageResultExecution timeMemory
977333faricaMecho (IOI09_mecho)C++14
2 / 100
391 ms65540 KiB
#include <bits/stdc++.h> #define int long long using namespace std; const int MAX_N = 4e5 + 5; const int MOD = 998244353; void solve() { int n, s; cin >> n >> s; string grid[n]; int sx=0, sy=0; vector<vector<int>>depth(n, vector<int>(n, -1)); queue<pair<int,int>>q; for(int i=0; i<n; ++i) { cin >> grid[i]; for(int j=0; j<n; ++j) { if(grid[i][j] == 'M') { sx=i; sy=j; } else if(grid[i][j] == 'H') { q.push({i, j}); depth[i][j] = 0; } } } while(!q.empty()) { int x = q.front().first, y = q.front().second; q.pop(); if(x && grid[x-1][y] != 'T' && depth[x-1][y] == -1) { depth[x-1][y] = depth[x][y] + 1; q.push({x-1, y}); } if(y && grid[x][y-1] != 'T' && depth[x][y-1] == -1) { depth[x][y-1] = depth[x][y] + 1; q.push({x, y-1}); } if(x+1<n && grid[x+1][y] != 'T' && depth[x+1][y] == -1) { depth[x+1][y] = depth[x][y] + 1; q.push({x+1, y}); } if(y+1<n && grid[x][y+1] != 'T' && depth[x][y+1] == -1) { depth[x][y+1] = depth[x][y] + 1; q.push({x, y+1}); } } priority_queue<pair<pair<int,int>, pair<int,int>>>pq; pq.push({{depth[sx][sy], 0}, {sx, sy}}); while(!pq.empty()) { int t = pq.top().first.first, visited = pq.top().first.second, x = pq.top().second.first, y = pq.top().second.second; pq.pop(); if(grid[x][y] == 'D') { cout << t << endl; return; } int new_t, cost = (visited+1)/s + ((visited+1) % s > 0 ? 1 : 0); if(x && grid[x-1][y] != 'T') { new_t = min(t, depth[x-1][y] - cost); if(new_t >= 0) pq.push({{new_t, visited + 1}, {x-1, y}}); } if(y && grid[x][y-1] != 'T') { new_t = min(t, depth[x][y-1] - cost); if(new_t >= 0) pq.push({{new_t, visited + 1}, {x, y-1}}); } if(x+1<n && grid[x+1][y] != 'T') { new_t = min(t, depth[x+1][y] - cost); if(new_t >= 0) pq.push({{new_t, visited + 1}, {x+1, y}}); } if(y+1<n && grid[x][y+1] != 'T') { new_t = min(t, depth[x][y+1] - cost); if(new_t >= 0) pq.push({{new_t, visited + 1}, {x, y+1}}); } } cout << -1 << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t=1; while(t--) solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...