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 ...
- 网页html结构搭建方法总结
在div+css布局中,一般都这样来整体构架的: <div id="header"></div><div id="center"& ...
- Verilog读写文件
在通过编写Verilog代码实现ram功能时,需要自己先计算寄存器的位数和深度再编写代码. 而如果需要在编写的ram中预置值的话,就需要使用Verilog语言编写程序读写文件,来将相应的数据赋给寄存器 ...
- Struts2的类型转换(下)
Struts2提供的类型转换: Struts2提供的是一个名叫StrutsTypeConverter的抽象类,这个类实际上是DefaultTypeConverter的子类.当我们继承自该抽象类 时,要 ...
- 在VB6/VBA中使用正则表达式
一.关于起因 最近在Office的QQ群里问如何在一串字符串中提取数值并加总的问题.如果使用正则表达式可以非常迅速的解决这个问题. 那么今天我就探讨一下在VB6/VBA中使用正则表达式的方法及代码,另 ...
- poptest老李谈分布式与集群 1
poptest老李谈分布式与集群 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:90882 ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(五)结合MockMvc进行服务端的单元测试
日常啰嗦 承接前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(四)单元测试实例>,已经讲解了dao层和service层的单元测试,还有控制器这层也不能 ...
- linux常用脚本
转载于http://justcoding.iteye.com/blog/1943504 我们在运维中,尤其是linux运维,都知道脚本的重要性,脚本会让我们的 运维事半功倍,所以学会写脚本是我们每个l ...
- [笔记]Learning to Rank算法介绍:RankNet,LambdaRank,LambdaMart
之前的博客:http://www.cnblogs.com/bentuwuying/p/6681943.html中简单介绍了Learning to Rank的基本原理,也讲到了Learning to R ...
- iOS 从url中获取文件名以及后缀
//这里有一个模拟器沙盒路径(完整路径) NSString* index=@"/Users/junzoo/Library/Application Support/iPhone Simulat ...