fork download
  1. #include <iostream>
  2.  
  3. int i, j, k, A[1000][1000], B[1000][1000], N, M;
  4.  
  5. int F(int x, int y) {
  6. if (x == 0 && y == 0 && (A[x][y] == 2 || A[x][y] == 3) ||
  7. x == 0 && y == M-1 && (A[x][y] == 1 || A[x][y] == 2) ||
  8. x == N-1 && y == M-1 && (A[x][y] == 1 || A[x][y] == 4) ||
  9. x == N-1 && y == 0 && (A[x][y] == 3 || A[x][y] == 4) ||
  10. x == 0 && A[x][y] == 2 ||
  11. x == N-1 && A[x][y] == 4 ||
  12. y == 0 && A[x][y] == 3 ||
  13. y == M-1 && A[x][y] == 1) return 0;
  14. B[x][y] = 1;
  15. if (A[x][y] == 1) {
  16. if (B[x][y+1] == 1) return 1;
  17. else return F(x, y+1);
  18. }
  19. if (A[x][y] == 2) {
  20. if (B[x-1][y] == 1) return 1;
  21. else return F(x-1, y);
  22. }
  23. if (A[x][y] == 3) {
  24. if (B[x][y-1] == 1) return 1;
  25. else return F(x, y-1);
  26. }
  27. if (A[x][y] == 4) {
  28. if (B[x+1][y] == 1) return 1;
  29. else return F(x+1, y);
  30. }
  31. return 0;
  32. }
  33.  
  34. int main() {
  35. std::cin >> N >> M;
  36. k = N*M;
  37. for (i=0; i<N; ++i)
  38. for (j=0; j<M; ++j)
  39. std::cin >> A[i][j];
  40.  
  41. for (k=i=0; i<N; ++i) {
  42. for (j=0; j<M; ++j) {
  43. k += F(i, j);
  44. for (int i=0; i<N; ++i) {
  45. for (int j=0; j<M; ++j) {
  46. B[i][j] = 0;
  47. }
  48. }
  49. }
  50. }
  51. std::cout << k;
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 5720KB
stdin
2 2
1 2
3 4
stdout
Standard output is empty