egogoboy

Вирусы

Jul 15th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. int main() {
  9.  
  10.     std::ifstream fin("input.txt");
  11.     std::ofstream fout("output.txt");
  12.  
  13.     short n, m, k;
  14.     fin >> n >> m >> k;
  15.    
  16.     vector<vector<bool>> map(n, vector<bool> (m, false));
  17.    
  18.     queue<int> qi, qj;
  19.     int vi, vj;
  20.     vector<int> di = { 0, 0, 1, -1 };
  21.     vector<int> dj = { 1, -1, 0, 0 };
  22.     int max = 0;
  23.     for (int i = 0; i < k; ++i) {
  24.         fin >> vi >> vj;
  25.         vi--; vj--;
  26.         qi.push(vi);
  27.         qj.push(vj);
  28.         map[vi][vj] = true;
  29.     }
  30.  
  31.     int r = k, sr = 0;
  32.         while (!qi.empty()) {
  33.  
  34.             vi = qi.front();
  35.             vj = qj.front();
  36.             qi.pop();
  37.             qj.pop();
  38.  
  39.             for (int i = 0; i < 4; ++i) {
  40.                 vi += di[i];
  41.                 vj += dj[i];
  42.                 if (vi >= 0 && vi < n && vj >= 0 && vj < m) {
  43.                     if (map[vi][vj] == false) {
  44.                         qi.push(vi);
  45.                         qj.push(vj);
  46.                         map[vi][vj] = true;
  47.                         sr++;
  48.                     }
  49.                 }
  50.                 vi -= di[i];
  51.                 vj -= dj[i];
  52.             }
  53.             r--;
  54.             if (r == 0) {
  55.                 max++;
  56.                 swap(r, sr);
  57.             }
  58.         }
  59.     fout << max;
  60.  
  61.     return 0;
  62.  
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment