简单BFS题目 主要是读懂题意

和中国的象棋中马的走法一样,走日字型,共八个方向

我最初wa在初始化上了。。。。以后多注意。。。

代码:

 #include <iostream>
#include <cstdio>
#include<cstdlib>
#include <cstring>
#include<queue>
using namespace std;
char a[],b[];
int map[][];
int end_x,end_y;
int d[][]={{-,},{-,-},{,},{,-},{-,},{-,-},{,},{,-}};
class node
{
public:
int x;
int y;
int step;
friend bool operator <(node a,node b)
{
return a.step>b.step;
}
}cur,next;
priority_queue<node> q;
void init()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
map[i][j]=;
}
void bfs()
{
while(!q.empty())
{
cur=q.top();
q.pop();
if(cur.x==end_x&&cur.y==end_y)
{
printf("To get from %s to %s takes %d knight moves.\n", a,b,cur.step);
return ;
}
for(int i=;i<;i++)
{
int x=cur.x+d[i][];
int y=cur.y+d[i][];
if(x>||x<||y>||y<||map[x][y]==) continue;
next.x=x;
next.y=y;
map[x][y]=;
next.step=cur.step+;
q.push(next);
}
}
}
int main()
{
while(scanf("%s %s",a,b)!=EOF)
{
init();
while(!q.empty()) q.pop();
int y1=a[]-'a'+;
int x1=a[]-'';
int y2=b[]-'a'+;
int x2=b[]-'';
cur.x=x1;
cur.y=y1;
cur.step=;
q.push(cur);
end_x=x2;
end_y=y2;
bfs();
}
return ;
}

hdu1372 Knight Moves BFS 搜索的更多相关文章

  1. HDU1372:Knight Moves(BFS)

    Knight Moves Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  2. HDU1372:Knight Moves(经典BFS题)

    HDU1372:Knight Moves(BFS)   Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  3. HDU-1372 Knight Moves (BFS)

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

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

  5. poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...

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

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

  7. POJ 1915 Knight Moves(BFS+STL)

     Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 ...

  8. UVA 439 Knight Moves(BFS)

    Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...

  9. HDU 1372 Knight Moves(BFS)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

随机推荐

  1. Linux之cut命令

    cut 参数: -d  指定分隔符,与-f 一起使用,默认是空格.例如:-d“|” -f  指定取第几段的数据与-d一起使用 -c  以字符为单位取出固定字符区间 示例: 取不连续区间的内容的时候使用 ...

  2. Android -- onMeasure()源码分析

    1,作为自定义控件最重要的三个方法之一,onMeasure()可以说是我们研究的重点,今天我们更详细的来研究一下View的onMeasure()方法和ViewGroup的onMeasure()方法 2 ...

  3. 性能调优案例分享:jvm crash的原因 2

    3.core dump分析 有了core dump文件,接下来要做的就是通过命令去解析此文件,定位具体问题了,主要有以下三个命令: (1)先执行gdb $JAVA_HOME$/bin/java cor ...

  4. Linux简介与厂商版本上

    Linux简介与厂商版本   作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 1. Linux简介 Linux可以有狭义和广义两种 ...

  5. java 解压 zip 包并删除

    需求是这样的,  在服务器上有 运营上传的zip 包,内容是用户的照片,我需要做的是 获取这些照片上传,并保存到 数据库. 这里面的 上传照片,保存数据库都不难,主要问题是解压zip包,和删除zip ...

  6. 改变iOS app的icon(iOS10.3)

    改变iOS app的icon 官方 iOS10.3新增了可以让开发者去更改app的icon,接下来看看怎么更改. 官方API给的东西很少,只是介绍了一个实例方法: open func setAlter ...

  7. Centos/RHEL上查看主板型号

    老是搞忘记,专门做个记录: [root@media ~]# dmidecode | grep "Product Name" Product Name: To be filled b ...

  8. oracle定时执行一个存储过程

    首先需要新建存储过程 一 存储过程: create or replace procedure Insertdata is begin INSERT INTO tab_dayta select * fr ...

  9. Linux下deb包安装工具(附带安装搜狗输入法)

    环境是在ubuntu14下的 #1.gdebi安装 使用deb安装工具gdebi,这个工具能解决所有依赖问题 sudo apt-get install gdebi #2.搜狗输入法 deb包下载地址: ...

  10. 使用NPOI写入Excel数据(ASP.NET)

    第一次做这个写入数据的功能,研究了下npoi的类 IWorkbook wb = new HSSFWorkbook(); //创建表 ISheet sh = wb.CreateSheet("X ...