Go to diff to previous submission
#include <iostream> #include <list> using namespace std; int R, C; int grid[100][100]; list<int> fifo; bool hop(int r, int c, int dr, int dc) { if ((r+dr)>=0 && (r+dr)<R && (c+dc)>=0 && (c+dc)<C) { if (grid[r+dr][c+dc] == -1) return true; else if (grid[r+dr][c+dc] == 0) { grid[r+dr][c+dc]=grid[r][c]+1; fifo.push_back(r+dr); fifo.push_back(c+dc); } } return false; } int main(int argc, char** argv){ 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; grid[GR-1][GC-1]=1; grid[LR-1][LC-1]=-1; fifo.push_back(GR-1); fifo.push_back(GC-1); while (!fifo.empty()) { r = fifo.front(); fifo.pop_front(); c = fifo.front(); fifo.pop_front(); 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; } } if (fifo.empty()) cout << "impossible" << endl; } return 0; }
--- 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;