orazma

c.cpp

May 2nd, 2026
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define pb push_back
  5. #define mp make_pair
  6. #define ll long long
  7.  
  8. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  9.  
  10. using namespace std;
  11. const int INF = 1e9 + 1;
  12. const int MOD = 1e9 + 7;
  13. const int MAXN = 1e3 + 10;
  14. char field[MAXN][MAXN];
  15. bool was[MAXN][MAXN];
  16. int dx[4] = {-1, 0, 0, 1};
  17. int dy[4] = {0, -1, 1, 0};
  18. queue <pair <int, int> > q[10000];
  19.  
  20. int n, m;
  21. bool check(int x, int y){
  22.     return (x >= 1 && x <= n && y >= 1 && y <= m);
  23. }
  24.  
  25. void solve(){
  26.  
  27.     cin >> n >> m;
  28.  
  29.     for (int i = 1; i <= n; i++){
  30.         for (int j = 1; j <= m; j++){
  31.             cin >> field[i][j];
  32.         }
  33.     }
  34.     q[0].push({1, 1});
  35.     int timer = 0;
  36.     while (!q[timer].empty()){
  37.  
  38.         while (!q[timer].empty()){
  39.             pair <int, int> z = q[timer].front();
  40.             q[timer].pop();
  41.             int vx = z.f;
  42.             int vy = z.s;
  43.             for (int i = 0; i < 4; i++){
  44.                 int tox = vx + dx[i];
  45.                 int toy = vy + dy[i];
  46.                 if (check(tox, toy) && !was[tox][toy] && field[vx][vy] == field[tox][toy]){
  47.                     q[timer].push({tox, toy});
  48.                     //dist[tox][toy] = dist[vx][vy];
  49.                     was[tox][toy] = 1;
  50.                 }
  51.                 if (check(tox, toy) && !was[tox][toy] && field[vx][vy] != field[tox][toy] && field[tox][toy] != '*'){
  52.                     q[timer + 1].push({tox, toy});
  53.                     //dist[tox][toy] = dist[vx][vy];
  54.                     was[tox][toy] = 1;
  55.                 }
  56.             }
  57.         }
  58.         timer++;
  59.     }
  60.     cout << timer;
  61. }
  62.  
  63. int main() {
  64.  
  65.     IOS;
  66.     int t = 1;
  67.     //cin >> t;
  68.     while(t--){
  69.         solve();
  70.     }
  71.     return 0;
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment