HDU 1372 Knight Moves(bfs)
嗯...
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372
这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向,然后从起点到终点跑一遍最典型的bfs即可...注意HDU的坑爹输入和输出...
AC代码:
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring> using namespace std; int ex, ey, sx, sy, step, g[][]; struct node{
int x, y, step;
}; int dir[][] = {{, }, {, }, {-, }, {, -}, {-, }, {, -}, {-, -}, {-, -}};
//方向
inline void bfs(){
memset(g, , sizeof(g));
queue<node> q;
node now, next;
now.x = sx;
now.y = sy;
now.step = ;
g[now.x][now.y] = ;
q.push(now);
while(!q.empty()){
now = q.front();
q.pop();
if(now.x == ex && now.y == ey){
step = now.step;
return;//找到终点
}
for(int i = ; i < ; i++){
next.x = now.x + dir[i][];
next.y = now.y + dir[i][];
if(next.x >= && next.x <= && next.y >= && next.y <= && !g[next.x][next.y]){
next.step = now.step + ;
g[next.x][next.y] = ;
q.push(next);
}
}
}
} int main(){
char c1, c2;
int s, t;
while(~scanf("%c%d %c%d", &c1, &s, &c2, &t)){
getchar();
sx = c1 - 'a' + ;
sy = s;
ex = c2 - 'a' + ;
ey = t;//起终点
bfs();
printf("To get from %c%d to %c%d takes %d knight moves.\n", c1, s, c2, t, step);
}
return ;
}
AC代码
HDU 1372 Knight Moves(bfs)的更多相关文章
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- 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 ...
- 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 (广搜)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- poj2243 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...
- poj2243 && hdu1372 Knight Moves(BFS)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...
- HDOJ/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 ( ...
随机推荐
- bootstrap的字体设置
@font-face { font-family: 'Glyphicons Halflings'; src: url('../fonts/glyphicons-halflings-regular.eo ...
- webpack-高级-发布策略
webpack的发布策略 在实际开发中,一般会有两套项目方案: 一套是开发期间的项目,包含了测试文件.测试数据.开发工具.测试工具等相关配置,有利于项目的开发和测试,但是这些文件仅用于开发,发布项目时 ...
- CefApp和CefClient的作用
CefApp 在cefsimple中,提到了一个cefapp的类型,这个类型是一个接口类,主要目的是提供获取三种handler的接口 /// // Implement this interface t ...
- redis哈希操作
用户可以通过执行hset命令为哈希中的指定字段设置值: 127.0.0.1:6379> hset hash field value 根据给定的字段是否存在于散列中,hset命令的行为也会有所不同 ...
- 主席树+二分 p4602
题意:给出每一种果汁的美味度,价格,升数: m个询问,每个询问给出最高上限的钱g,以及给出最少的w 意思是,最多用g的钱去买最少l的果汁,问能得到的最大美味度: 美味度是取所有果汁中美味度的最小值: ...
- Vue集成openlayers
1.安装ol库 使用cnpm i ol -s命令安装 2.建一个olMap.vue文件夹 <template> <div class='olMap'> <h2>{{ ...
- BufferedInputStream 介绍
BufferedInputStream 介绍 BufferedInputStream 是缓冲输入流.它继承于FilterInputStream.BufferedInputStream 的作用是为另一个 ...
- win api 音频可视化
暂时记录,改天有时间再完善...其实写好好久了,但以前的代码丢了,重新写一遍.. 原理和 python 的一样,获取输入设备,然后把数据读取到 buffer 中,在绘制出来. 这里要注意两点: 1. ...
- Python学习之列表篇
浮点数类型:round(x,d)可对浮点数进行四舍五入,科学计数法:aeb表示a*10^bpython大小写敏感整数类型:无范围限制,pow(x,y)表示x^y,想算多大算多大,四种表示形式:十进制, ...
- opencv:图像的算术操作
前提:输入图像的大小和类型必须一致 越界处理: 大于255,则会减去255 小于0,则等于0 基本计算,加减乘除 #include <opencv2/opencv.hpp> #includ ...