hdu1372 Knight Moves BFS 搜索
简单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 搜索的更多相关文章
- HDU1372:Knight Moves(BFS)
Knight Moves Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- HDU1372:Knight Moves(经典BFS题)
HDU1372:Knight Moves(BFS) Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- 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 ( ...
- poj2243 && hdu1372 Knight Moves(BFS)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- POJ 1915 Knight Moves(BFS+STL)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20913 Accepted: 9702 ...
- UVA 439 Knight Moves(BFS)
Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...
- HDU 1372 Knight Moves(BFS)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
随机推荐
- python的with语句,超级强大
With语句是什么? 有一些任务,可能事先需要设置,事后做清理工作.对于这种场景,Python的with语句提供了一种非常方便的处理方式.一个很好的例子是文件处理,你需要获取一个文件句柄,从文件中读取 ...
- bootstrap-typeahead自动补充
官方文档:https://github.com/bassjobsen/Bootstrap-3-Typeahead 这个是基于bootstrap的typeahead与基于jquery的jquery-Ty ...
- 2017-3-28 JavaScript 基础、语法
前端三剑客: html+css+js(html 决定网页上有什么,css决定东西是怎么摆放的,js决定东西的功能) js定义: js是一个脚本语言,需要有宿主文件,它的宿主文件是html文件. js ...
- PRINCE2学习
今天对PRINCE2中提及的7大主题进行学习,主要的内容是通过概述和PMBOK之间的对比做一些总结,每个主题所包含的过程和方法并没有太多涉及,没有对整个体系有全面深入的学习,有些断章取义的地方也请博友 ...
- Win7+CentOS双系统(一)
注意:1.由于涉及到对硬盘操作,请妥善备份数据,避免损失. 2.为了达到每个步骤都有图片覆盖,我使用了虚拟机来进行测试,不过请大家放心,我已经在自己的物理机上成功实现了. 3.我的步骤是绝对正确和缺一 ...
- stick footer布局
需求: 将footer固定到底部.文章内容不足满屏时 footer在底部,超过满屏时footer在内容末尾. 方法一: <div id="wrap"> <div ...
- webscoket实战之利用httpsession定向推送
webscoket实战之利用httpsession定向推送 开发框架 springboot 场景 在利用websocket主动推送信息给客户端的过程中,经常会遇到一个普遍需求,就是推送的消息要定向推送 ...
- 你的计算机也可以看懂世界——十分钟跑起卷积神经网络(Windows+CPU)
众所周知,如果你想研究Deep Learning,那么比较常用的配置是Linux+GPU,不过现在很多非计算机专业的同学有时也会想采用Deep Learning方法来完成一些工作,那么Linux+GP ...
- Linux-配置vim开发环境
vim是一个类似于vi的著名的功能强大.高度可定制的文本编辑器,在vi的基础上改进和增加了很多特性.vim是纯粹的自由软件. 为了满足使用者的要求,将vim界面配置为自己想要的界面类型也变得流行起来. ...
- SecureCRT中vim乱码问题
Options->Session Options,接着在Terminal->Appearance页签中设定Character encoding为Unicode (UTF-8).