Go to diff to previous submission
#include <iostream> #include <cstdio> #include <queue> #include <set> #define getc() getc(stdin) typedef unsigned int uint; using namespace std; struct pos { int r; int c; int jumps; }; int main() { int rMod[8] = {2,2,1,-1,-2,-2,-1,1}; int cMod[8] = {-1,1,2,2,1,-1,-2,-2}; int R = 0, C = 0, GR = 0, GC = 0, LR = 0, LC = 0; while (scanf("%d%d%d%d%d%d", &R, &C, &GR, &GC, &LR, &LC) != EOF) { queue<pos> q; pos start; set<pair<int, int> > positions; start.c = GC; start.r = GR; start.jumps = 0; q.push(start); positions.insert(make_pair(GR, GC)); while (!q.empty()) { pos oldPos = q.front(); q.pop(); if (oldPos.c == LC && oldPos.r == LR) { printf("%d\n", oldPos.jumps); break; } oldPos.jumps++; for (int i = 0; i < 8; i++) { if (oldPos.r + rMod[i] <= R && oldPos.r + rMod[i] >= 1 && oldPos.c + cMod[i] <= C && oldPos.c + cMod[i] >= 1) { pos newPos; newPos.jumps = oldPos.jumps; newPos.r = oldPos.r + rMod[i]; newPos.c = oldPos.c + cMod[i]; if (positions.insert(make_pair(newPos.r, newPos.c)).second) { q.push(newPos); } } } } if (q.empty()) { printf("impossible\n"); } } // cout << "Hello World!" << endl; return 0; }
--- c4.s658.cteam093.grasshop.cpp.0.grasshop.cpp +++ c4.s689.cteam093.grasshop.cpp.0.grasshop.cpp @@ -20,5 +20,5 @@ int R = 0, C = 0, GR = 0, GC = 0, LR = 0, LC = 0; - while (scanf("%d%d%d%d%d%d", &R, &C, &GR, &GC, &LR, &LC) == 6) { + while (scanf("%d%d%d%d%d%d", &R, &C, &GR, &GC, &LR, &LC) != EOF) { queue<pos> q; pos start; @@ -28,5 +28,5 @@ start.jumps = 0; q.push(start); - positions.insert(make_pair(GC, GR)); + positions.insert(make_pair(GR, GC)); while (!q.empty()) {