Advertisement
Josif_tepe

Untitled

Oct 24th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5. typedef long long ll;
  6.  
  7. int main()
  8. {
  9.     int n, m;
  10.     cin >> n >> m;
  11.     int mat[n][m];
  12.     for(int i = 0; i < n; i++) {
  13.         for(int j = 0; j < m; j++) {
  14.             mat[i][j] = -1;
  15.         }
  16.     }
  17.     int R, K;
  18.     cin >> R >> K;
  19.     R--;
  20.     K--;
  21.     queue<int> Q;
  22.     Q.push(R);
  23.     Q.push(K);
  24.     Q.push(0);
  25.     mat[R][K] = 0;
  26.     int biggest_number = 0;
  27.     while(!Q.empty()) {
  28.         int ci = Q.front();
  29.         Q.pop();
  30.         int cj = Q.front();
  31.         Q.pop();
  32.         int number = Q.front();
  33.         Q.pop();
  34.         biggest_number = max(biggest_number, number);
  35.         if(ci + 1 < n and mat[ci + 1][cj] == -1) {
  36.             Q.push(ci + 1);
  37.             Q.push(cj);
  38.             Q.push(number + 1);
  39.             mat[ci + 1][cj] = number + 1;
  40.         }
  41.         if(ci - 1 >= 0 and mat[ci - 1][cj] == -1) {
  42.             Q.push(ci - 1);
  43.             Q.push(cj);
  44.             Q.push(number + 1);
  45.             mat[ci - 1][cj] = number + 1;
  46.         }
  47.         if(cj + 1 < m and mat[ci][cj + 1] == -1) {
  48.             Q.push(ci);
  49.             Q.push(cj + 1);
  50.             Q.push(number + 1);
  51.             mat[ci][cj + 1] = number + 1;
  52.         }
  53.         if(cj - 1 >= 0 and mat[ci][cj - 1] == -1) {
  54.             Q.push(ci);
  55.             Q.push(cj - 1);
  56.             Q.push(number + 1);
  57.             mat[ci][cj - 1] = number + 1;
  58.         }
  59.     }
  60.     int chocolates = 0;
  61.    
  62.     for(int i = 0; i < n; i++) {
  63.         for(int j = 0; j < m; j++) {
  64.             if(mat[i][j] == biggest_number) {
  65.                 chocolates++;
  66.             }
  67.         }
  68.        
  69.     }
  70.     cout << biggest_number << endl;
  71.     cout << chocolates << endl;
  72.     return 0;
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement