Advertisement
Alex_tz307

USACO 2019 January Contest, Silver Problem 2. Icy Perimeter

Apr 19th, 2021
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("perimeter.in");
  6. ofstream fout("perimeter.out");
  7.  
  8. const int NMAX = 1e3 + 3;
  9. const int dl[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1};
  10. char a[NMAX][NMAX];
  11. bool viz[NMAX][NMAX];
  12.  
  13. void solve() {
  14.   int N;
  15.   fin >> N;
  16.   for (int i = 1; i <= N; ++i)
  17.     fin >> (a[i] + 1);
  18.   int Amax = 0, Pmin = 0;
  19.   for (int i = 1; i <= N; ++i)
  20.     for (int j = 1; j <= N; ++j)
  21.       if (a[i][j] == '#' && !viz[i][j]) {
  22.         queue<pair<int,int>> Q;
  23.         Q.emplace(i, j);
  24.         viz[i][j] = true;
  25.         int A = 0, P = 0;
  26.         while (!Q.empty()) {
  27.           int l = Q.front().first, c = Q.front().second;
  28.           Q.pop();
  29.           ++A;
  30.           for (int k = 0; k < 4; ++k) {
  31.             int lv = l + dl[k], cv = c + dc[k];
  32.             if (a[lv][cv] != '#')
  33.               ++P;
  34.             else {
  35.               if (!viz[lv][cv] && lv > 0 && cv > 0 && lv <= N && cv <= N) {
  36.                 viz[lv][cv] = true;
  37.                 Q.emplace(lv, cv);
  38.               }
  39.             }
  40.           }
  41.         }
  42.         if (A > Amax || (A == Amax && P < Pmin)) {
  43.           Amax = A;
  44.           Pmin = P;
  45.         }
  46.       }
  47.   fout << Amax << ' ' << Pmin << '\n';
  48. }
  49.  
  50. void close_files() {
  51.   fin.close();
  52.   fout.close();
  53. }
  54.  
  55. int main() {
  56.   solve();
  57.   close_files();
  58.   return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement