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 ( ...
随机推荐
- Django的安装、使用详解、自动化测试应用以及程序打包
1.Django的安装 pip install Django 验证 Django 是否能被 Python 识别 >>> import django >>> prin ...
- pyfits fits图像区域选择
在用pyfits读取fits格式的图像时,得到的数组的结构如下 f=pyfits.open('rr.fits') data1=f[0].data data1数组的第一行,对应于图像的最下面一行,数组第 ...
- 编译和链接(lib和dll区别)(转载)
1.头文件并不参加链接和编译.编译器第一步要做的就是简单的把头文件在包含它的源文件中展开.不知你是否能理解这句话.也就是头文件里面有什么内容,通通把它移到包含这个头文件的源文件里.(我觉得这是个很重要 ...
- 概率DP lightoj 1265
题意: 1.两只老虎相遇 就互相残杀 2.老虎与鹿相遇 鹿死 3.老虎与人相遇 人死 4.人与鹿相遇 鹿死 5.鹿与鹿相遇 无果 求人活的概率 解析:如果老虎为0 则人活得概率为1 ...
- Opencv -lippicv
参考博客:https://blog.csdn.net/tmosk/article/details/76578082 参考博客:https://blog.csdn.net/kaka20080622/ar ...
- Go性能调优
文章引用自 Go性能调优 在计算机性能调试领域里,profiling 是指对应用程序的画像,画像就是应用程序使用 CPU 和内存的情况. Go语言是一个对性能特别看重的语言,因此语言中自带了 pr ...
- KFC 小猪短租
# 分析肯德基门店信息 import requests,json post_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op= ...
- 输入与输出 Perl 第五章
1. chmop($line=<STDIN>) ; #读取下一行,截掉换行符. 2. while(defined($line=<STDIN>) { print " ...
- wix在使用heat自动生成wxs时添加windows服务组件
最近需要给安装包增加一个windows服务组件,按照我的理解,我以为只需要Product.wxs加一段如下的标签就可以了 <Componet Id="myservice"&g ...
- 自编C++游戏
背景 周末无聊,于是编了一个类似于cmd的小玩意. 可是越想越不对劲,所以把它改成了一个小游戏. 信息 语言:DEV_C++ 源代码已公布!! 打败GG 版本:1.0(正式版) 版本:1.5(番外) ...