Advertisement
ThegeekKnight16

Bagunça da Madalena

Jul 9th, 2022
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 5e2 + 10;
  4. int Sala[MAXN][MAXN];
  5. int dx[] = {-1,-1, 0, 1,1,1,0,-1};
  6. int dy[] = { 0,-1,-1,-1,0,1,1, 1};
  7.  
  8. bool OutOfRange(int x, int y, int L, int C)
  9. {
  10.     if (x < 1 || x > L) return true;
  11.     if (y < 1 || y > C) return true;
  12.     return false;
  13. }
  14.  
  15. int check_valid(int x, int y, int L, int C)
  16. {
  17.     int resp = 8;
  18.     for (int k = 0; k < 8; k++)
  19.     {
  20.         int hx = x + dx[k];
  21.         int hy = y + dy[k];
  22.         resp -= OutOfRange(hx, hy, L, C);
  23.     }
  24.     return resp;
  25. }
  26.  
  27. void brincadeira(int x, int y, int L, int C)
  28. {
  29.     int tempo = 1;
  30.     while (tempo < L+C+1)
  31.     {
  32.         int validos = check_valid(x, y, L, C);
  33.         int fora = Sala[x][y] / validos;
  34.         Sala[x][y] = Sala[x][y] % validos;
  35.        
  36.         for (int k = 0; k < 8; k++)
  37.         {
  38.             int hx = x + dx[k];
  39.             int hy = y + dy[k];
  40.             if (OutOfRange(hx, hy, L, C)) continue;
  41.             Sala[hx][hy] += fora;
  42.         }
  43.        
  44.         int nextX; int nextY; int quantNext = -1;
  45.         for (int k = 0; k < 8; k++)
  46.         {
  47.             int hx = x + dx[k];
  48.             int hy = y + dy[k];
  49.             if (OutOfRange(hx, hy, L, C)) continue;
  50.             if (quantNext < Sala[hx][hy])
  51.             {
  52.                 nextX = hx;
  53.                 nextY = hy;
  54.                 quantNext = Sala[hx][hy];
  55.             }
  56.         }
  57.        
  58.         x = nextX; y = nextY;
  59.         tempo++;
  60.     }
  61. }
  62.  
  63. int main()
  64. {
  65.     ios_base::sync_with_stdio(false);
  66.     cin.tie(NULL);
  67.     int L, C;
  68.     cin >> L >> C;
  69.     for (int i = 1; i <= L; i++)
  70.     {
  71.         for (int j = 1; j <= C; j++)
  72.         {
  73.             cin >> Sala[i][j];
  74.         }
  75.     }
  76.    
  77.     if (L == 1 && C == 1) {cout << Sala[1][1]; return 0;}
  78.    
  79.     int A, B;
  80.     cin >> A >> B;
  81.     brincadeira(A, B, L, C);
  82.     int Max = -1;
  83.     for (int i = 1; i <= L; i++)
  84.     {
  85.         for (int j = 1; j <= C; j++)
  86.         {
  87.             Max = max(Max, Sala[i][j]);
  88.         }
  89.     }
  90.     cout << Max;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement