当前位置:网站首页>HDU 1372 & POJ 2243 Knight Moves(广度优先搜索)
HDU 1372 & POJ 2243 Knight Moves(广度优先搜索)
2022-07-04 17:59:00 【相思明月楼】
Problem 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 file 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模板题,加一点转换。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
typedef struct{
int x, y, step;
}p;
int dir[8][2] = {
{1,-2}, {2,-1}, {2,1}, {1,2}, {-1,2}, {-2,1}, {-2,-1}, {-1,-2}};
int map[10][10], ex, ey;
char s[5], s1[5];
int bfs() {
queue<p> q;
p t, next, start;
start.x = s[0]-'a', start.y = s[1]-'1';
start.step = 0;
ex = s1[0]-'a', ey = s1[1]-'1';
memset(map, 0, sizeof(map));
map[start.x][start.y] = 1;
q.push(start);
while(!q.empty()) {
t = q.front();
q.pop();
if(t.x == ex && t.y == ey) {
return t.step;
}
for(int i = 0; i < 8; i++) {
next.x = t.x + dir[i][0];
next.y = t.y + dir[i][1];
if(next.x == ex && next.y == ey) {
return t.step+1;
}
if(next.x >= 0&&next.x<8&&next.y>=0&&next.y<8&&map[next.x][next.y]==0) {
next.step = t.step + 1;
map[next.x][next.y] = 1;
q.push(next);
}
}
}
return 0;
}
int main() {
while(scanf("%s %s", s, s1) != EOF) {
printf("To get from %s to %s takes %d knight moves.\n", s, s1, bfs());
}
return 0;
}
边栏推荐
- How to use async Awati asynchronous task processing instead of backgroundworker?
- 使用canal配合rocketmq监听mysql的binlog日志
- 千万不要只学 Oracle、MySQL!
- 876. Intermediate node of linked list
- 更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
- Caché WebSocket
- 在线SQL转Excel(xls/xlsx)工具
- The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
- Unity编辑器扩展C#遍历文件夹以及子目录下的所有图片
- PointNeXt:通过改进的模型训练和缩放策略审视PointNet++
猜你喜欢
大div中有多个div,这些div在同一行显示,溢出后产生滚动条而不换行
一文掌握数仓中auto analyze的使用
小发猫物联网平台搭建与应用模型
Comment utiliser async awati asynchrone Task Handling au lieu de backgroundworker?
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0
建立自己的网站(15)
Opencv functions and methods related to binary threshold processing are summarized for comparison and use
PolyFit软件介绍
与二值化阈值处理相关的OpenCV函数、方法汇总,便于对比和拿来使用
A method of using tree LSTM reinforcement learning for connection sequence selection
随机推荐
Rookie post station management system based on C language
Shell programming core technology II
页面元素垂直水平居中、实现已知或者未知宽度的垂直水平居中。
Shell programming core technology "three"
每日一题(2022-07-02)——最低加油次数
Unity给自己的脚本添加类似编辑器扩展的功能案例ContextMenu的使用
redis分布式锁的8大坑总结梳理
2021 合肥市信息学竞赛小学组
Wechat reading notes of "work, consumerism and the new poor"
Upgrade the smart switch, how much is the difference between the "zero fire version" and "single fire" wiring methods?
函数式接口
DeFi生态NFT流动性挖矿系统开发搭建
C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
“只跑一趟”,小区装维任务主动推荐探索
神经网络物联网应用技术就业前景【欢迎补充】
php伪原创api对接方法
2022健康展,北京健博会,中国健康展,大健康展11月13日
Send and receive IBM WebSphere MQ messages
使用canal配合rocketmq监听mysql的binlog日志
Have you guys ever used CDC direct Mysql to Clickhouse