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 ...
随机推荐
- lintcode-160-寻找旋转排序数组中的最小值 II
160-寻找旋转排序数组中的最小值 II 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 数组中可能存在 ...
- OSG数学基础:坐标系变换
三维实体对象需要经过一系列的坐标变换才能正确.真实地显示在屏幕上.在一个场景中,当读者对场景中的物体进行各种变换及相关操作时,坐标系变换是非常频繁的. 坐标系变换通常包括:世界坐标系-物体坐标系变换. ...
- cURL和file_get_contents实现模拟post请求
以前面试时候,面试官问过我后端有没有跨域问题,但是不敢肯定,现在可以肯定的说没有. 不文用php的cURL和file_get_contents方法分别实现后端跨域.本文场景也是在tp5下实现的. 一, ...
- Mysql8 忘记Root密码(转)
第一步:修改配置文件免密码登录mysql vim /etc/my.cnf 1.2 在 [mysqld]最后加上如下语句 并保持退出文件: skip-grant-tables 1.3 重启mysql服务 ...
- 初入py
1.下载工具sublime 我的网盘下载地址:https://pan.baidu.com/s/18-U1ZSg_zHoSAqUuvXj_PQ 直接解压即可 2.配置py27 在新建的文件里面编辑并保存 ...
- array to object
array to object native js & ES6 https://stackoverflow.com/questions/4215737/convert-array-to-obj ...
- 【数据库】各种主流 SQLServer 迁移到 MySQL 工具对比
在部署前期,首要任务就是考虑如何快速把基于 SQL Server 数据库的应用程序移植到阿里云的 MySQL 数据库.由于程序是基于 O/R mapping 编写,并且数据库中没有使用存储过程.用户函 ...
- BZOJ4813 CQOI2017小Q的棋盘(树形dp)
设f[i][j]为由i号点开始在子树内走j步最多能经过多少格点,g[i][j]为由i号点开始在子树内走j步且回到i最多能经过多少格点,转移显然. #include<iostream> #i ...
- CentOS 磁盘阵列(raid10)
1.通过mdadm命令进行磁盘阵列部署 mdadm是multiple devices admin的简称,它是Linux下的一款标准的软件 RAID 管理工具 如果没有mdadm命令,通过yum安装一下 ...
- POJ1066:Treasure Hunt——题解
http://poj.org/problem?id=1066 题目大意:给一个由墙围成的正方形,里面有若干墙,每次破墙只能从(当前看到的)墙的中点破,求最少破多少墙才能看到宝藏. —————————— ...