当前位置:网站首页>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;
}
边栏推荐
- FPGA timing constraint sharing 01_ Brief description of the four steps
- prometheus安装
- Shell 編程核心技術《四》
- Is the securities account opened by qiniu safe?
- 26. 删除有序数组中的重复项 C#解答
- 2021 Hefei informatics competition primary school group
- 关于判断点是否位于轮廓内的一点思考
- Build your own website (15)
- The difference and usage between substr (), slice (), and substring () in the string interception methods of "understand series after reading"
- Rookie post station management system based on C language
猜你喜欢
Pytorch学习(四)
Oracle with as ora-00903: invalid table name multi report error
MySQL数据库基本操作-DDL | 黑马程序员
Lm10 cosine wave homeopathic grid strategy
2022CoCa: Contrastive Captioners are Image-Text Fountion Models
To sort out messy header files, I use include what you use
自由小兵儿
Process of manually encrypt the mass-producing firmware and programming ESP devices
物联网应用技术的就业前景和现状
FPGA时序约束分享01_四大步骤简述
随机推荐
2019年蜀山区第十五届青少年信息学竞赛
Shell programming core technology "I"
千万不要只学 Oracle、MySQL!
Go microservice (II) - detailed introduction to protobuf
Is the securities account opened by qiniu safe?
C # implementation defines a set of SQL statements that can be executed across databases in the middle of SQL (detailed explanation of the case)
Download the first Tencent technology open day course essence!
Introduction to polyfit software
关于判断点是否位于轮廓内的一点思考
From automation to digital twins, what can Tupo do?
Is it safe to open an account at Great Wall Securities? How to open an account when buying stocks
Shell 编程核心技术《四》
在线SQL转Excel(xls/xlsx)工具
The kth largest element in the array
A method of using tree LSTM reinforcement learning for connection sequence selection
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
How test engineers "attack the city" (Part 2)
DeFi生态NFT流动性挖矿系统开发搭建
26. 删除有序数组中的重复项 C#解答
Explore the contour drawing function drawcontours() of OpenCV in detail with practical examples