Advertisement
Nita_Cristian

ferma

Feb 26th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("ferma.in");
  6. ofstream fout("ferma.out");
  7.  
  8. int n, m, v, a[450][450], zone_totale, zona_curenta = 1;
  9. char caractere[450][450];
  10. int di[4] = {-1, 0, 1, 0}, dj[4] = {0, 1, 0, -1};
  11. map<int, int> mapa;
  12.  
  13. void fil(int i, int j, char caracter_curent, int zona, int cul)
  14. {
  15.     int cate = 1;
  16.     queue<pair<int, int>> q;
  17.     q.push({i,j});
  18.  
  19.     caractere[i][j] = '-';
  20.     a[i][j] = cul;
  21.     while(!q.empty())
  22.     {
  23.         i = q.front().first;
  24.         j = q.front().second;
  25.         q.pop();
  26.  
  27.         for(int k = 0; k < 4; k++)
  28.         {
  29.             int ni = i + di[k], nj = j + dj[k];
  30.             if(caractere[ni][nj] == caracter_curent)
  31.             {
  32.                 caractere[ni][nj] = '-';
  33.                 a[ni][nj] = cul;
  34.                 q.push({ni, nj});
  35.                 cate++;
  36.             }
  37.         }
  38.     }
  39.     mapa[zona] = cate;
  40. }
  41.  
  42. int main()
  43. {
  44.     fin >> v >> n >> m;
  45.     for(int i = 1; i <= n; i++)
  46.         for(int j = 1; j <= m; j++)
  47.             fin >> caractere[i][j];
  48.  
  49.     for(int i = 1; i <= n; i++)
  50.         for(int j = 1; j <= m; j++)
  51.             if(caractere[i][j] != '-')
  52.                 fil(i, j, caractere[i][j], zona_curenta, caractere[i][j] - 96), zone_totale++, zona_curenta++;
  53.  
  54.  
  55.     if(v == 1)
  56.     {
  57.         fout << zone_totale << '\n';
  58.     }
  59.     else
  60.     {
  61.     }
  62.  
  63.     for(int i = 1; i <= n; i++)
  64.     {
  65.         for(int j = 1; j <= m; j++)
  66.             fout << setw(2)<< a[i][j] << ' ';
  67.         fout << '\n';
  68.     }
  69.  
  70.     fin.close();
  71.     fout.close();
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement