grasshop.cpp
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
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;
fronta.push(make_pair(x,y));
if(x==m_x && y==m_y)
koniec=true;
}
}
}
int main()
{
pair<int,int> suradnice;
int x,y;
while(scanf("%d%d%d%d%d%d", &r, &s, &m_x, &m_y, &dest_x, &dest_y)==6)
{
memset(grid, 0, sizeof(int)*100*100);
koniec=false;
m_x--; m_y--;
dest_x--; dest_y--;
grid[dest_x][dest_y]=-1;
fronta.push(make_pair(dest_x,dest_y));
while(!fronta.empty())
{
suradnice=fronta.front();
fronta.pop();
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(grid[m_x][m_y]==0)
printf("impossible\n");
else
printf("%d\n", (grid[m_x][m_y]+1));
}
return 0;
}