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 ( ...
随机推荐
- 13.56Mhz下直接阻抗匹配调试步骤
直接匹配阻抗,天线与射频芯片在同一块板子,调试步骤与50欧姆阻抗匹配调试天线参数差不多,多了一部分射频芯片端的滤波部分的参数计算.下面介绍调试过程. 1.首先看一下射频芯片发射部分原理图:分析原理图时 ...
- 浅谈DAO工厂设计模式(工厂模式的好处)
随着软件分层设计的流行及广泛的应用,对于DAO的设计模式大家已经不再陌生了,DAO层已经在软件系统的开发中成为必不可少的一层,将后台的数据层和前台的VO进行分离.前段时间也针对于DAO的设计介绍过一个 ...
- 使用datatable动态添加的顺序与存储的顺序不一致
原因是datatable在展示数据的时候帮助我们排序了 将其禁止排序即可:"ordering":false
- WIN10 设置WEB
Web服务器搭建步骤(Win10) 1.在“开始”菜单处打开“控制面板”. 2点击“程序”. 3.点击“启动或关闭Windows功能”. 4.对“Internet Information Servic ...
- 【应急响应】Tomcat安全加固
一.删除或选定无效账号 二.密码复杂度 三.启用日志记录功能 四.登录超时 五.Tomcat错误页面重定向 六.禁止Tomcat目录列表显示文件
- opencv静态编译
在Windows下opencv静态编译. 使用cmake生成visual Studio 2015 解决方案如下图所示: 重点看红色框线里的内容,先编译ALL_BUILD,这样就把所有子项目编译成功.所 ...
- Spring - MVC - thymeleaf 缓存关闭
1. 概述 spring 配合 thymeleaf 关闭页面缓存 2. 背景 最近复习 spring 找了本书叫 spring in action 5th 本人水平有限 书还写得那么难 调试中遇到了问 ...
- Flutter 开发入门实践
前言: Flutter 是 Google 推出的跨平台解决方案, 开发语言:Dart 优势: 劣势: 学习推荐: 官方网站:https://flutter.io/ 书籍:<Flutter技术入门 ...
- SpringMVC:ssm整合练习一
西部开源-秦疆老师:MyBatis,Spring,SpringMVC 整合练习一 , 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! ss ...
- Innovus 对multibit 的支持
如果在综合阶段没有做multibit merge, 或综合阶段由于缺失物理信息multibit cell merge 不合理,那就需要PR 工具做multibit merge 或split. Inno ...