Guest User

Untitled

a guest
Jul 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <vector>
  7. #include <queue>
  8. #include <map>
  9. #include <deque>
  10. using namespace std;
  11.  
  12. ifstream in { "input.in" };
  13. ofstream out { "output.out" };
  14.  
  15. using Pair = pair<int, int>;
  16.  
  17. const int dx[] = { -1, 0, 1, 0 };
  18. const int dy[] = { 0, 1, 0, -1 };
  19.  
  20. int n;
  21. Pair start, stop;
  22. bool a[200][200];
  23.  
  24. void citeste() {
  25. int m;
  26. in >> n >> m;
  27.  
  28. while (m--) {
  29. int i, j;
  30. in >> i >> j;
  31.  
  32. a[i][j] = true;
  33. }
  34.  
  35. in >> start.first >> start.second >> stop.first >> stop.second;
  36. --start.first;
  37. --start.second;
  38. --stop.first;
  39. --stop.second;
  40. }
  41.  
  42. int Lee() {
  43. int d[200][200] = {};
  44. d[start.first][start.second] = 1;
  45. a[start.first][start.second] = true;
  46.  
  47. queue<Pair> q;
  48. q.push(start);
  49. while (!q.empty()) {
  50. Pair p = q.front();
  51. q.pop();
  52.  
  53. for (int k { 0 }; k < 4; ++k) {
  54. int i { p.first + dx[k] };
  55. int j { p.second + dy[k] };
  56.  
  57. if (i >= 0 && j >= 0 && i < n && j < n && !a[i][j] && !d[i][j]) {
  58. d[i][j] = d[p.first][p.second] + 1;
  59. if (i == stop.first && j == stop.second)
  60. return d[i][j];
  61.  
  62. q.emplace(i, j);
  63. }
  64. }
  65. }
  66.  
  67. return d[stop.first][stop.second];
  68. }
  69.  
  70. int main() {
  71.  
  72. citeste();
  73. out << Lee();
  74. }
Add Comment
Please, Sign In to add comment