#include #define MAX 200 using namespace std; int dx[] = {-1, 1, 2, 1,-1,-2}; // standard always follow. int dy[] = { 2, 2, 0,-2,-2, 0}; // standard always follow. string path[]={"UL","UR","R","LR","LL","L"}; int dis[MAX][MAX][6]; // editorial sol. char s[MAX][MAX]={'0'}; int n; struct node { // learn to make nodes. int x, y, dir; node() { } // an empty node creation. node(int x, int y, int dir) { // x , t denote the current position and the dir // ditermines what ??? this->x = x, this->y = y, this->dir = dir; } }; bool valid(int x, int y) // I did this in the first implementation. { if ( x < 0 || y < 0 || x >= n || y >= n ) return false; //if ( s[x][y] == '*' ) return false; return true; } int main() { cin>>n; int sx,sy,ex,ey; cin>>sx>>sy>>ex>>ey; int cnt1 = 0, cnt2 = 0; for(int i=0;i q; for ( int k = 0; k < 6; k++ ) { dis[sx][sy][k] = 0; q.push(node(sx, sy, k)); } int ans=0; while ( !q.empty() ) { node f = q.front(); q.pop(); for ( int k = 0; k < 6; k++ ) { // bfs traversal in a grid. int new_x = f.x + dx[k]; // thus making use of dx and dy. int new_y = f.y + dy[k]; if ( valid(new_x, new_y) && ((new_x - f.x)*(new_x - f.x) + (new_y - f.y)*(new_y - f.y)) < init ) { dis[new_x][new_y][k] = dis[f.x][f.y][f.dir] + 1; q.push(node(new_x, new_y, k)); } if(new_x == ex && new_y == ey){ ans = dis[new_x][new_y][k]; break; } } } printf("%d\n", ans); return 0; }