a53

Jaina

a53
May 17th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("jaina.in");
  7. ofstream fout("jaina.out");
  8.  
  9. #define MaxN 101
  10. #define INF 0x3f3f3f3f
  11.  
  12. int n;
  13. int ip, jp, is, js;
  14. int a[MaxN][MaxN];
  15. int m;
  16. int x, y;
  17.  
  18. struct Coord
  19. {
  20. int x, y;
  21. } source[MaxN][MaxN];
  22.  
  23. const int di[] = { -1, -1, -1, 0, 1, 1, 1, 0 };
  24. const int dj[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
  25.  
  26. queue<pair<int, int> >Q;
  27.  
  28. void Read();
  29. void Laser_fill(int, int);
  30. void Init();
  31. void Lee();
  32. bool Ok(int, int);
  33.  
  34. int main()
  35. {
  36. Read();
  37. Lee();
  38. fout << a[is][js] << '\n';
  39.  
  40. fin.close();
  41. fout.close();
  42. return 0;
  43. }
  44.  
  45. void Read()
  46. {
  47. fin >> n;
  48.  
  49. Init();
  50.  
  51. fin >> ip >> jp >> is >> js;
  52.  
  53. fin >> m;
  54.  
  55. for (int i = 1; i <= m; ++i)
  56. {
  57. fin >> x >> y;
  58. Laser_fill(x, y);
  59. }
  60. }
  61.  
  62. void Laser_fill(int x, int y)
  63. {
  64. for (int i = 1; i <= n; ++i)
  65. {
  66. a[x][i] = -1;
  67.  
  68. if (!source[x][i].x)
  69. source[x][i].x = INF;
  70.  
  71. if ( source[x][i].x > x )
  72. source[x][i].x = x;
  73. source[x][i].y = y;
  74.  
  75. a[i][y] = -1;
  76.  
  77. if (!source[i][y].x)
  78. source[i][y].x = INF;
  79.  
  80. if ( source[i][y].x > x )
  81. source[i][y].x = x;
  82. source[i][y].y = y;
  83.  
  84. }
  85.  
  86. a[x][y] = INF;
  87. }
  88.  
  89. void Init()
  90. {
  91. for (int i = 1; i <= n; ++i)
  92. for (int j = 1; j <= n; ++j)
  93. a[i][j] = INF;
  94. }
  95.  
  96.  
  97. void Lee()
  98. {
  99. Q.push({ ip,jp });
  100. a[ip][jp] = 0;
  101.  
  102. while (!Q.empty())
  103. {
  104. int i = Q.front().first;
  105. int j = Q.front().second;
  106. Q.pop();
  107.  
  108. for (int d = 0; d < 8; ++d)
  109. {
  110. int iv = i + di[d];
  111. int jv = j + dj[d];
  112.  
  113. int x_sursa = source[iv][jv].x;
  114. int y_sursa = source[iv][jv].y;
  115.  
  116. if (a[iv][jv] == -1 && a[x_sursa][y_sursa] > a[i][j] + 1)
  117. {
  118. a[x_sursa][y_sursa] = a[i][j] + 1;
  119. Q.push({ x_sursa, y_sursa });
  120. }
  121. else
  122.  
  123. if (Ok(iv, jv) && a[iv][jv] > a[i][j] + 1)
  124. {
  125. a[iv][jv] = a[i][j] + 1;
  126. Q.push({ iv,jv });
  127. }
  128.  
  129. }
  130. }
  131. }
  132.  
  133. bool Ok(int i, int j)
  134. {
  135. if (i < 1 || i > n || j < 1 || j > n)
  136. return false;
  137. if (a[i][j] == -1)
  138. return false;
  139. return true;
  140. }
Add Comment
Please, Sign In to add comment