PKU-2243 Knight Moves (单BFS、双BFS)

本文介绍了一种使用双向BFS算法解决骑士在国际象棋棋盘上从一个位置到另一个位置所需最少移动次数的问题。提供了两种不同的BFS实现:单向和双向搜索,并通过样例输入输出展示了算法的有效性。

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

原题链接 http://poj.org/problem?id=2243

Knight Moves

Description

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

Input

The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

Output

For each test case, print one line saying "To get from xx to yy takes n knight moves.".

Sample Input

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

Sample Output

To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.

 

Source Code(单向BFS)

#include <iostream>
#include <queue>
using namespace std;

int dir[8][2]={{2,-1},{2,1},{-2,-1},{-2,1},{1,2},{1,-2},{-1,2},{-1,-2}};
int searched[10][10];

struct node {
	int x, y;
	int step;
};

queue<node> Q;
node start, end, top, next;

bool isInside(int x, int y) {
	if (x < 1 || y < 1 || x > 8 || y > 8) {
		return false;
	}
	return true;
}

void BFS() {

	int i, newx, newy;

	while (!Q.empty()){
		Q.pop();
	}

	Q.push(start);

	while (!Q.empty()) {
		top = Q.front();
		Q.pop();

		for (i = 0; i < 8; i++) {
			newx = top.x + dir[i][0];
			newy = top.y + dir[i][1];
			if (isInside(newx, newy) && !searched[newx][newy]) {
				searched[newx][newy] = 1;
				next.x = newx;
				next.y = newy;
				next.step = top.step + 1;

				if (next.x == end.x && next.y == end.y) {
					printf("%d", next.step);
					return;
				} else {
					Q.push(next);
				}			
			}
		}
	}
}

int main () {
	int i, j;
	char start_ch[5], end_ch[5];
	while (scanf("%s %s", start_ch, end_ch) != EOF) {
		memset(searched, 0, sizeof(searched));
		start.x = start_ch[0] - 'a' + 1;
		start.y = start_ch[1] - '0';
		end.x = end_ch[0] - 'a' + 1;
		end.y = end_ch[1] - '0';

		searched[start.x][start.y] = 1;
		start.step = 0;
		printf("To get from %s to %s takes ",start_ch, end_ch);
		if (start.x == end.x && start.y == end.y) {
			printf("0");
		} else {
			BFS();
		}	
		printf(" knight moves.\n");
	}
	return 0;
}


Source Code(双向BFS)

#include <iostream>
#include <queue>
using namespace std;

int dir[8][2]={{2,-1},{2,1},{-2,-1},{-2,1},{1,2},{1,-2},{-1,2},{-1,-2}};
int searched[10][10], step[10][10];

struct node {
	int x, y;
	int step;
};

queue<node> Q, Q1;
node start, end, top, next;

bool isInside(int x, int y) {
	if (x < 1 || y < 1 || x > 8 || y > 8) {
		return false;
	}
	return true;
}

void BFS() {

	int i, newx, newy;

	while (!Q.empty()){
		Q.pop();
	}

	while (!Q1.empty()){
		Q1.pop();
	}

	Q.push(start);
	Q1.push(end);

	while (!Q.empty() || !Q1.empty()) {

		if (!Q.empty()) {
			top = Q.front();
			Q.pop();
			
			for (i = 0; i < 8; i++) {
				newx = top.x + dir[i][0];
				newy = top.y + dir[i][1];
				if (isInside(newx, newy)) {
					next.x = newx;
					next.y = newy;
					if (searched[newx][newy] == 0) {
						next.step = top.step + 1;
						searched[newx][newy] = 1;
						step[newx][newy] = next.step;
						Q.push(next);
					} else if (searched[newx][newy] == 2) {
						printf("%d", step[top.x][top.y] + step[next.x][next.y] + 1);
						return;
					}	
				}
			}
		}

		if (!Q1.empty()) {
			top = Q1.front();
			Q1.pop();
				
			for (i = 0; i < 8; i++) {
				newx = top.x + dir[i][0];
				newy = top.y + dir[i][1];
				if (isInside(newx, newy)) {	
					next.x = newx;
					next.y = newy;
					if (searched[newx][newy] == 0) {
						next.step = top.step + 1;
						searched[newx][newy] = 2;
						step[newx][newy] = next.step;
						Q1.push(next);
					} else if (searched[newx][newy] == 1) {
						printf("%d", step[top.x][top.y] + step[next.x][next.y] + 1);
						return;
					}	
				}
			}
		}
	}
}

int main () {
	int i, j;
	char start_ch[5], end_ch[5];
	while (scanf("%s %s", start_ch, end_ch) != EOF) {
		memset(searched, 0, sizeof(searched));
		start.x = start_ch[0] - 'a' + 1;
		start.y = start_ch[1] - '0';
		end.x = end_ch[0] - 'a' + 1;
		end.y = end_ch[1] - '0';

		searched[start.x][start.y] = 1;
		step[start.x][start.y] = 0;
		start.step = 0;

		searched[end.x][end.y] = 2;
		step[end.x][end.y] = 0;
		end.step = 0;

		printf("To get from %s to %s takes ",start_ch, end_ch);
		if (start.x == end.x && start.y == end.y) {
			printf("0");
		} else {
			BFS();
		}	
		printf(" knight moves.\n");
	}
	return 0;
}


 

内容概要:本文介绍了奕斯伟科技集团基于RISC-V架构开发的EAM2011芯片及其应用研究。EAM2011是一款高性能实时控制芯片,支持160MHz主频和AI算法,符合汽车电子AEC-Q100 Grade 2和ASIL-B安全标准。文章详细描述了芯片的关键特性、配套软件开发套件(SDK)和集成开发环境(IDE),以及基于该芯片的ESWINEBP3901开发板的硬件资源和接口配置。文中提供了详细的代码示例,涵盖时钟配置、GPIO控制、ADC采样、CAN通信、PWM输出及RTOS任务创建等功能实现。此外,还介绍了硬件申领流程、技术资料获取渠道及开发建议,帮助开发者高效启动基于EAM2011芯片的开发工作。 适合人群:具备嵌入式系统开发经验的研发人员,特别是对RISC-V架构感兴趣的工程师和技术爱好者。 使用场景及目标:①了解EAM2011芯片的特性和应用场景,如智能汽车、智能家居和工业控制;②掌握基于EAM2011芯片的开发板和芯片的硬件资源和接口配置;③学习如何实现基本的外设驱动,如GPIO、ADC、CAN、PWM等;④通过RTOS任务创建示例,理解多任务处理和实时系统的实现。 其他说明:开发者可以根据实际需求扩展这些基础功能。建议优先掌握《EAM2011参考手册》中的关键外设寄存器配置方法,这对底层驱动开发至关重要。同时,注意硬件申领的时效性和替代方案,确保开发工作的顺利进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值