HDU 1372 Knight Moves(BFS)
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.
题解:基础的bfs,但是和其他题目不同的是,这个走法是‘日’字形的,就像象棋中的马,还有就是这个棋盘没有障碍物,判断什么的就省了。
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
int spx,spy,epx,epy;
int dir[][] = {{-,-},{-,},{-,},{,},{,},{,-},{,-},{-,-}};
int mp[][];
int vis[][];
struct Node
{
int x,y;
int time;
};
int bfs(int t)
{
msv;
vis[spx][spy]=;
queue<Node> q;
while(!q.empty())q.pop();
Node node;
node.time=,node.x=spx,node.y=spy;
q.push(node);
while(!q.empty())
{
node=q.front();
q.pop();
if(node.x==epx&&node.y==epy)return node.time;
if(node.x>=||node.x<||node.y>=||node.y<)continue;
for(int i=;i<;i++)
{
Node nn;
nn.time=node.time+;
nn.x=node.x+dir[i][];
nn.y=node.y+dir[i][];
if(vis[nn.x][nn.y])continue;
else q.push(nn);
}
}
return -;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
char sp[],ep[];
while(cin>>sp>>ep)
{
spx=sp[]-'',spy=sp[]-'a';
epx=ep[]-'',epy=ep[]-'a';
msp;
int ans=bfs();
printf("To get from %s to %s takes %d knight moves.\n",sp,ep,ans);
}
return ;
}
HDU 1372 Knight Moves(BFS)的更多相关文章
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- 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(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- 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【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 ...
- 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
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- eclipse 配置 Tomcat 遇到的问题以及解决办法
Eclipse是一个开发JSP的很好的工具,而笔者在配置Tomcat服务器的时候遇到了一些小问题,在这里给大家总结一些经验,希望能帮助同样遇到这些问题的广大同行们能够简单轻松地解决这些问题~ 笔者在以 ...
- JavaScript基础(更新第二波)
下面接着说JavaScript打开新的窗口. open()方法可以查找一个已经存在或者新建的浏览器窗口. 语法: window.open([URL]),[窗口名称],[参数字符串] 参数说明: URL ...
- MySql 如何实现不同数据库同步【2个】
环境要求: Windows 操作系统 需要Mysql 3.23.15以后的版本. 假设数据库A为主机,数据库B为从机(A向B提供同步服务,即B中的数据来自A) A机器:IP=10.10.151.166 ...
- Scala内部类
注意:Java内部类从属于外部类,而Scala内部类从属于对象(外部类的实例本身).
- 【小技巧】C#判断电脑是否联网
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 关于css起名
作者:词晖链接:https://www.zhihu.com/question/19586885/answer/48933504来源:知乎著作权归作者所有,转载请联系作者获得授权. CSS书写顺序 1. ...
- Jenkins2.32.1+svn+maven安装配置与构建部署
这两天学习了一下持久化集成工具Jenkins,在自己的本地搭建一个简单的Jenkins环境. 使用环境:Windows64系统,JDK1.8,eclipse,svn(Windows版本VisualSV ...
- Java并发编程:深入剖析ThreadLocal(转载)
Java并发编程:深入剖析ThreadLocal(转载) 原文链接:Java并发编程:深入剖析ThreadLocal 想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨下ThreadL ...
- 大型网站技术架构 核心原理与案例分析 pdf
需要的小伙伴拿去,哈哈.百度云链接:https://pan.baidu.com/s/1hsuiM3q
- 利用虚函数减少导出DLL的头文件依赖
概要 设想这样一个场景:我有一个类FunClass,它的声明位于FunClass.h,并且在FunClass.h中,我还引用了secret.h. 现在我需要把FunClass导出成DLL文件供别人二次 ...