Advertisement
Josif_tepe

Untitled

Mar 9th, 2025
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5. typedef long long ll;
  6. const int maxn = 5000;
  7.  
  8. vector<int> graph[maxn];
  9.  
  10. int main() {
  11.     int n, m;
  12.     cin >> n >> m;
  13.    
  14.     char c[n][m];
  15.    
  16.    
  17.     int mat[n][m];
  18.     int idx = 0;
  19.    
  20.     for(int i = 0; i < n; i++) {
  21.         for(int j = 0; j < m; j++) {
  22.             cin >> c[i][j];
  23.             mat[i][j] = idx;
  24.             idx++;
  25.         }
  26.     }
  27.    
  28.     int di[] = {-1, 1, 0, 0};
  29.     int dj[] = {0, 0, -1, 1};
  30.     for(int i = 0; i < n; i++) {
  31.         for(int j = 0; j < m; j++) {
  32.             for(int k = 0; k < 4; k++) {
  33.                 int ti = i + di[k];
  34.                 int tj = j + dj[k];
  35.                
  36.                 if(ti >= 0 and ti < n and tj >= 0 and tj < m and c[i][j] == c[ti][tj]) {
  37.                     graph[mat[i][j]].push_back(mat[ti][tj]);
  38.                     graph[mat[ti][tj]].push_back(mat[i][j]);
  39.                    
  40.                 }
  41.             }
  42.         }
  43.     }
  44.    
  45.    
  46.    
  47.     return 0;
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement