제출 #1100760

#제출 시각아이디문제언어결과실행 시간메모리
1100760Uniq0rnMecho (IOI09_mecho)C++14
100 / 100
177 ms6604 KiB
#include <bits/stdc++.h> using namespace std; #define ld long double #define ll long long int #define pi pair<int,int> #define sz(x) (int)x.size() #define all(x) begin(x),end(x) int main() { cin.tie(0)->sync_with_stdio(0); int n,s;cin >> n >> s; vector<vector<char>> a(n + 1,vector<char>(n + 1)); queue<pair<int,int>> qu; vector<vector<int>> bdist(n + 1,vector<int>(n + 1)); vector<vector<bool>> bvis(n + 1,vector<bool>(n + 1)); int si = -1,sj = -1,ei = -1,ej = -1; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin >> a[i][j]; if(a[i][j] == 'H'){ bvis[i][j] = true; qu.push({i,j}); } if(a[i][j] == 'M'){ si = i; sj = j; } if(a[i][j] == 'D'){ ei = i; ej = j; } } } while(sz(qu)){ int ni = qu.front().first,nj = qu.front().second; //cout << ni << ' ' << nj << '\n'; qu.pop(); for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++){ if(abs(i) == abs(j)) continue; int ii = ni + i,jj = nj + j; if(ii < 1 || ii > n || jj < 1 || jj > n) continue; if(bvis[ii][jj] || a[ii][jj] == 'T' || a[ii][jj] == 'D') continue; bvis[ii][jj] = true; bdist[ii][jj] = bdist[ni][nj] + 1; qu.push({ii,jj}); } } /* for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout << (bdist[i][j] == 1e9 ? -1 : bdist[i][j]) << ' '; } cout << '\n'; } cout << "---------------------------------------------\n"; */ int ans = -1; int l = 0,r = n * n; while(l <= r){ int mid = (l + r) >> 1; if(bdist[si][sj] <= mid){ r = mid - 1; continue; } //cout << mid << '\n'; vector<vector<int>> dist(n + 1,vector<int>(n + 1)); vector<vector<bool>> vis(n + 1,vector<bool>(n + 1)); queue<pair<int,int>> q; q.push({si,sj}); vis[si][sj] = true; bool ok = false; while(sz(q)){ int ni = q.front().first,nj = q.front().second; q.pop(); for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++){ if(abs(i) == abs(j)) continue; int ii = ni + i,jj = nj + j; if(ii < 1 || ii > n || jj < 1 || jj > n) continue; if(vis[ii][jj]) continue; if(a[ii][jj] == 'T' || a[ii][jj] == 'H') continue; if(bdist[ii][jj] <= mid + (dist[ni][nj] + 1) / s) continue; vis[ii][jj] = true; dist[ii][jj] = dist[ni][nj] + 1; q.push({ii,jj}); } } for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++){ if(abs(i) == abs(j)) continue; int ii = ei + i,jj = ej + j; if(ii < 1 || ii > n || jj < 1 || jj > n) continue; if(a[ii][jj] == 'T' || a[ii][jj] == 'H') continue; if(!vis[ii][jj]) continue; if(dist[ii][jj] / s + mid >= bdist[ii][jj]) continue; ok = true; } /* for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cout << dist[i][j] << ' '; } cout << '\n'; } */ if(ok){ l = mid + 1; ans = mid; } else r = mid - 1; } cout << ans; /* for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cout << bdist[i][j] << ' '; } cout << '\n'; } */ return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...