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 ( ...
随机推荐
- AcWing STL初步学习
vector, 变长数组,倍增的思想 size() 返回元素个数 empty() 返回是否为空 clear() 清空 front()/back() push_back()/pop_back() beg ...
- Ubuntu16.04 anaconda3 opencv3.1.0 安装CPU版本caffe
安装anaconda3 安装opencv3.1.0 安装依赖库 修改Makefile.config 修改Makefile 编译报错,卸载anaconda中的protobuffer: conda uni ...
- Httpclient 工具类(get,put)
package com.googosoft.until; import java.io.IOException; import org.apache.http.HttpEntity; import o ...
- DFS(深度优先搜索)
基本概念 深度优先搜索算法(Depth First Search,简称DFS):一种用于遍历或搜索树或图的算法. 沿着树的深度遍历树的节点,尽可能深的搜索树的分支.当节点v的所在边都己被探寻过或者在搜 ...
- Nginx开始(自己使用,简单开始)
Nginx 1.网址 2.安装 如果需要资料的话可以直接来下载进行下载 前提 sudo yum install yum-utils 云主机重启之后需要执行 nginx -c /etc/nginx/ng ...
- source insight 编译后出现停止工作解决方法
解决方法: 工程的路径不要有中文,都用英文
- IdentityHashCodeTest
Java学习:identityHashCode和hashCode方法, System类提供了一个identityHashCode(Object x)方法,这个方法返回的是指定对象的精确hashCode ...
- 201771010135 杨蓉庆《面对对象程序设计(java)》第十二周学习总结
1.实验目的与要求 (1) 掌握Java GUI中框架创建及属性设置中常用类的API: (2) 掌握Java GUI中2D图形绘制常用类的API: (3) 了解Java GUI中2D图形中字体与颜色的 ...
- k8s Learning Notes
Kubernetes - 组件介绍 MESOS APACHE 分布式资源管理框架 2019-5 Twitter > Kubernetes Docker Swarm 2019-07 阿里云宣布 D ...
- 吴裕雄 python 神经网络TensorFlow实现LeNet模型处理手写数字识别MNIST数据集
import tensorflow as tf tf.reset_default_graph() # 配置神经网络的参数 INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE ...