Knight Moves
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 23234 | Accepted: 10892 |
Description
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.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/d2b26809455ce72d961db3fab309bf1a.jpeg)
Input
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.
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
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 1
Sample Output
5 28 0
Source
TUD Programming Contest 2001, Darmstadt, Germany
题意:
求一个骑士(
.-' _..`.
/ .'_.'.'
| .' (.)`.
;' ,_ `.
.--.__________.' ; `.;-'
| ./ /
| | /
`..'`-._ _____, ..'
/ | | | |\ \
/ /| | | | \ \
/ / | | | | \ \
/_/ |_| |_| \_\
|__\ |__\ |__\ |__\)
马
从一点走到另一点的最小步数。
分析:
bfs,用队列保存节点,每次搜索八个方向,搜到之后返回步数。
代码:
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
//八个方向的偏移量
const int dir[][2] = {-2,-1, -1,-2, 1,-2, 2,-1, 1,2, 2,1, -1,2, -2,1};
struct pos
{
int x, y, cnt;
} cur, next, Beg, End;
int n;
int vis[310][310];
int bfs()
{
queue<pos> Q;
Q.push(Beg); //将起点放进队列
while(!Q.empty()) {
cur = Q.front(); //取队首元素
Q.pop();
if(cur.x == End.x && cur.y == End.y) return cur.cnt; //走到目标位置,返回步数
for(int i = 0; i < 8; i++) { //拓展八个方向
int tx = cur.x + dir[i][0];
int ty = cur.y + dir[i][1];
if(tx >= 0 && tx < n && ty >= 0 && ty < n && !vis[tx][ty]) { //判断下一个位置是否可行
vis[tx][ty] = 1; //标记
next.cnt = cur.cnt + 1; //步数加一
next.x = tx;
next.y = ty;
Q.push(next); //进队列
}
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
scanf("%d%d%d%d", &Beg.x, &Beg.y, &End.x, &End.y);
memset(vis, 0, sizeof(vis));
vis[Beg.x][Beg.y] = 1;
Beg.cnt = 0;
cout << bfs() << endl;
}
return 0;
}