Go to diff to previous submission
#include <cstdio> #include <cstdlib> #include <queue> #include <set> struct XPoint{ int r; int c; int delka; XPoint(int i, int j, int k){ r = i; c = j; delka = k; } }; int main(){ int rows, cols, gregrow, gregcol, leafrow, leafcol; bool skok; int delka; bool mapa[101][101]; while(scanf("%d%d%d%d%d%d",&rows,&cols,&gregrow, &gregcol, &leafrow,&leafcol)==6){ if(gregcol == leafcol && gregrow == leafrow){printf("0\n"); continue;} std::queue<XPoint> fronta; skok = false; for(int i = 1; i <= rows; i++) for(int j = 1; j <= cols; j++) mapa[i][j] = false; fronta.push(XPoint(gregrow,gregcol,0)); mapa[gregrow][gregcol] = true; while(!fronta.empty()){ XPoint t = fronta.front(); fronta.pop(); delka = t.delka +1; int r = t.r - 1; int c = t.c - 2; if(r == leafrow && c == leafcol) {skok = true;break;} if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} r = t.r + 1; c = t.c - 2; if(r == leafrow && c == leafcol) {skok = true;break;} if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} r = t.r -2; c = t.c +1; if(r == leafrow && c == leafcol) {skok = true;break;} if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} r = t.r -2; c = t.c -1; if(r == leafrow && c == leafcol) {skok = true;break;} if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} r = t.r +2; c = t.c +1; if(r == leafrow && c == leafcol) {skok = true;break;} if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} r = t.r +2; c = t.c -1; if(r == leafrow && c == leafcol) {skok = true;break;} if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} r = t.r +1; c = t.c +2; if(r == leafrow && c == leafcol) {skok = true;break;} if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} r = t.r - 1; c = t.c + 2; if(r == leafrow && c == leafcol) {skok = true;break;} if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} } if(skok) printf("%d\n",delka); else printf("impossible\n"); } return 0; }
--- c4.s999.cteam033.grasshop.cpp.0.grasshop.cpp +++ c4.s1028.cteam033.grasshop.cpp.0.grasshop.cpp @@ -6,11 +6,11 @@ struct XPoint{ - int x; - int y; + int r; + int c; int delka; XPoint(int i, int j, int k){ - x = i; - y = j; + r = i; + c = j; delka = k; } @@ -26,5 +26,7 @@ bool mapa[101][101]; - while(scanf("%d%d%d%d%d%d",&rows,&cols, &gregcol, &gregrow,&leafcol,&leafrow)==6){ + while(scanf("%d%d%d%d%d%d",&rows,&cols,&gregrow, &gregcol, &leafrow,&leafcol)==6){ + if(gregcol == leafcol && gregrow == leafrow){printf("0\n"); continue;} + std::queue<XPoint> fronta; skok = false; @@ -43,43 +45,43 @@ delka = t.delka +1; - int x = t.x - 1; - int y = t.y - 2; - if(x == leafrow && y == leafcol) {skok = true;break;} - if(!mapa[x][y] && x >= 1 && x <= rows && y >= 1 && y <= cols) {fronta.push(XPoint(x,y,t.delka +1));mapa[x][y] = true;} + int r = t.r - 1; + int c = t.c - 2; + if(r == leafrow && c == leafcol) {skok = true;break;} + if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} - x = t.x + 1; - y = t.y - 2; - if(x == leafrow && y == leafcol) {skok = true;break;} - if(!mapa[x][y] && x >= 1 && x <= rows && y >= 1 && y <= cols) {fronta.push(XPoint(x,y,t.delka +1));mapa[x][y] = true;} + r = t.r + 1; + c = t.c - 2; + if(r == leafrow && c == leafcol) {skok = true;break;} + if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} - x = t.x -2; - y = t.y +1; - if(x == leafrow && y == leafcol) {skok = true;break;} - if(!mapa[x][y] && x >= 1 && x <= rows && y >= 1 && y <= cols) {fronta.push(XPoint(x,y,t.delka +1));mapa[x][y] = true;} + r = t.r -2; + c = t.c +1; + if(r == leafrow && c == leafcol) {skok = true;break;} + if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} - x = t.x -2; - y = t.y -1; - if(x == leafrow && y == leafcol) {skok = true;break;} - if(!mapa[x][y] && x >= 1 && x <= rows && y >= 1 && y <= cols) {fronta.push(XPoint(x,y,t.delka +1));mapa[x][y] = true;} + r = t.r -2; + c = t.c -1; + if(r == leafrow && c == leafcol) {skok = true;break;} + if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} - x = t.x +2; - y = t.y +1; - if(x == leafrow && y == leafcol) {skok = true;break;} - if(!mapa[x][y] && x >= 1 && x <= rows && y >= 1 && y <= cols) {fronta.push(XPoint(x,y,t.delka +1));mapa[x][y] = true;} + r = t.r +2; + c = t.c +1; + if(r == leafrow && c == leafcol) {skok = true;break;} + if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} - x = t.x +2; - y = t.y -1; - if(x == leafrow && y == leafcol) {skok = true;break;} - if(!mapa[x][y] && x >= 1 && x <= rows && y >= 1 && y <= cols) {fronta.push(XPoint(x,y,t.delka +1));mapa[x][y] = true;} + r = t.r +2; + c = t.c -1; + if(r == leafrow && c == leafcol) {skok = true;break;} + if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} - x = t.x +1; - y = t.y +2; - if(x == leafrow && y == leafcol) {skok = true;break;} - if(!mapa[x][y] && x >= 1 && x <= rows && y >= 1 && y <= cols) {fronta.push(XPoint(x,y,t.delka +1));mapa[x][y] = true;} + r = t.r +1; + c = t.c +2; + if(r == leafrow && c == leafcol) {skok = true;break;} + if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} - x = t.x - 1; - y = t.y + 2; - if(x == leafrow && y == leafcol) {skok = true;break;} - if(!mapa[x][y] && x >= 1 && x <= rows && y >= 1 && y <= cols) {fronta.push(XPoint(x,y,t.delka +1));mapa[x][y] = true;} + r = t.r - 1; + c = t.c + 2; + if(r == leafrow && c == leafcol) {skok = true;break;} + if(!mapa[r][c] && r >= 1 && r <= rows && c >= 1 && c <= cols) {fronta.push(XPoint(r,c,t.delka +1));mapa[r][c] = true;} }