Knight Moves
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 23833 | Accepted: 11202 |
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.

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
分析:宽度优先遍历,遇到终点返回步数就行了。
AC代码
#include <iostream>
using namespace std;
const int MAX_SIZE = 305;
int size;
int dir[8][2] = {{-1, -2},{-1, 2},{-2, -1},{-2, 1},{1, -2},{1, 2},{2, -1},{2, 1}};
int map[MAX_SIZE][MAX_SIZE] = {0};
typedef struct
{
int x;
int y;
int step;
}Point;
typedef struct
{
Point visited[90001];
int front;
int rear;
}Queue;
int bfs(Point start, Point end)
{
Queue queue = {{0}, 0, 0};
memset(map, 0, sizeof(map));
queue.visited[0].x = start.x;
queue.visited[0].y = start.y;
queue.visited[0].step = 0;
queue.rear++;
map[start.x][start.y] = 1;
if(start.x == end.x && start.y == end.y) return 0;
while(queue.front < queue.rear)
{
//if(queue.visited[queue.front].x == end.x && queue.visited[queue.front].y == end.y) break;
for(int i = 0; i < 8; i++)
{
int xx = queue.visited[queue.front].x + dir[i][0];
int yy = queue.visited[queue.front].y + dir[i][1];
if(xx >= 0 && xx < size && yy >= 0 && yy < size && map[xx][yy] == 0)
{
queue.visited[queue.rear].x = xx;
queue.visited[queue.rear].y = yy;
queue.visited[queue.rear].step = queue.visited[queue.front].step + 1 ;
queue.rear++;
map[xx][yy] = 1;
if(queue.visited[queue.rear - 1].x == end.x && queue.visited[queue.rear - 1].y == end.y)
{
return queue.visited[queue.rear-1].step;
}
}
}
queue.front++;
}
return 0;
}
int main()
{
Point start, end;
int num;
int result = 0;
freopen("input.txt","r", stdin);
//setbuf(stdout, NULL);
scanf("%d", &num);
while(num--)
{
cin >> size;
if(size < 4) return 0;
cin >> start.x >> start.y;
cin >> end.x >> end.y;
result = bfs(start, end);
cout <<result <<endl;
}
return 0;
}
说明:
由于之前visited数组大小为10000,本地调试通过,但提交到POJ时WA,后来改为90001后AC。
本题中,由于要求不使用库函数,所以队列是自己写的,可能效率不如STL,如果想使用STL经过简单的修改就可以了