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 ...
随机推荐
- iOS开发之iOS程序的启动过程
程序启动的完整过程: (1)main函数 (2)UIApplicationMain * 创建UIApplication对象 * 创建UIApplication的delegate对象 (3)delega ...
- 自适应的tab菜单栏
代码部分: Css代码:*{ margin:0px; padding:0px; font-size:62.5%;}body{ background-color:#FFFFFF;}.zw-test-ti ...
- webots自学笔记(六)实用控制器函数补充
原创文章,来自"博客园,_阿龙clliu" http://www.cnblogs.com/clliu/,转载请注明原文章出处. 用Webots软件做机器人仿真时,可以编 ...
- window7使用svn(svn系列 客户端 知识二总结)
♣eclipse插件subclipse ♣TortoiseSVN ♣svn操作 ♣注意事项 使用svn可以在eclipse直接装subclipse或者在windows使用TortoiseSVN ...
- Legendary Items-微软实习生笔试第一题
题目如下: 这道题难点不仅在于正确理解题意,判断递归条件,更在于用数学方法推出解决公式.因为N最大为1百万,而内存只有256MB, 所以暴力递归肯定会超时,超空间. 不过,我才疏学浅,又没有大量时间去 ...
- .net做的exe和electron做的exe之间的通信问题
目前工作遇到个问题: .net做的exe和electron做的exe,之间进行数据通信 目前找到两个相对方便的方法: 1.命名管道 ①.net命名管道资料: 进程间通信 - 命名管道实现 ②elect ...
- 随机跳转页面之使用VBA公共变量
p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...
- Wpf之布局
Wpf之布局 上一章大家有了自己的一个Hello World的wpf程序,今天咱们就一起走进WPF,一起来看看wpF的前台xaml这门语言的魅力. 写过web 的人都知道布局这个概念,在web中布局和 ...
- Android 布局(线性布局、相对布局)
一.线性布局(LinearLayout) <LinearLayout****</LinearLayout>1. orientation(布局方向)value=0 horizontal ...
- 深度学习开发环境搭建教程(Mac篇)
本文将指导你如何在自己的Mac上部署Theano + Keras的深度学习开发环境. 如果你的Mac不自带NVIDIA的独立显卡(例如15寸以下或者17年新款的Macbook.具体可以在"关 ...