grasshop.cpp
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <climits>
#include <cstdio>
using namespace std;
int field[105][105];
bool vis[105][105];
int x[] = {1,2,2,1,-1,-2,-2,-1};
int y[] = {-2,-1,1,2,2,1,-1,-2};
int r,c,gx,gy,lx,ly;
bool check(int x, int y){
if(x < 1 || x >= c || y < 1 || y >= r)
return false;
return true;
}
int main(){
while(cin >> r >> c>>gy>>gx>>ly>>lx){
for(int i = 0; i < r + 1; i++)
for(int j = 0; j < c + 1; j++)
vis[i][j] = false;
queue < pair <int,int> > q;
q.push( pair<int,int>(gx,gy) );
field[gy][gx] = 0;
vis[gy][gx] = true;
bool ok = false;
pair<int,int> fq;
while(!q.empty()){
fq = q.front();
q.pop();
if(fq.first == lx && fq.second == ly){
ok = true;
break;
}
for(int i = 0; i < 8; i++){
if(check(fq.first + x[i], fq.second + y[i]))
if(!vis[fq.second + y[i]][fq.first + x[i]]){
vis[fq.second + y[i]][fq.first + x[i]] = true;
field[fq.second + y[i]][fq.first + x[i]] = field[fq.second][fq.first] + 1;
q.push( pair<int,int>(fq.first + x[i],fq.second + y[i]) );
}
}
}
if(ok)
cout << field[ly][lx] << endl;
else
cout << "impossible" << endl;
}
return 0;
}