poj 2243 Knight Moves

国际象棋中的马走棋盘问题 国际象棋马走棋盘:选择棋盘上的某个位置,找出一条遍历路径. 立即下载

Knight Moves
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11794 Accepted: 6646

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.

题意:国际象棋中,给出骑士的起始位置和目标位数,求骑士最少走几步就能到达目标位置。

解题思路:简单bfs,不明白为什么用c++提交TLE,G++提交AC;

#include <iostream>
#include <queue>
#include <string.h>
#include <stdio.h>
using namespace std;
struct point{
	int x,y;
	int step;
};
int a[]={-1, 1,-2, 2,-2, 2,-1, 1};
int b[]={-2,-2,-1,-1, 1, 1, 2, 2};
bool map[10][10];
int bfs(point start,point end){
	queue<point> q;
	start.step=0;
	q.push(start);
	while (q.size()){
		point p=q.front();
		if (p.x==end.x && p.y==end.y){
			return p.step;
		}
		for (int i=0;i<8;i++){	
			if (p.x+a[i]<0 || p.x+a[i]>7 || p.y+b[i]<0 || p.y+b[i]>7 || map[p.x+a[i]][p.y+b[i]]==true)
				continue;	
			point s;
			s.x=p.x+a[i];
			s.y=p.y+b[i];
			s.step=p.step+1;
			q.push(s);
		}
		map[p.x][p.y]=true;
		q.pop();
	}
	return -1;
}
int main(){
	char start[3],end[3];
	while (cin>>start>>end){		
		memset(map,false,sizeof(map));
		point starts,ends;
		starts.x=start[0]-'a';
		starts.y=start[1]-'1';
		ends.x=end[0]-'a';
		ends.y=end[1]-'1';
		cout<<"To get from "<<start<<" to "<<end<<" takes "<<bfs(starts,ends)<<" knight moves."<<endl;
	}
	return 0;
}


国际象棋马走棋盘问题 国际象棋马走棋盘:在棋盘中任意选择一个位置,找出一条遍历棋盘的路径. 立即下载

相关推荐

【Vite+Vue3+TS】基于Vite+Vue3+TypeScript+ESLint+Prettier+Stylelint搭建项目(亲测超详细)

结合了很多项目和文章,总结一篇项目构建的笔记。 温馨提示:文章末尾记录了我在搭建项目过程中,遇到的一些问题和解决办法 ~

aDiaoYa_的博客 4231

POJ-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

m0_56861113的博客 197

LVGL与STM32的理解(基于正点原子资料)

我们要在lcd_draw_fast_rgb_color里面实现对屏的具体操作(也就是在这里实现自己的代码)。lcd_set_window、lcd_write_ram_prepare、lcd_wr_data这三个函数是本例程LCD屏的控制函数(也就是自己写的函数)。以下是stm32工程的文件目录,GUI_APP也可以忽略(细节参考正点原子例程):这里主要是再次加强一下印象,主要就是把。,里面的lcd_init()与lcd_display_dir是。它们都是LVGL的接口,按照它们的需求,把内容给它们即可。

weixin_39257775的博客 3437

骑士的移动

题意:骑士在一个8*8的棋盘上移动,1-8代表行号,a-h代表列号,给出骑士的初始位置和目的位置,求骑士最少的移动步数;题目隐含一层意思(骑士移动规则是中国象棋的“马”的走法) 输入:一串字符串,包含起始位置和目的位置; 输出:骑士的最少移动步数; 解题思路:看到最少,立马想到BFS,按层搜索,步数加1,最后输出总步数; 1 #include<cstdio> 2 ...

weixin_34227447的博客 274

从零构建eSIM RSP服务:云原生架构与GSMA合规实践

本文详细阐述了从零开始构建一个符合GSMA SAS-SM安全标准的云原生eSIM RSP服务的完整路径。内容涵盖云原生基础设施选型、容器化部署、GSMA标准接口集成,并重点分享了通过SAS-SM安全审计的实战经验与避坑指南,为物联网及智能设备企业自建可控的远程SIM配置服务提供了系统性的解决方案。

weixin_29322553的博客 458

S32K3 eMIOS输入捕获(ICU)全解析:从信号测量到代码实现

本文深入解析了S32K3 eMIOS输入捕获(ICU)技术,从信号测量到代码实现的完整流程。详细对比了六种核心捕获模式的性能差异,提供了动态模式切换、Counter Bus配置、中断服务程序优化等实战技巧,并分享了汽车电子中的典型应用案例,如发动机转速测量和CAN唤醒脉冲检测。

weixin_33137081的博客 347

POJ - 2243 Knight Moves

https://vjudge.net/problem/POJ-2243 题目 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.

斯文~ 352

Jenkins中文显示不全?三步搞定Locale插件+汉化包的正确安装姿势

本文针对Jenkins界面中文显示不全问题,提供从原理到实战的深度解决方案。文章详细解析了Locale插件与中文语言包的核心机制,并分步指导如何通过配置JVM环境、正确安装插件及执行深度排查,彻底解决汉化顽疾,实现Jenkins界面的完美中文显示。

weixin_42611310的博客 259

POJ2243 Knight Moves —— A*算法

题目链接:http://poj.org/problem?id=2243 Knight Moves Time Limit:1000MS Memory Limit:65536K Total Submissions:14500 Accepted:8108 ...

alince20008的博客 223

PointNet++改进策略目录

后续我将如何使用文章中创新点加入的PointNet++中代码实现部分进行更新。

u012901740的博客 2195

LIS2DH三轴加速度传感器原理图设计,已量产(加速度传感器)

最近在做一款可穿戴设备原型时,选了 LIS2DH 这款加速度计 —— 比起之前常用的 ADXL345,它在低功耗和稳定性上的优化刚好戳中了可穿戴设备 “续航 + 可靠” 的核心需求。下面结合这次画的原理图,聊聊这个小模块的硬件设计细节。

不脱发的程序猿 388

POJ 2243 Knight Moves

Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9346   Accepted: 5342 Description A friend of you is doing research on the Traveling Knight Proble

Magic__ 922

Xilinx DDR3 MIG IP核设计--汇总导航

快速上手Xilinx DDR3 IP核----汇总篇

孤独的单刀 2万+

poj2243 Knight Moves

<br />#include<iostream><br /> #include<queue><br /> using namespace std;<br /> struct d_node{<br />  int a,b;<br /> };<br /> d_node d[8]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};<br /> int visited[8][8];<br /> struct state_node{<b

rightbag的专栏 878

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

Knight Moves(POJ2243)

题目: 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 ches...

qq_44859533的博客 483

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

POJ-2243 Knight Moves

POJ-2243 Knight Moves 题目链接:POJ-2243 题目大意:骑士移动 日字型移动 棋盘列数为字母 行数为数字 问从e2到e4最少需要几步 解题思路:标准BFS 代码块: #include<iostream> #include<queue> #include<cstring> #include<string> using na...

鱼头的水题之路 201

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

【POJ 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 o...

dingguayi7025的博客 92

POJ Knight Moves 2243 x

                  Knight Moves Time Limit:1000MS Memory Limit:65536K Total Submissions:13974 Accepted:7797 Description A friend of you is doing research on the...

anjian3128的专栏 84

POJ2243 Knight Moves

题目链接:http://poj.org/problem?id=2243 #include #include int knight[64][64]; void Floyed() { int i,j,k,x,y; for(i=0;i<64;++i) { for(j=0;j<64;++j) {//棋盘两格之间的相对位置 x=abs(i/8-j/8); y=

不可不戒 627

POJ 2243 Knight Moves bfs

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 chessbo...

起名好难呀! 168

POJ2243-Knight Moves

http://poj.org/problem?id=2243 最简单的bfs输出下路径 #include<stdio.h> #include<string.h> struct { int x,y,step; } queue[10000],e; bool mark[10][10]; int dir[8][2]= {2,1,1,2,-1,2,-2...

weixin_30726161的博客 127
上一篇: poj 2612 Mine Sweeper
下一篇: 初学DP(1) 黑书中的《括号序列》
JinbaoSite0144
博客等级 码龄12年 231粉丝 157原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值