AlexNeagu11

Fill

Feb 4th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include<fstream>
  2. using namespace std;
  3.  
  4. int a[106][106];
  5. bool vizitat[106][106];
  6. int n, m;
  7. ifstream in("fill.in");
  8. ofstream out("fill.out");
  9.  
  10. // vizitat[i][j] = 1 -> deja "umplut" celula (i,j)
  11. // vizitat[i][j] = 0 -> inca nu am "umplut" celula (i,j)
  12. void umple(int x, int y) {
  13. // ne aflam acum la celula (x,y)
  14. // efectuam procedeul dorit pe ea : in cazul nostru o marcam ca vizitata
  15. vizitat[x][y] = 1;
  16. // acum "propagam" functia peste vecinii lui (x,y) (daca se afla in matrice, nu i-am umplut deja si sunt zone de uscat)
  17. if(x + 1 < n && !vizitat[x + 1][y] && a[x + 1][y] == 1) umple(x + 1, y);
  18. if(x - 1 >= 0 && !vizitat[x - 1][y] && a[x - 1][y] == 1) umple(x - 1, y);
  19. if(y + 1 < m && !vizitat[x][y + 1] && a[x][y + 1] == 1) umple(x, y + 1);
  20. if(y - 1 >= 0 && !vizitat[x][y - 1] && a[x][y - 1] == 1) umple(x, y - 1);
  21. // aici se incheie functia
  22. }
  23. int main() {
  24.  
  25. in >> n >> m;
  26. for(int i = 0; i < n; ++i) {
  27. for(int j = 0; j < m; ++j) {
  28. in >> a[i][j];
  29. }
  30. }
  31. int answer = 0;
  32. for(int i = 0; i < n; ++i) {
  33. for(int j = 0; j < m; ++j) {
  34. if(!vizitat[i][j] && a[i][j] == 1) {
  35. ++answer;
  36. umple(i, j);
  37. }
  38. }
  39. }
  40. out << answer << '\n';
  41. }
  42.  
  43.  
  44.  
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment