题目连接

简单bfs搜索


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int, int> pa;
typedef long long LL;
int dir[8][2]={2,1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2};
struct node
{
int x;
int y;
int step;
};
int vis[10][10];//标记数组
int st,sd,et,ed;
queue<node>que;
string c1,c2;
void init()
{
for(int i=0;i<=8;i++)
for(int j=0;j<=8;j++)
vis[i][j]=0;
}
void dfs()
{
node now,next;
now.x=st;
now.y=sd;
vis[st][sd]=1;
now.step=0;
while(!que.empty())
que.pop();
que.push(now);
while(!que.empty())
{
now=que.front();
que.pop();
if(now.x==et&&now.y==ed)
{
cout<<"To get from "<<c1<<" to "<<c2<<" takes "<<now.step<<" knight moves."<<endl;
break;
}
for(int i=0;i<8;i++)
{ int X=now.x+dir[i][0];
int Y=now.y+dir[i][1];
if(X>=1&&X<=8&&Y>=1&&Y<=8&&!vis[X][Y])
{
next.x=X;
next.y=Y;
next.step=now.step+1;
vis[X][Y]=1;
que.push(next);
}
}
}
}
int main ()
{
while(cin>>c1>>c2)
{
init();
st=c1[0]-'a'+1;
sd=c1[1]-'1'+1;
et=c2[0]-'a'+1;
ed=c2[1]-'1'+1;
dfs();
}
return 0;
}

HDU<1372>/bfs的更多相关文章

  1. hdu 1372 BFS

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...

  2. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  3. HDU 1372 Knight Moves(最简单也是最经典的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  4. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...

  5. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  6. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  7. 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 ...

  8. HDOJ/HDU 1372 Knight Moves(经典BFS)

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

  9. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

随机推荐

  1. ORA-39070

    背景介绍: 项目以前建立的库,表空间文件文件比较多,问了一下dba,了解到表空间建立很多没有带来优惠(都在一块磁盘上),效率也不会提高:现在要进行新库的迁移(目前使用的是asm磁盘组rac集群),正好 ...

  2. perl Socket接收超时设置

    一般来说, IO::Socket::INET里的Timeout设置是对于conncet的 如果你想设置recv接收超时, 可以这样设置: usr Socket: ...... , )); #注意这里p ...

  3. javaScript中的一些知识

    利用js动态生成table <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http ...

  4. 十三、oracle 数据字典和动态性能视图

    一.概念数据字典是oracle数据库中最重要的组成部分,它提供了数据库的一些系统信息.动态性能视图记载了例程启动后的相关信息. 二.数据字典1).数据字典记录了数据库的系统信息,它是只读表和视图的集合 ...

  5. Android 透明状态栏&着色状态栏

    Android 5.0 及以上实现方式(android在5.0之后引入Material Design 实现方式相对简单) 透明状态栏,背景浸入状态栏 if (Build.VERSION.SDK_INT ...

  6. 用Visual Studio 2015 编写第一个UMDF驱动遇到的问题!!

    前提:Visual Studio 2015已经成功安装了驱动环境,WDK都已经完全正常安装了,在Visual Studio 2015的菜单可以看到"Driver"菜单项了.这说明已 ...

  7. VMWare桥接、NAT和only-host三种模式

    如果你想利用VMWare安装虚拟机,或想创建一个与网内其他机器相隔离的虚拟系统,进行特殊的调试工作.此时,对虚拟系统网络连接模式的选择就非常重要了.如果你选择的工作模式不正确,就无法实现上述目的,也就 ...

  8. iPhone5s 等 64位真机 运行 带有百度地图等 仅支持32位系统API和SDK的问题

    将下图中画红色框的部分去掉,程序就不再支持64位了.iPhone5s 将使用它的32位兼容模式.(而不再是64位模式)百度地图也不会报错了.

  9. ios导航栏适配

    我们做屏幕导航栏横竖屏适配的时候,会发现top的值多少都有一点的偏移,加了背景色之后从0开始,不加背景色从64开始,解决方法self.extendedLayoutIncludesOpaqueBars ...

  10. kubernetes 留言版DEMO

    kubernates hello world1 关闭防火墙 $systemctl disable firewalld $systemctl stop firewalld 2 安装etcd 和 kube ...