Source code for submission s759

Go to diff to previous submission

grasshop.cpp

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <queue>
  4. #include <set>
  5.  
  6. #define getc() getc(stdin)
  7. typedef unsigned int uint;
  8. using namespace std;
  9.  
  10. struct pos {
  11. int r;
  12. int c;
  13. int jumps;
  14. };
  15.  
  16. int main()
  17. {
  18. int rMod[8] = {2,2,1,-1,-2,-2,-1,1};
  19. int cMod[8] = {-1,1,2,2,1,-1,-2,-2};
  20.  
  21. int R = 0, C = 0, GR = 0, GC = 0, LR = 0, LC = 0;
  22. while (scanf("%d%d%d%d%d%d", &R, &C, &GR, &GC, &LR, &LC) != EOF) {
  23. queue<pos> q;
  24. pos start;
  25. set<pair<int, int> > positions;
  26. start.r = GR;
  27. start.c = GC;
  28. start.jumps = 0;
  29. if (GR == LR && GC == LC) {
  30. printf("0\n");
  31. continue;
  32. }
  33. q.push(start);
  34. positions.insert(make_pair(start.r, start.c));
  35. bool done = false;
  36. while (!q.empty() && !done) {
  37. pos oldPos = q.front();
  38. q.pop();
  39. oldPos.jumps++;
  40. for (int i = 0; i < 8; i++) {
  41. pos newPos;
  42. newPos.jumps = oldPos.jumps;
  43. newPos.r = oldPos.r + rMod[i];
  44. newPos.c = oldPos.c + cMod[i];
  45. if (newPos.c == LC && newPos.r == LR) {
  46. printf("%d\n", newPos.jumps);
  47. done = true;
  48. break;
  49. }
  50. if (newPos.r <= R && newPos.r >= 1 &&
  51. newPos.c <= C && newPos.c >= 1) {
  52. if (positions.insert(make_pair(newPos.r, newPos.c)).second)
  53. {
  54. q.push(newPos);
  55. }
  56. }
  57. }
  58. }
  59.  
  60. if (!done) {
  61. printf("impossible\n");
  62. }
  63. }
  64. return 0;
  65. }
  66.  
  67.  

Diff to submission s716

grasshop.cpp

--- c4.s716.cteam093.grasshop.cpp.0.grasshop.cpp
+++ c4.s759.cteam093.grasshop.cpp.0.grasshop.cpp
@@ -27,4 +27,8 @@
         start.c = GC;
         start.jumps = 0;
+        if (GR == LR && GC == LC) {
+            printf("0\n");
+            continue;
+        }
         q.push(start);
         positions.insert(make_pair(start.r, start.c));