fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int n, dy, dx;
  7.  
  8. bool findway(vector<vector<int>> arr, int sy, int sx, int top = 0, int bot = 0, int left = 0, int right = 0)
  9. {
  10. if (sy == (dy - 1) && sx == (dx - 1)) return true;
  11. // left
  12. if (sx >= 1 && arr[sy][sx - 1] == 0 && left == 0) {
  13. if (findway(arr, sy, sx - 1, 0, 0, 0, 1))
  14. return true;
  15. }
  16. // right
  17. if (sx < n - 1 && arr[sy][sx + 1] == 0 && right == 0) {
  18. if (findway(arr, sy, sx + 1, 0, 0, 1, 0))
  19. return true;
  20. }
  21. // top
  22. if (sy >= 1 && arr[sy - 1][sx] == 0 && top == 0) {
  23. if (findway(arr, sy - 1, sx, 0, 1, 0, 0))
  24. return true;
  25. }
  26. // bottom
  27. if (sy < n - 1 && arr[sy + 1][sx] == 0 && bot == 0) {
  28. if (findway(arr, sy + 1, sx, 1, 0, 0, 0))
  29. return true;
  30. }
  31. return false;
  32. }
  33.  
  34. int main()
  35. {
  36. int sy, sx;
  37. cin >> n >> sy >> sx >> dy >> dx;
  38. vector<vector<int>> arr(n);
  39. for (int i = 0; i < n; ++i) {
  40. arr[i].resize(n);
  41. for (int j = 0; j < n; ++j)
  42. cin >> arr[i][j];
  43. }
  44. if (!findway(arr, sy - 1, sx - 1))
  45. cout << "NO";
  46. else cout << "YES";
  47. return 0;
  48. }
  49.  
Time limit exceeded #stdin #stdout 5s 7270400KB
stdin
5 1 1 5 5
1 0 0 0 0
1 1 1 0 0
0 0 0 0 1
0 1 1 1 1
0 0 0 0 0
stdout
Standard output is empty