Source code for submission s666

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. fifo.clear();
  26. for (r=0; r<R;r++)
  27. for (c=0; c<C; c++)
  28. grid[r][c]=0;
  29. cin >> GR >> GC >> LR >> LC;
  30. grid[GR-1][GC-1]=1;
  31. grid[LR-1][LC-1]=-1;
  32. fifo.push_back(GR-1);
  33. fifo.push_back(GC-1);
  34. while (!fifo.empty()) {
  35. r = fifo.front(); fifo.pop_front();
  36. c = fifo.front(); fifo.pop_front();
  37. if (hop(r,c,1,2) || hop(r,c,2,1) || hop(r,c,2,-1) || hop(r,c,1,-2) ||
  38. hop(r,c,-1,-2) || hop(r,c,-2,-1) || hop(r,c,-2,1) || hop(r,c,-1,2)) {
  39. fifo.push_back(GR-1);
  40. cout << grid[r][c] << endl;
  41. break;
  42. }
  43. }
  44. if (fifo.empty()) cout << "impossible" << endl;
  45. }
  46.  
  47. return 0;
  48. }
  49.  

Diff to submission s637

grasshop.cpp

--- c4.s637.cteam018.grasshop.cpp.0.grasshop.cpp
+++ c4.s666.cteam018.grasshop.cpp.0.grasshop.cpp
@@ -37,4 +37,5 @@
                         if (hop(r,c,1,2) || hop(r,c,2,1) || hop(r,c,2,-1) || hop(r,c,1,-2) || 
                                 hop(r,c,-1,-2) || hop(r,c,-2,-1) || hop(r,c,-2,1) || hop(r,c,-1,2)) {
+                                fifo.push_back(GR-1);
                                 cout << grid[r][c] << endl;
                                 break;