AlexNeagu11

Lac

Feb 4th, 2022
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include<fstream>
  2. using namespace std;
  3.  
  4. ifstream in("lac.in");
  5. ofstream out("lac.out");
  6.  
  7. int n, m;
  8. int dx[4] = {0, 0, -1, 1}; // directii pe axa x
  9. int dy[4] = {1, -1, 0, 0}; // directii pe axa y
  10. int answer = 0;
  11. int a[1006][1006];
  12. bool vizitat[1006][1006];
  13.  
  14. void umple(int x, int y) {
  15. vizitat[x][y] = 1;
  16. // marcam celula curenta ca vizitata
  17. for(int d = 0; d < 4; ++d) {
  18. int nx = x + dx[d];
  19. int ny = y + dy[d];
  20. // (nx, ny) noua celula
  21. if(nx >= 0 && ny >= 0 && nx < n && ny < m && vizitat[nx][ny] == 0 && a[nx][ny] == 1) {
  22. umple(nx, ny);
  23. }
  24. }
  25. }
  26.  
  27. int main() {
  28. in >> n >> m;
  29. for(int i = 0; i < n; ++i) {
  30. for(int j = 0; j < m; ++j) {
  31. in >> a[i][j];
  32. }
  33. }
  34.  
  35. // incercam sa efectuam "umplerea" incepand cu o celula de pe margine
  36. // in acest pas al algoritmul vom "umple" peninsulele
  37.  
  38. int peninsule = 0;
  39.  
  40. for(int i = 0; i < n; ++i) {
  41. for(int j = 0; j < m; ++j) {
  42. if( (i == 0 || j == 0 || i == n - 1 || j == m - 1) && a[i][j] == 1 && vizitat[i][j] == 0) {
  43. umple(i, j);
  44. ++peninsule;
  45. }
  46. }
  47. }
  48.  
  49. int insule = 0;
  50. for(int i = 1; i < n - 1; ++i) {
  51. for(int j = 1; j < m - 1; ++j) {
  52. if(a[i][j] == 1 && vizitat[i][j] == 0) {
  53. umple(i, j);
  54. ++insule;
  55. }
  56. }
  57. }
  58.  
  59. out << insule << ' ' << peninsule << '\n';
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment