HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0=
题意:(百度复制粘贴0.0)
题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步
思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中,骑士是在一个3*2的格子中进行对角线移动,通过画图很容易就知道骑士最多可以朝八个方向移动,那么就朝8个方向进行BFS即可
//细节注意:
1.输入开始结束坐标时需要保存 所以我用了全局变量 因为输出还需要他们=0=;
2.依旧是用bool类型进行map保存,防止内存超限, 0表示还未走过, 1表示走过此点;
3.结束标志 找到e_x, e_y 此点;
4.BFS开始之前直接把开始坐标s_x, s_y转化好, 开始点从a-h表示X下标0-7, 1-8表示y下标0-7;
代码奉上:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define N 60
using namespace std;
int dir[8][2] = {1,2, 2,1, -1,2, 2,-1, 1,-2, -2,1, -1,-2, -2,-1};
bool maps[10][10];
char ch1, ch2;//输入时时字符 分表表示s_x e_x
int s_x, s_y, e_x, e_y;//开始坐标与结束坐标
typedef struct xy
{
int x, y, step;
} zb;//step保存从开始到此坐标需走步数
zb s, e;
void BFS()
{
int x, y;
zb v;
queue<zb>q;
q.push(s);
while(!q.empty())
{
int i;
s = q.front();
if(s.x == e.x && s.y == e.y)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n", ch1, s_y + 1, ch2, e_y + 1, s.step);
return;//输出时记得y下标比给的值大一 更正回来
}
q.pop();
for(i = 0; i < 8; i++)
{
x = s.x + dir[i][0];
y = s.y + dir[i][1];
if(x >= 0 && x < 8 && y >= 0 && y < 8 && maps[x][y] == 0)
{
maps[x][y] = 1;//走过标记
v = {x, y, s.step + 1};//步数别忘记加1
q.push(v);
}
}
}
}
int main()
{
while(~scanf(" %c%d %c%d", &ch1, &s_y, &ch2, &e_y))
{
memset(maps, 0, sizeof(maps));
s_x = ch1 - 'a';
s_y--;//给的Y比下标大1
e_x = ch2 - 'a';
e_y--;
s = {s_x, s_y, 0};
e = {e_x, e_y};
maps[s.x][s.y] = 1;//开始点记得标记已走过
BFS();
}
}
HDU 1372 Knight Moves的更多相关文章
- HDU 1372 Knight Moves(最简单也是最经典的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- 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 ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- HDU 1372 Knight Moves(BFS)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
随机推荐
- asp.net mvc4 设置build项目时,编译view页面
新建好项目后,把system.web.mvc.dll移除,重新选择本地C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies ...
- Leetcode: 4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- 大数的乘法(C++)
题目:POJ 2398 Bull Math Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13410 Accepted: ...
- 较全的IT方面帮助文档
http://www.shouce.ren/post/d/id/108632 XSLT参考手册-新.CHMhttp://www.shouce.ren/post/d/id/108633 XSL-FO参考 ...
- CMD执行BCP命令
C:\>BCP "EXEC GetU '2016-7-11' ,'-1'" queryout "C:\\C3Marketing\SummaryReport_test ...
- listivew 动态刷新单个item
使用ViewHolder来刷新某项数据,而不用每次都全部刷新数据. 继承BaseAdapter,新建ViewHolder类. public class TestListAdapter extends ...
- 错误,这个如何解决呢?内存溢出的问提。把JAVA_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128m 还是不行
java.lang.OutOfMemoryError: PermGen space at java.lang.ClassLoader.defineClass1(Native Method) at ja ...
- 关于Java项目打包
可以选择以下几种办法: 一.使用Eclipse,右键项目导出jar. 二.使用Eclipse,右键项目导出runnable jar. 三.使用Eclipse 插件fat jar,导出可执行的jar包. ...
- Python学习笔记(2)
变量 变量名就像我们现实社会的名字,把一个值赋值给一个名字时,它会存储在存储中,称之为变量(Variable),在大多数语言中,都把这种行为称为“给变量赋值”或“把值存储在变量中”. 而Python与 ...
- jquery总结04-DOM节点操作
一般js操作节点 ①创建节点(元素文本)document.createElement innerHTML ②添加属性 setAttribute ③加入文档 appendChild 操作繁琐还有兼容性 ...