#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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, pair p4) { int N = p3.first; int M = p3.second; queue, int> > Que; map, bool> Vis; int dx = p4.first; int dy = p4.second; int ddx[8] = {dx, dx, -dx, -dx, dy, dy, -dy, -dy}; int ddy[8] = {-dy, dy, dy, -dy, dx, -dx, dx, -dx}; Que.push(make_pair(p1, 0)); while(!Que.empty()) { pair, int> temp = Que.front(); Que.pop(); if(temp.first.first == p2.first && temp.first.second == p2.second) return temp.second; int x = temp.first.first; int y = temp.first.second; int dis = temp.second + 1; if(Vis.count(make_pair(x, y))) continue; Vis[make_pair(x, y)] = true; for(int i = 0; i < 8; ++i) { int x1 = x + ddx[i]; int y1 = y + ddy[i]; if(valid(x1, y1, N, M)) Que.push(make_pair(make_pair(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.first = x1; p1.second = y1; pair p2; p2.first = x2; p2.second = y2; pair p3; p3.first = N; p3.second = M; pair p4; p4.first = dx; p4.second = dy; return bfs(p1, p2, p3, p4); } int main(){ int n; cin >> n; // your code goes here for (int i=1; i