当前位置:网站首页>HDU 1372 & POJ 2243 Knight moves (breadth first search)
HDU 1372 & POJ 2243 Knight moves (breadth first search)
2022-07-04 19:36:00 【Acacia moon tower】
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 The template questions , Add a little conversion .
#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;
}
边栏推荐
- Summary and sorting of 8 pits of redis distributed lock
- Safer, smarter and more refined, Chang'an Lumin Wanmei Hongguang Mini EV?
- FTP, SFTP file transfer
- The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology
- Generate XML elements
- 线上数据库迁移的几种方法
- The 15th youth informatics competition in Shushan District in 2019
- Shell 编程核心技术《四》
- Functional interface
- Guys, for help, I use MySQL CDC 2.2.1 (Flink 1.14.5) to write Kafka and set
猜你喜欢

PolyFit软件介绍

Opencv functions and methods related to binary threshold processing are summarized for comparison and use
Summary and sorting of 8 pits of redis distributed lock

用实际例子详细探究OpenCV的轮廓绘制函数drawContours()

线上数据库迁移的几种方法

在线SQL转Excel(xls/xlsx)工具

LM10丨余弦波动顺势网格策略

HMM隐马尔可夫模型最详细讲解与代码实现

Detailed explanation of the binary processing function threshold() of opencv

Hough Transform 霍夫变换原理
随机推荐
The page element is vertically and horizontally centered, realizing the vertical and horizontal centering of known or unknown width.
socket编程demo二
如何使用Async-Awati异步任务处理代替BackgroundWorker?
Don't just learn Oracle and MySQL!
C# 使用StopWatch测量程序运行时间
The kth largest element in the array
MySQL数据库基本操作-DDL | 黑马程序员
HMM隐马尔可夫模型最详细讲解与代码实现
Opencv functions and methods related to binary threshold processing are summarized for comparison and use
2014 Hefei 31st youth informatics Olympic Games (primary school group) test questions
Pytest 可视化测试报告之 Allure
“只跑一趟”,小区装维任务主动推荐探索
Online data migration scheme encountered in the project 1 - general idea sorting and technical sorting
Shell 编程核心技术《三》
FPGA timing constraint sharing 01_ Brief description of the four steps
node_ Exporter deployment
Unity编辑器扩展C#遍历文件夹以及子目录下的所有图片
牛客小白月赛7 I 新建 Microsoft Office Word 文档
1672. 最富有客户的资产总量
Stream stream