grasshop.cpp
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct Node {
int r;
int c;
int value;
};
bool canGo(int GR, int GC, int R, int C)
{
bool can = false;
if(GR >= 1 && GR <= R)
can = true;
if(GC >= 1 && GC <= C)
can = true;
else
can = false;
return can;
}
int isIn(int r, int c, Node* end,int Rgride, int Cgride, int LR, int LC)
{
if(LR == r && LC == c)
return 0;
for(int i = 0; i < 20; i++)
{
if( end[i].r == r && end[i].c == c )
{
if( canGo(end[i].r ,end[i].c,Rgride,Cgride) && (
canGo(end[i].r+2 ,end[i].c+2,Rgride,Cgride) ||
canGo(end[i].r+2 ,end[i].c-2,Rgride,Cgride) ||
canGo(end[i].r-2 ,end[i].c+2,Rgride,Cgride) ||
canGo(end[i].r-2 ,end[i].c+2,Rgride,Cgride) ) )
return end[i].value;
else
return -1;
}
}
return -1;
}
bool isMax(int a, int b){
if(a > b)
return true;
else
return false;
}
void createEnd( Node* end , int lr, int lc )
{
end[0].r = lr - 2;
end[0].c = lc - 1;
end[0].value = 1;
end[1].r = lr - 2;
end[1].c = lc - 0;
end[1].value = 2;
end[2].r = lr - 2;
end[2].c = lc + 1;
end[2].value = 1;
//---
end[3].r = lr - 1;
end[3].c = lc - 2;
end[3].value = 1;
end[4].r = lr - 1;
end[4].c = lc - 1;
end[4].value = 2;
end[5].r = lr - 1;
end[5].c = lc - 0;
end[5].value = 3;
end[6].r = lr - 1;
end[6].c = lc + 1;
end[6].value = 2;
end[7].r = lr - 1;
end[7].c = lc + 2;
end[7].value = 1;
//---
end[8].r = lr + 0;
end[8].c = lc - 2;
end[8].value = 2;
end[9].r = lr + 0;
end[9].c = lc - 1;
end[9].value = 3;
end[10].r = lr + 0;
end[10].c = lc + 1;
end[10].value = 3;
end[11].r = lr + 0;
end[11].c = lc + 2;
end[11].value = 2;
//---
end[12].r = lr + 1;
end[12].c = lc - 2;
end[12].value = 1;
end[13].r = lr + 1;
end[13].c = lc - 1;
end[13].value = 2;
end[14].r = lr + 1;
end[14].c = lc - 0;
end[14].value = 3;
end[15].r = lr + 1;
end[15].c = lc + 1;
end[15].value = 2;
end[16].r = lr + 1;
end[16].c = lc + 2;
end[16].value = 1;
//---
end[17].r = lr + 2;
end[17].c = lc - 1;
end[17].value = 1;
end[18].r = lr + 2;
end[18].c = lc - 0;
end[18].value = 2;
end[19].r = lr + 2;
end[19].c = lc + 1;
end[19].value = 1;
}
void setBoolean(int GR, int GC,int LR,int LC, bool&right, bool&top, bool&convert )
{
if(GR < LR) top = false;
else top = true;
if(GC < LC) right = true;
else right = false;
if( top && right ) // nahoru doprava
convert = isMax( GR-LR, GC-LC ); // true: nahoru 2x, doprava 1x; false: nahoru 1x, doprava 2x;
else if( top && !right ) // nahoru doleva
convert = isMax( GR-LR, GC-LC ); // true: nahoru 2x, doleva 1x; false: nahoru 1x, doleva 2x;
else if( !top && right ) // dolu doprava
convert = isMax( GR-LR, GC-LC ); // true: dolu 2x, doprava 1x; false: dolu 1x, doprava 2x;
else if( !top && !right ) // dolu doleva
convert = isMax( GR-LR, GC-LC ); // true: dolu 2x, doleva 1x; false: dolu 1x, doleva 2x;
}
int main(void) {
int R,C,GR,GC,LR,LC;
int pomGR,pomGC;
int inR, inC;
bool right,top,convert,posible;
int pomVal;
int counter = 0;
Node* end;
while(scanf("%d %d %d %d %d %d",&R,&C,&GR,&GC,&LR,&LC) == 6)
{
posible = true;
counter = 0;
inR = R;
inC = C;
pomGR = GR;
pomGC = GC;
setBoolean(GR,GC,LR,LC,right,top,convert);
end = new Node[20];
createEnd(end, LR, LC);
while( (pomVal = isIn(pomGR, pomGC, end, R, C, LR, LC)) == -1 ) {
setBoolean(pomGR,pomGC,LR,LC,right,top,convert);
if( top && right && convert )
{
pomGR = pomGR - 2;
pomGC = pomGC + 1;
}
else if( top && right && !convert )
{
pomGR = pomGR - 1;
pomGC = pomGC + 2;
}
//--------------
else if( top && !right && convert )
{
pomGR = pomGR - 2;
pomGC = pomGC - 1;
}
else if( top && !right && !convert )
{
pomGR = pomGR - 1;
pomGC = pomGC - 2;
}
//--------------
else if( !top && right && convert )
{
pomGR = pomGR + 2;
pomGC = pomGC + 1;
}
else if( !top && right && !convert )
{
pomGR = pomGR + 1;
pomGC = pomGC + 2;
}
//--------------
else if( !top && !right && convert )
{
pomGR = pomGR + 2;
pomGC = pomGC - 1;
}
else if( !top && !right && !convert )
{
pomGR = pomGR + 1;
pomGC = pomGC - 2;
}
if( !canGo(pomGR,pomGC,R,C) )
{
posible = false;
break;
}
counter ++;
}
counter += pomVal;
if(posible)
cout << counter << endl;
else
cout << "impossible" << endl;
}
return 0;
}