Source code for submission s597

g.cpp

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cctype>
  6. #include <string>
  7. #include <cstring>
  8. #include <cmath>
  9. #include <algorithm>
  10. #include <utility>
  11. #include <stack>
  12. #include <vector>
  13. #include <queue>
  14. #include <deque>
  15. #include <set>
  16. #include <map>
  17. #include <list>
  18.  
  19. #define SIZEOF(a) (sizeof(a)/sizeof(a[0]))
  20. #define FILL(a,b) fill(a,a+SIZEOF(a),b)
  21. #define FOR(a,b,c) for(int a=b;a<=c;a++)
  22. #define FORARR(i,a) for(unsigned i=0; i<SIZEOF(a); i++)
  23. #define FOREACH(a,b) for(__typeof((b).begin()) a = (b).begin(); a!=(b).end(); a++)
  24. #define GETI(a) scanf("%d ", &a);
  25. #define SWAP(a,b) { __typeof(a) t = a; a = t; b = t; }
  26.  
  27. using namespace std;
  28.  
  29. struct coords {
  30. int x,y;
  31. coords(int x, int y) : x(x),y(y) {}
  32. };
  33.  
  34.  
  35. int main(void)
  36. {
  37. int R,C;
  38. cin >> R >> C;
  39. int startX, startY;
  40. cin >> startX >> startY;
  41. int endX, endY;
  42. cin >> endX >> endY;
  43.  
  44. while (!feof(stdin)) {
  45.  
  46. vector<vector<int> > visited(max(R,C)+1, vector<int>(max(R,C)+1,-1));
  47. visited[startX][startY] = 0;
  48.  
  49. queue<coords> q;
  50. q.push(coords(startX, startY));
  51.  
  52. while (!q.empty()) {
  53. coords c = q.front();
  54. q.pop();
  55. // cerr << "at " << c.x << " " << c.y << endl;
  56. coords nbs[] = {
  57. coords(c.x-1, c.y-2),
  58. coords(c.x-1, c.y+2),
  59. coords(c.x+1, c.y-2),
  60. coords(c.x+1, c.y+2),
  61. coords(c.x-2, c.y-1),
  62. coords(c.x-2, c.y+1),
  63. coords(c.x+2, c.y-1),
  64. coords(c.x+2, c.y+1),
  65. };
  66. FORARR(i,nbs) {
  67. coords nb = nbs[i];
  68. // validate...
  69. if (nb.x < 1 || nb.x > R || nb.y < 1 || nb.y > C)
  70. continue; // bad
  71. if (visited[nb.x][nb.y] > 0) continue;
  72. visited[nb.x][nb.y] = visited[c.x][c.y] + 1;
  73. if (nb.x == endX && nb.y == endY) {
  74. // victory!
  75. cout << visited[nb.x][nb.y] << endl;
  76. goto out;
  77. }
  78. q.push(nb);
  79. }
  80.  
  81.  
  82. }
  83.  
  84. cout << "impossible" << endl;
  85. out: {}
  86. cin >> R >> C;
  87. cin >> startX >> startY;
  88. cin >> endX >> endY;
  89. }
  90.  
  91.  
  92.  
  93.  
  94. return 0;
  95. }
  96.