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 ( ...
随机推荐
- PHP中的闭包和匿名函数
闭包的概念是指在创建闭包时,闭包会封装周围的状态的函数.即便闭包所在环境不在了.但闭包中封装的状态依然存在. 匿名函数就是没有名称的函数. 它们看似很函数一样,实际上它们属于Closure类的实例 P ...
- hdu 4358 Boring counting dfs序+莫队+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- pycharm的一些设置和快捷键
最近在搞python的开发,用上了pycharm,所以记录一些pycharm的设置 1. pycharm默认是自动保存的,但我个人不太习惯,习惯自己按ctrl + s 所以进行如下设置: 1. Se ...
- poj2280Amphiphilic Carbon Molecules(极角排序)
链接 卡了几天的破题,对于hdu的那份数据,这就一神题.. 借助极角排序,枚举以每一个点进行极角排序,然后构造两条扫描线,一个上面一个下面,两条同时走,把上线和下线的点以及上线左边的点分别统计出来,如 ...
- 查看本机的IP地址方法:
查看本机的IP地址方法:对于XP系统:方法一:如果右下角系统托盘区有本地连接的小电脑,双击小电脑→支持,就可以看到本机IP地址.无线连接也是一样.方法二:开始→运行cmd /k ipconfig,IP ...
- Cryptopp iOS 使用 RSA加密解密和签名验证签名
Cryptopp 是一个c++写的功能完善的密码学工具,类似于openssl 官网:https://www.cryptopp.com 以下主要演示Cryptopp 在iOS上的RSA加密解密签名与验证 ...
- div水平居中与垂直居中的方法【摘自美浩工作室官方博客 】
大家往往在写页面中会遇到不固定宽和高的div如果水平和垂直都居中呢?在写css的时候经常遇到的一个问题,当div没有固定的宽度或者高度的时候,如何才能让div水平或者垂直居中显示.如果div有固定宽度 ...
- C++学习之:括号匹配与栈的使用
#include <stack> using std::stack ; 变量定义: stack<T> stackName ; 成员函数: 成员函数 功能 bool empt ...
- 初学者:JSP登陆界面
学生登陆查询系统 1 程序的主要功能及特点 实现一个登录界面的基本功能,具体要求: 登录界面login.jsp含有表单,用户能够输入用户名和密码,并提交表单给verify.jsp. Verify.js ...
- 读<jquery 权威指南>[2]-事件
1. 事件冒泡 阻止事件冒泡的两种方式: event.stopPropagation(); return false ; 2. 绑定事件——bind(type,[data],function) ty ...