Submission #254401

#TimeUsernameProblemLanguageResultExecution timeMemory
254401oscarsierra12Mecho (IOI09_mecho)C++14
73 / 100
242 ms12408 KiB
#include<bits/stdc++.h> using namespace std ; const int N = 1000 ; string ma[N] ; int dist[N][N], dist2[N][N] ; int X [] = {0,0,-1,1}, Y[] = {1,-1,0,0} ; int visi[N][N] ; int main() { int n, s ; cin >> n >> s ; for ( int i = 0 ; i < n ; ++i ) cin >> ma[i] ; queue <pair<int,int>> bfs ; pair<int,int> bg, en ; for ( int i = 0 ; i < n ; ++i ) { for ( int j = 0 ; j < n ; ++j ) { if ( ma[i][j] == 'H' ) bfs.push({i,j}), dist2[i][j] = 0, visi[i][j] = 1 ; if ( ma[i][j] == 'T' ) visi[i][j] = 1 ; if ( ma[i][j] == 'M' ) bg = {i,j} ; if ( ma[i][j] == 'D' ) en = {i,j} ; } } while ( bfs.size() ) { pair<int,int> cur = bfs.front() ; bfs.pop() ; for ( int k = 0 ; k < 4 ; ++k ) { int nwx = X[k] + cur.first ; int nwy = Y[k] + cur.second ; if ( nwx < 0 || nwy < 0 || nwx >= n || nwy >= n || visi[nwx][nwy] ) continue ; visi[nwx][nwy] = 1 ; dist2[nwx][nwy] = 1 + dist2[cur.first][cur.second] ; bfs.push({nwx, nwy}) ; } } int lw = 0, hg = n*n + 5 ; while ( lw <= hg ) { int mid = (lw+hg) / 2 ; memset ( visi, 0, sizeof visi ) ; memset ( dist, 0, sizeof dist ) ; bfs.push(bg) ; visi[bg.first][bg.second] = 1 ; while ( bfs.size() ) { pair<int,int> cur = bfs.front() ; bfs.pop() ; for ( int k = 0 ; k < 4 ; ++k ) { int nwx = X[k] + cur.first ; int nwy = Y[k] + cur.second ; if ( nwx < 0 || nwy < 0 || nwx >= n || nwy >= n || visi[nwx][nwy] || ma[nwx][nwy] == 'T' ) continue ; if ( (1 + dist[cur.first][cur.second]) / s + mid >= dist2[nwx][nwy] ) continue ; visi[nwx][nwy] = 1 ; dist[nwx][nwy] = 1 + dist[cur.first][cur.second] ; bfs.push({nwx, nwy}) ; } } if ( visi[en.first][en.second] ) lw = mid+1 ; else hg = mid-1 ; } cout << hg << endl ; }
#Verdict Execution timeMemoryGrader output
Fetching results...