Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.
Input
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.
The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.
Output
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.
For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.
Sample Input
3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1Sample Output
5 28 0
题意:第一行T组测试数据,下一行:n,棋盘为n*n,下两行为起始坐标和终点坐标,问从起点到终点最少要多少步。PS:只能按如图方向走。
思路:bfs
AC代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define maxn 500
using namespace std;
int vis[maxn][maxn];
int cnt[maxn][maxn];
int d[8][2] = {{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}};
int n,stx,sty,enx,eny;
int bfs(int stx,int sty)
{
queue<int> q;
q.push(stx);
q.push(sty);
while(!q.empty()){
int x = q.front(); q.pop();
int y = q.front(); q.pop();
if(x==enx&&y==eny) break;
for(int i=0;i<8;++i){
int nx = x+d[i][0];
int ny = y+d[i][1];
if(!vis[nx][ny]&&nx>=0&&nx<n&&ny>=0&&ny<n){
q.push(nx);q.push(ny);
vis[nx][ny] = 1;
cnt[nx][ny] = cnt[x][y]+1;
}
}
}
return cnt[enx][eny];
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
memset(vis,0,sizeof(vis));
memset(cnt,0,sizeof(cnt));
scanf("%d",&n);
scanf("%d %d %d %d",&stx,&sty,&enx,&eny);
vis[stx][sty] = 1;
int ans = bfs(stx,sty);
printf("%d\n",ans);
}
return 0;
}