-
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<queue> using namespace std; struct posi{ int x,y; }; posi st, ed; int tc; int l; int map[300][300]; bool d[300][300]; int dist[300][300]; int dx[] = {-2, -1,-2,-1,1,2,2,1}; int dy[] = {-1, -2,1, 2,-2,-1,1,2}; int ans = 0; void bfs( ){ queue<pair<int, int>> q; q.push({st.x, st.y}); dist[st.x][st.y] = 0; d[st.x][st.y] = true; while(!q.empty()){ int x = q.front().first; int y = q.front().second; q.pop(); for(int i = 0; i < 8; i++){ int nx = x+dx[i]; int ny = y+dy[i]; if(nx >= 0 && nx < l && ny>=0 &&ny <l){ if(d[nx][ny] == false){ q.push({nx,ny}); dist[nx][ny] = dist[x][y]+1; d[nx][ny] = true; if(nx == ed.x && ny == ed.y){ ans = dist[nx][ny]; } } } } } } int main(){ scanf("%d", &tc); for(int t = 1; t<=tc; t++){ ans = 0; memset(map, 0, sizeof(map)); memset(dist, 0, sizeof(dist)); memset(d,false, sizeof(d)); scanf("%d", &l); scanf("%d %d", &st.x, &st.y); scanf("%d %d", &ed.x, &ed.y); bfs(); printf("%d\n",ans); } return 0; }
BFS