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