POJ-1915 Knight Moves

本文详细介绍了如何使用双向广搜算法解决骑士在棋盘上从一个位置移动到另一个位置的最短路径问题。通过分析棋盘布局和骑士的移动规则,提出了一种有效的求解方法,并提供了代码实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Knight Moves
Time Limit: 1000MS Memory Limit: 30000K
 

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.

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.

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
————————————————————集训4.2的分割线————————————————————

前言:POJ-2243(ZOJ-1091)和这道题基本是一样的,但是棋盘的大小在2243中固定为8×8,这道题是n <= 300,所以(也许)就必须使用双向BFS了。但是我2243也是用的双向广搜……

还有一个AOJ-510,棋盘大小是20 000 000 × 20 000 000,图是根本存不下来了,但是设想两个点距离很远很远,可以先贪心到一个范围内再进行搜索。

思路:双向广搜可以减少数倍的空间使用量,(尽管时间上不一定占优势)。关键在于同时对两个队列进行扩展,vis数组有三个值来表示:0-没走过,1-q1走过,2-q2走过。当一个队列走到另一个队列走过的点的时候,起点和终点的骑士相遇得到解。写两遍bfs,用一个数组his来储存曾经走到这个点上的时候用到的步数,相遇就直接加上去返回,走过的不能走,没走过就更新his。

代码如下:

/*
ID: j.sure.1
PROG: 
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
#define LIM (nx >= 0 && nx < n && ny >= 0 && ny < n)
int sx, sy, ex, ey, n;
const int N = 305;
char vis[N][N];
int his[N][N];
const int d[8][2] = {{-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}};
struct Node
{
	int x, y, step;
};
queue <Node> qs, qe;

int bfs()
{
	while(!qs.empty() || !qe.empty()) {
		Node ts, te;
		if(!qs.empty()) {
			ts = qs.front(); qs.pop();
			for(int dd = 0; dd < 8; dd++) {
				int nx = ts.x + d[dd][0], ny = ts.y + d[dd][1];
				if(LIM && vis[nx][ny] != 1) {
					if(vis[nx][ny] != 0)
						return ts.step + 1 + his[nx][ny];
					else {
						Node ns;
						ns.step = ts.step + 1;
						ns.x = nx; ns.y = ny;
						his[nx][ny] = ns.step;
						vis[nx][ny] = 1;
						qs.push(ns);
					}
				}
			}
		}
		if(!qe.empty()) {
			te = qe.front(); qe.pop();
			for(int dd = 0; dd < 8; dd++) {
				int nx = te.x + d[dd][0], ny = te.y + d[dd][1];
				if(LIM && vis[nx][ny] != 2) {
					if(vis[nx][ny] != 0)
						return te.step + his[nx][ny] + 1;
					else {
						Node ne;
						ne.step = te.step + 1;
						ne.x = nx; ne.y = ny;
						his[nx][ny] = ne.step;
						vis[nx][ny] = 2;
						qe.push(ne);
					}
				}
			}
		}
	}
	return -1;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("1915.in", "r", stdin);
	freopen("1915.out", "w", stdout);
#endif
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d", &n);
		scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
		if(sx == ex && sy == ey)
			printf("0\n");
		else {
			memset(vis, 0, sizeof(vis));
			memset(his, 0, sizeof(his));
			while(!qs.empty()) qs.pop();
			while(!qe.empty()) qe.pop();
			Node st, et;
			st.x = sx; st.y = sy; st.step = 0;
			et.x = ex; et.y = ey; et.step = 0;
			qs.push(st); qe.push(et);
			vis[sx][sy] = 1; vis[ex][ey] = 2;
			printf("%d\n", bfs());
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值