Submission #636906

#TimeUsernameProblemLanguageResultExecution timeMemory
636906ojoConmigoMecho (IOI09_mecho)C++17
23 / 100
1100 ms27928 KiB
#include <bits/stdc++.h> using namespace std; int n,s; vector<vector<char>> grid; vector<pair<int,int>> hives; pair<int,int> root,obj; vector<vector<int>> mapa; vector<vector<bool>> visit; vector<vector<int>> dp; int sx[4] = {0,0,1,-1}; int sy[4] = {1,-1,0,0}; bool ok(int x, int y){ return x < n && y < n && x >= 0 && y >= 0; } void bfs(){ queue<pair<int,int>> cola; for(auto h : hives){ cola.push(h); } while(cola.size()){ auto q = cola.front(); cola.pop(); int x = q.first; int y = q.second; for(int i=0; i<4; i++){ int nx = x + sx[i]; int ny = y + sy[i]; if(ok(nx,ny)){ if(mapa[nx][ny] != -1)continue; if(grid[nx][ny] == 'G' || grid[nx][ny] == 'M'){ mapa[nx][ny] = mapa[x][y]+1; cola.push({nx,ny}); } } } } } /* bool dfs(int x, int y,int k){ if(x == obj.first && y == obj.second)return true; visit[x][y] = true; bool b = false; for(int i=0; i<4; i++){ int nx = x+sx[i]; int ny = y+sy[i]; if(ok(nx,ny)){ if(grid[nx][ny] == 'G' || grid[nx][ny] == 'D'){ if(visit[nx][ny]) continue; if(mapa[nx][ny] > k){ b = max(b,dfs(nx,ny,k)); } } } } return b; } */ int dfs(int x, int y,int cost, int steps){ //cout << x << " " << y << " " << cost << " " << steps << endl; //cout << dp[x][y] << endl; if(x == obj.first && y == obj.second) return 1e9; if(mapa[x][y] - cost < 0) return -1; if(dp[x][y] >= mapa[x][y] - cost) return -1; dp[x][y] = mapa[x][y] - cost; if(steps == s){ steps = 0; cost++; } int sol = -1; for(int i=0; i<n; i++){ int nx = x+sx[i]; int ny = y+sy[i]; if(ok(nx,ny)){ if(grid[nx][ny] == 'G' || grid[nx][ny] == 'D'){ int k = dfs(nx,ny,cost,steps+1); sol = max(sol,k); //cout << nx << " " << ny << " k: " << k << " sol: " << sol << endl; } } } //cout << x << " " << y << " " << cost << " sol: " << sol << endl; if(sol != -1) return min(sol,dp[x][y]); return -1; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n >> s; grid.assign(n,vector<char> (n)); mapa.assign(n,vector<int>(n,-1)); for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ cin >> grid[i][j]; if(grid[i][j] == 'H'){ hives.push_back({i,j}); mapa[i][j] = 0; } if(grid[i][j] == 'M'){ root = {i,j}; } if(grid[i][j] == 'D'){ obj = {i,j}; } } } bfs(); /* for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ cout << mapa[i][j] << " "; } cout << endl; } */ dp.assign(n,vector<int>(n,-1)); cout << dfs(root.first,root.second,1,0) << endl; return 0; /* int lo = 1, hi = n*n, res = -1; while(lo <= hi){ int mid = (lo+hi)/2; int k = dfs(root.first,root.second,s*mid); if(k - mid > res){ res = mid; lo = mid+1; }else hi = mid-1; } cout << res << endl; */ }
#Verdict Execution timeMemoryGrader output
Fetching results...