Go to diff to previous submission
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; typedef unsigned long int ulint; ulint R, C, GR, GC, LR, LC; static ulint visited[150][150]; struct point { point () {} point (ulint x, ulint y) : x(x), y(y) {} ulint x; ulint y; }; static int drow[] = {1,1,2,2,-1,-1,-2,-2}; static int dcol[] = {2,-2,1,-1,2,-2,1,-1}; inline void solve() { visited[GR][GC] = 1; queue<point> bfsq; bfsq.push(point(GR, GC)); point curr; while (!bfsq.empty()) { curr = bfsq.front(); bfsq.pop(); ulint nx; ulint ny; for (uint d = 0; d < 8; ++d) { nx = curr.x + drow[d]; ny = curr.y + dcol[d]; if (nx > 0 && nx <= R && ny > 0 && ny <= C && visited[nx][ny] == 0) { visited[nx][ny] = visited[curr.x][curr.y] + 1; bfsq.push(point(nx, ny)); } } } /* for (uint i = 1; i<=R; i++) { for (uint j=1; j<=C; j++) { printf("%lu ",visited[i][j]); } printf("\n"); } */ if (visited[LR][LC] > 0) { printf("%lu\n", visited[LR][LC] - 1); } else { printf("impossible\n"); } } int main() { while (scanf("%lu %lu %lu %lu %lu %lu", &R, &C, &GR, &GC, &LR, &LC) != EOF) { memset(visited, 0, sizeof(visited)); solve(); } }
--- c4.s581.cteam097.grasshop.cpp.0.grasshop.cpp +++ c4.s595.cteam097.grasshop.cpp.0.grasshop.cpp @@ -43,5 +43,5 @@ ny = curr.y + dcol[d]; - if (nx > 0 && nx < R && ny > 0 && ny < C && visited[nx][ny] == 0) + if (nx > 0 && nx <= R && ny > 0 && ny <= C && visited[nx][ny] == 0) { visited[nx][ny] = visited[curr.x][curr.y] + 1; @@ -51,4 +51,13 @@ } + /* + for (uint i = 1; i<=R; i++) { + for (uint j=1; j<=C; j++) { + printf("%lu ",visited[i][j]); + } + printf("\n"); + } + */ + if (visited[LR][LC] > 0) {