Go to diff to previous submission
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <queue> #include <utility> using namespace std; int grid[100][100]; int r, s, m_x, m_y ,dest_x, dest_y; bool koniec; queue<pair<int,int> > fronta; void skoc(int x, int y, int from_x, int from_y) { if(x<s && y<r && x>=0 && y>=0) { if(grid[x][y]==0) { grid[x][y]=grid[from_x][from_y]+1; //printf("x = %d y = %d n = %d --- from_x=%d, from_y=%d\n", x, y, grid[x][y], from_x, from_y); fronta.push(make_pair(x,y)); if(x==m_x && y==m_y) { //printf("koniec=true\n"); koniec=true; } } } } int main() { pair<int,int> suradnice; int x,y; while(scanf("%d%d%d%d%d%d", &r, &s, &m_y, &m_x, &dest_y, &dest_x)==6) { memset(grid, 0, sizeof(int)*100*100); koniec=false; m_x--; m_y--; dest_x--; dest_y--; grid[dest_x][dest_y]=-1; while(!fronta.empty()) fronta.pop(); //printf("fronta.push(%d,%d)\n", dest_x, dest_y); if(m_x==dest_x && m_y==dest_y) koniec=true; fronta.push(make_pair(dest_x,dest_y)); while(!fronta.empty()) { suradnice=fronta.front(); fronta.pop(); //printf("fronta.pop = (%d,%d)\n", suradnice.first, suradnice.second); x=suradnice.first+2; y=suradnice.second+1; skoc(x, y, suradnice.first, suradnice.second); x=suradnice.first+1; y=suradnice.second+2; skoc(x, y, suradnice.first, suradnice.second); x=suradnice.first-1; y=suradnice.second+2; skoc(x, y, suradnice.first, suradnice.second); x=suradnice.first-2; y=suradnice.second+1; skoc(x, y, suradnice.first, suradnice.second); x=suradnice.first-2; y=suradnice.second-1; skoc(x, y, suradnice.first, suradnice.second); x=suradnice.first-1; y=suradnice.second-2; skoc(x, y, suradnice.first, suradnice.second); x=suradnice.first+1; y=suradnice.second-2; skoc(x, y, suradnice.first, suradnice.second); x=suradnice.first+2; y=suradnice.second-1; skoc(x, y, suradnice.first, suradnice.second); if(koniec) break; } if(!koniec) printf("impossible\n"); else printf("%d\n", (grid[m_x][m_y]+1)); } return 0; }
--- c4.s993.cteam072.grasshop.cpp.0.grasshop.cpp +++ c4.s1071.cteam072.grasshop.cpp.0.grasshop.cpp @@ -23,5 +23,8 @@ fronta.push(make_pair(x,y)); if(x==m_x && y==m_y) + { + //printf("koniec=true\n"); koniec=true; + } } } @@ -41,4 +44,6 @@ while(!fronta.empty()) fronta.pop(); //printf("fronta.push(%d,%d)\n", dest_x, dest_y); + if(m_x==dest_x && m_y==dest_y) + koniec=true; fronta.push(make_pair(dest_x,dest_y)); while(!fronta.empty()) @@ -46,5 +51,5 @@ suradnice=fronta.front(); fronta.pop(); - // printf("fronta.pop = (%d,%d)\n", suradnice.first, suradnice.second); + //printf("fronta.pop = (%d,%d)\n", suradnice.first, suradnice.second); x=suradnice.first+2; @@ -82,5 +87,5 @@ if(koniec) break; } - if(grid[m_x][m_y]==0) + if(!koniec) printf("impossible\n"); else