Advertisement
Guest User

pereti

a guest
Jan 23rd, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int a[105][105], b[105][105], n, m;
  6.  
  7. void Citire()
  8. {
  9. ifstream fin("pereti.in");
  10. fin >> n >> m;
  11. for (int i = 1; i <= n; i++)
  12. for (int j = 1; j <= m; j++)
  13. fin >> a[i][j];
  14. fin.close();
  15. }
  16.  
  17. inline int Interior(int i, int j)
  18. {
  19. if (i < 0 || i > n + 1) return 0;
  20. if (j < 0 || j > m + 1) return 0;
  21. return 1;
  22. }
  23.  
  24. void Fill(int i, int j)
  25. {
  26. b[i][j] = 1;
  27. if (Interior(i - 1, j) && b[i - 1][j] == 0 && ((a[i][j] & 8) == 0))
  28. Fill(i - 1, j);
  29. if (Interior(i + 1, j) && b[i + 1][j] == 0 && ((a[i][j] & 2) == 0))
  30. Fill(i + 1, j);
  31. if (Interior(i, j + 1) && b[i][j + 1] == 0 && ((a[i][j] & 4) == 0))
  32. Fill(i, j + 1);
  33. if (Interior(i, j - 1) && b[i][j - 1] == 0 && ((a[i][j] & 1) == 0))
  34. Fill(i, j - 1);
  35. }
  36.  
  37. void Bordare()
  38. {
  39. int i, j;
  40. /// linia 0:
  41. for (j = 1; j <= m; j++)
  42. if ((a[1][j] & 8) != 0) a[0][j] = 2;
  43. /// linia n + 1
  44. for (j = 1; j <= m; j++)
  45. if ((a[n][j] & 2) != 0) a[n + 1][j] = 8;
  46. /// coloana 0
  47. for (i = 1; i <= n; i++)
  48. if ((a[i][1] & 1) != 0) a[i][0] = 4;
  49. for (i = 1; i <= n; i++)
  50. if ((a[i][m] & 4) != 0) a[i][m + 1] = 1;
  51. }
  52.  
  53. void Rezolva()
  54. {
  55. int i, j, cnt = 0;
  56. Fill(0, 0);
  57. for (i = 0; i <= n + 1; i++)
  58. for (j = 0; j <= m + 1; j++)
  59. if (b[i][j] == 1)
  60. {
  61. if ((a[i][j] & 8) != 0) cnt++;
  62. if ((a[i][j] & 4) != 0) cnt++;
  63. if ((a[i][j] & 2) != 0) cnt++;
  64. if ((a[i][j] & 1) != 0) cnt++;
  65. }
  66. ofstream fout("pereti.out");
  67. fout << cnt << "\n";
  68. fout.close();
  69. }
  70.  
  71. int main()
  72. {
  73. Citire();
  74. Bordare();
  75. Rezolva();
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement