Source code for submission s721

Go to diff to previous submission

grasshop.cpp

  1. #include <iostream>
  2. #include <list>
  3.  
  4. using namespace std;
  5. int R, C;
  6. int grid[100][100];
  7. list<int> fifo;
  8.  
  9. bool hop(int r, int c, int dr, int dc) {
  10. if ((r+dr)>=0 && (r+dr)<R && (c+dc)>=0 && (c+dc)<C) {
  11. if (grid[r+dr][c+dc] == -1)
  12. return true;
  13. else if (grid[r+dr][c+dc] == 0) {
  14. grid[r+dr][c+dc]=grid[r][c]+1;
  15. fifo.push_back(r+dr);
  16. fifo.push_back(c+dc);
  17. }
  18. }
  19. return false;
  20. }
  21.  
  22. int main(int argc, char** argv){
  23. int GR, GC, LR, LC, r, c;
  24. while (cin >> R >> C) {
  25. cin >> GR >> GC >> LR >> LC;
  26. if (GR==LR && GC==LC) {
  27. cout << 0 << endl;
  28. continue;
  29. }
  30. fifo.clear();
  31. for (r=0; r<R;r++)
  32. for (c=0; c<C; c++)
  33. grid[r][c]=0;
  34. grid[GR-1][GC-1]=1;
  35. grid[LR-1][LC-1]=-1;
  36. fifo.push_back(GR-1);
  37. fifo.push_back(GC-1);
  38. while (!fifo.empty()) {
  39. r = fifo.front(); fifo.pop_front();
  40. c = fifo.front(); fifo.pop_front();
  41. if (hop(r,c,1,2) || hop(r,c,2,1) || hop(r,c,2,-1) || hop(r,c,1,-2) ||
  42. hop(r,c,-1,-2) || hop(r,c,-2,-1) || hop(r,c,-2,1) || hop(r,c,-1,2)) {
  43. fifo.push_back(GR-1);
  44. cout << grid[r][c] << endl;
  45. break;
  46. }
  47. }
  48. if (fifo.empty()) cout << "impossible" << endl;
  49. }
  50.  
  51. return 0;
  52. }
  53.  

Diff to submission s666

grasshop.cpp

--- c4.s666.cteam018.grasshop.cpp.0.grasshop.cpp
+++ c4.s721.cteam018.grasshop.cpp.0.grasshop.cpp
@@ -23,9 +23,13 @@
         int GR, GC, LR, LC, r, c;
         while (cin >> R >> C) {
+                cin >> GR >> GC >> LR >> LC;
+                if (GR==LR && GC==LC) { 
+                        cout << 0 << endl;
+                        continue;
+                }
                 fifo.clear();
                 for (r=0; r<R;r++)
                         for (c=0; c<C; c++)
                                 grid[r][c]=0;
-                cin >> GR >> GC >> LR >> LC;
                 grid[GR-1][GC-1]=1;
                 grid[LR-1][LC-1]=-1;