#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define pb push_back #define ff first #define ss second #define mpa make_pair typedef long long LL; using namespace std; bool valid(int x, int y, int N, int M) { if(x <= 0 || y <= 0 || x > N || y > M) return false; return true; } int bfs(pair p1, pair p2, pair p3,int dx[],int dy[]) { int N = p3.ff; int M = p3.ss; queue, int> > Que; map, bool> Vis; Que.push(mpa(p1, 0)); while(!Que.empty()) { pair, int> temp = Que.front(); Que.pop(); if(temp.ff.ff == p2.ff && temp.ff.ss == p2.ss) return temp.ss; int x = temp.ff.ff; int y = temp.ff.ss; int dis = temp.ss + 1; if(Vis.count(mpa(x, y))) continue; Vis[mpa(x, y)] = true; for(int i = 0; i < 8; ++i) { int x1 = x + dx[i]; int y1 = y + dy[i]; if(valid(x1, y1, N, M)) Que.push(mpa(mpa(x1, y1), dis)); } } return -1; } int solve(int N, int M, int x1, int y1, int x2, int y2,int dx[],int dy[]) { pair p1; p1.ff = x1; p1.ss = y1; pair p2; p2.ff = x2; p2.ss = y2; pair p3; p3.ff = N; p3.ss = M; int ans = bfs(p1, p2, p3,dx,dy); return ans; } void fill(int dx[],int dy[],int a,int b) { for(int i=0;i<8;i++) { if(i==0 || i==1) dx[i] = a; if(i==2 || i == 3) dx[i] = -a; if(i == 4 || i == 5) dx[i] = b; if(i == 6 || i == 7) dx[i] = -b; } dy[0] = -b; dy[1] = b; dy[2] = b; dy[3] = -b; dy[4] = a; dy[5] = -a; dy[6] = a; dy[7] = -a; } int main(){ int n; cin >> n; int i,j; int dx[8],dy[8]; for(i=1;i