HDU 1372 Knight Moves (广搜)
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.
分析:
题意求是求a点跳到b点。
一开始以为是简单的四个方向,发现步数老是不对,从a1到h8走了十几步,而案例只要6步。此时我就开始想可能不是简单的四个方向,开会仔细的验算案例。
可以看倒数第二个案例,从b1到c3,只要一步。对角线构成的矩形变成了有点”日“的形状,这正是象棋里的马走日。
所以是跳马。众所周知,中国象棋的马可以往八个方向跳。
所以改下方向。把输入弄好。
一个裸BFS。就能求出最小的步数了
代码:
#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;
int vis[10][10];
int dir[8][2]={{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}}; //马的八个方向
int sx,sy;
struct node
{
int x,y;
int step;
}
;
bool check(int x,int y)
{
if(x>8 ||x<1 ||y>8 ||y<1 ||vis[x][y])
return 0;
return 1;
}
int bfs(int x,int y)
{
node st,ed;
int i;
queue<node>q;
st.x=x;
st.y=y;
st.step=0;
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
if(st.x==sx &&st.y==sy)
return st.step;
for(i=0;i<8;i++)
{
ed.x=st.x+dir[i][0];
ed.y=st.y+dir[i][1];
if(!check(ed.x,ed.y))
continue;
ed.step=st.step+1;
vis[ed.x][ed.y]=1;
q.push(ed);
}
}
}
int main()
{
char s[22];
char s1[22];
int x2,y2;
int i,j;
while(scanf("%s%s",s,s1)!=EOF)
{
sx=s1[0]-'a'+1;
sy=s1[1]-'0';
x2=s[0]-'a'+1;
y2=s[1]-'0'; // 控制好输入
// printf("%d %d\n",x2,y2); // 用这个可以检查转为后的坐标
// printf("%d %d\n",sx,sy);
memset(vis,0,sizeof(vis));
vis[x2][y2]=1;
int ans=bfs(x2,y2); //裸BFS
printf("To get from %s to %s takes %d knight moves.\n",s,s1,ans);
}
return 0;
}
HDU 1372 Knight Moves (广搜)的更多相关文章
- HDU 1372 Knight Moves(最简单也是最经典的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- HDU 1372 Knight Moves 题解
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- ZOJ 1091 (HDU 1372) Knight Moves(BFS)
Knight Moves Time Limit: 2 Seconds Memory Limit: 65536 KB A friend of you is doing research on ...
随机推荐
- lol人物模型提取(一)
前段时间去青岛搞团建去了,闲来无事逛了会儿淘宝,无想买个lol手办,意之间发现了这张店铺宣传图: 哎呀我去,这模型做得挺逼真啊,然而这家店铺是卖zoe的cosplay道具的,不是手办-_-|| ...
- 3ds Max学习日记(二)
搞了一天终于把第二章弄完了,虽然有点慢,但也是没有办法,毕竟中途学累了总得放松一下吧. 又get到了一个新知识点,是关于3ds max的捕捉工具的,详情见3DS MAX 学习笔记 一(捕捉工具 ...
- table与div互相嵌套注意
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- windows 2008 iis7 上传大文件限制的真正解决办法
以前做了一个网站 ,当时本机测试时上传文件大小没有问题,上G也应该可以,可是放在服务器后只能上传小于30M以下文件,当时基本需要也基本在30M以下,就没有管,后在网上发现原来是window2008本身 ...
- CCS3 动画-鼠标放上去放大背景图片
---〉 效果如上,一个简单的过渡放大效果, <!DOCTYPE HTML> <html> <body> <style> #test{ width:30 ...
- 禁止移动端input弹出软键盘
在做三级联动,或者一些时间插件的时候总是弹出软键盘,用下面的方法就可以禁用掉,废话不多说直接上代码. HTML代码 <div class=""> <div> ...
- stm32的两种固件下载模式:JTAG和SWD
一.JTAG模式 这种模式一般有10pin的.14pin的和20pin的,尽管引脚数和引脚的排列顺序不同,但是其中有一些引脚是一样的.值得注意的是,不同的IC公司会自己定义自家产品专属的Jtag头,来 ...
- 海盗船长小米首页小船来回摆动CSS3.0效果
海盗船长小米首页小船来回摆动CSS3.0效果,偶然之间看到的,就写了一个. <!DOCTYPE html> <html lang="en"> <hea ...
- BZOJ5466 NOIP2018保卫王国(倍增+树形dp)
暴力dp非常显然,设f[i][0/1]表示i号点不选/选时i子树内的答案,则f[i][0]=Σf[son][1],f[i][1]=a[i]+Σmin(f[son][0],f[son][1]). 注意到 ...
- HTML、 CSS、 JavaScript三者的关系 1
HTML. CSS. JavaScript三者的关系 网页主要由三部分组成: 结构( Structure) . 表现( Presentation) 和行为( Behavior) HTML —— ...