Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int a[100][100],m;
  5. //functia asta o sa furnizeze prin i si j pozitia primul element nenul
  6. int posofNext(int &i,int &j)
  7. {
  8.     for(i=0;i<m;i++)
  9.         for(j=0;j<m;j++)
  10.             if(a[i][j]==1)
  11.                 return 0;
  12.     return -1;//cod eroare
  13. }
  14.  
  15. void etichetare(int i,int j,int eticheta)
  16. {
  17.     if(a[i][j]==0||a[i][j]==eticheta)
  18.         return;
  19.     a[i][j]=eticheta;
  20.     //etichetam si nodurile invecinate
  21.     etichetare(i+1,j,eticheta);
  22.     etichetare(i-1,j,eticheta);
  23.     etichetare(i,j-1,eticheta);
  24.     etichetare(i,j+1,eticheta);
  25. }
  26.  
  27.  
  28. int main() {
  29.     cin>>m;
  30.     for(int k=0;k<m;k++)
  31.         for(int l=0;l<m;l++)
  32.             cin>>a[k][l];
  33.     int i,j,eticheta=2;
  34.     while(posofNext(i,j)!=-1)
  35.         {
  36.             etichetare(i,j,eticheta++);
  37.         }
  38.  
  39.     //afisare
  40.     for(int k=0;k<m;k++){
  41.         for(int l=0;l<m;l++)
  42.             cout<<a[k][l]<<' ';
  43.         cout<<endl;
  44.     }
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement