HDU 1372 Knight Moves (bfs)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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.
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.
xx to yy takes n knight moves.".
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std; struct point
{
int x,y;
};
int p[][]; // 标记步数
int sx,sy,dx,dy; // 起始地点 和 终点
int aa[][] = {-,-,-,-,,-,,-,,,,,-,,-,}; // 方向数组
void bfs(int a,int b)
{
memset(p,,sizeof(p));
queue<point > que;
point p1,p2,p3;
p1.x = a; p1.y = b; // 将起始点加入队列
que.push(p1);
p[a][b] = ;
if (a==dx && b==dy) // 如果起始点和终点相同 直接输出
return ;
while (!que.empty())
{
p2 = que.front();
for (int i = ; i < ; i ++)
{
p3.x = p2.x+aa[i][];
p3.y = p2.y+aa[i][];
if (p3.x> && p3.x<= && p3.y> && p3.y<=) // 判断边界
{
if (!p[p3.x][p3.y])
{
que.push(p3);
p[p3.x][p3.y] = p[p2.x][p2.y]+;
if (p3.x == dx && p3.y==dy) // 如果到达终点直接输出
return ;
}
}
}
que.pop();
}
return ;
}
int main ()
{
char s1[],s2[];
while (scanf("%s%s",s1,s2)!=EOF)
{
sx = s1[]-'a'+;
sy = s1[]-'';
dx = s2[]-'a'+;
dy = s2[]-'';
bfs(sx,sy);
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,p[dx][dy]);
}
return ;
}
HDU 1372 Knight Moves (bfs)的更多相关文章
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- 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 ...
- 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 (广搜)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- poj2243 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...
- poj2243 && hdu1372 Knight Moves(BFS)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏
Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...
随机推荐
- jquery之replaceAll(),replaceWith()方法详解
一:replaceAll() replaceAll()函数用于使用当前匹配元素替换掉所有的目标元素. 该函数属于jQuery对象(实例). 语法 jQuery 1.2 新增该函数. jQueryObj ...
- Count Primes - LeetCode
examination questions Description: Count the number of prime numbers less than a non-negative number ...
- stm32cube--IWDG使用
IWDG使用的是32芯片内部的40k独立晶振,该晶振为rtc和iwdg提供时钟,即使是主时钟坏了也不影响它们. 主要用到三个寄存器, IWDG_KR 键值寄存器 IWDG_PR 预分频寄 ...
- react native get started run 模拟机报错解决
参照 http://reactnative.cn/docs/0.30/getting-started.html#content 1)当执行 react-native run-android 这个环节的 ...
- IntelliJ IDEA 设置代码提示或自动补全的快捷键
IntelliJ IDEA 设置代码提示或自动补全的快捷键 点击 文件菜单(File) –> 点击 设置(Settings- Ctrl+Alt+S), –> 打开设置对话框. 在左侧的 ...
- Xshell 登录 AWS CentOS 出现“所选择的用户秘钥未在远程主机上注册“,最终解决办法!
其实就是 登录用户名错了,是 root,不是centos 也不是 ec2-user ! Xshell 连接配置界面如下 最重要是 登录授权配置 最后,登录成功! 就这么简单
- js+css3文字模糊代码
在写文字模糊的时候要理清自己的思路,根据以下的步骤来: 对你要模糊的文字进行布局 <body style="background:#ccc;"> <ul clas ...
- 读javascript高级程序设计03-函数表达式、闭包、私有变量
一.函数声明和函数表达式 定义函数有两种方式:函数声明和函数表达式.它们之间一个重要的区别是函数提升. 1.函数声明会进行函数提升,所以函数调用在函数声明之前也不会报错: test(); functi ...
- Java 并发和多线程(一) Java并发性和多线程介绍[转]
作者:Jakob Jenkov 译者:Simon-SZ 校对:方腾飞 http://tutorials.jenkov.com/java-concurrency/index.html 在过去单CPU时 ...
- 编译安装php 5.5 缺少依赖包 及解决方案
必要时可以用 YUM 选择安装以下相关软件包: #yum install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel ...