fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define all(a) (a).begin(), (a).end()
  4. #define dbg_line(x) cout << (x) << '\n'
  5. #define dbg(x) cout << x << " "
  6. #define len(x) (int)x.length()
  7.  
  8. using namespace std;
  9.  
  10. // <--> Report constants <-->
  11.  
  12. typedef pair<int, int> pii;
  13. const int max_n = 1e5 + 5;
  14. const ll inf = 1e9;
  15. const ll m_inf = -1e9;
  16. const ll mod = 1e9 + 7;
  17. const int base = 32;
  18.  
  19. // <--> Report variables <-->
  20.  
  21. int n, m, p, q, s, t;
  22. bool adj[1005][1005];
  23. set<pair<int, int>> visited;
  24. int dx[4] = {-1, -1, 1, 1};
  25. int dy[4] = {-1, 1, -1, 1};
  26.  
  27. // <--> Main Code is Here <-->
  28.  
  29. void setIO() {
  30. ios_base::sync_with_stdio(false);
  31. cin.tie(NULL);
  32. cout.tie(NULL);
  33. }
  34.  
  35. void call_file() {
  36. freopen("input.txt", "r", stdin);
  37. freopen("output.txt", "w", stdout);
  38. }
  39.  
  40. struct Distance{
  41. int x, y;
  42. int d;
  43. };
  44.  
  45. bool check(int i, int j){
  46. return i >=1 && i <= n && j >= 1 && j <= n && !adj[i][j];
  47. }
  48.  
  49. int BFS(){
  50. queue<Distance> Q;
  51. visited.insert({p, q});
  52. Q.push({p, q, 0});
  53. while (!Q.empty()){
  54. Distance u = Q.front();
  55. Q.pop();
  56. for (int i = 0; i < 4; i++){
  57. int x = u.x, y = u.y;
  58. while (check(x + dx[i], y + dy[i])){
  59. x += dx[i];
  60. y += dy[i];
  61. if (x == s && y == t){
  62. return u.d + 1;
  63. }
  64. if (visited.find({x, y}) == visited.end()){
  65. visited.insert({x, y});
  66. Q.push({x, y, u.d + 1});
  67. }
  68. }
  69. }
  70. }
  71. return -1;
  72. }
  73.  
  74. int main() {
  75. setIO();
  76. call_file();
  77. cin >> n >> m >> p >> q >> s >> t;
  78. while (m--){
  79. int x, y;
  80. cin >> x >> y;
  81. adj[x][y] = true;
  82. }
  83. cout << BFS();
  84.  
  85. }
  86.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
Standard output is empty