Knight Moves(hdu1372 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1372
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6731 Accepted Submission(s): 4059
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.
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.
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; int mv[8][2] = {{-2,-1},{-1,-2},{-2,1},{-1,2},{1,-2},{2,-1},{1,2},{2,1}}; int v[9][9],map[9][9]; char a[3],b[3]; struct node { int x,y,ans; }q[1000001]; void bfs(int x,int y) { int e=0; int s=0; memset(v,0,sizeof(v)); struct node t,f; t.x=x; t.y=y; t.ans=0; v[t.x][t.y]=1; q[e++]=t; while(s<e) { t=q[s++]; if(map[t.x][t.y]==1) { printf("To get from %s to %s takes %d knight moves.\n",a,b,t.ans); } for(int i=0;i<8;i++) { f.x=t.x+mv[i][0]; f.y=t.y+mv[i][1]; f.ans=t.ans+1; if(f.x>=0&&f.x<8&&f.y>=0&&f.y<8&&v[f.x][f.y]==0) { q[e++]=f; v[f.x][f.y]=1; } } } } int main() { while(scanf("%s%s",a,b)!=EOF) { memset(map,0,sizeof(map)); map[b[1]-'0'-1][b[0]-'a']=1;//因为a-'0'从0开始,所以a[1]-'0'-1; bfs(a[1]-'0'-1,a[0]-'a'); } return 0; }
分类:
图论
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构