hdu Knight Moves
这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int c[][];
int dir[][] = {{-,-},{-,},{-,},{,},{,},{,-},{,-},{-,-}};
typedef struct
{
int x,y,count;
}node;
node start,finish;
int bfs()
{
memset(c,,sizeof(c));
node pre,cur;
start.count = ;
queue<node> q;
q.push(start);
c[start.x][start.y] = ;
while(!q.empty())
{
pre = q.front();
q.pop();
if(pre.x == finish.x&&pre.y == finish.y)
return pre.count;
for(int i = ; i < ; i++)
{
cur.x = pre.x + dir[i][];
cur.y = pre.y + dir[i][];
if(cur.x<||cur.x>||cur.y<||cur.y>)continue;
if(c[cur.x][cur.y]==)continue;
c[cur.x][cur.y] = ;
cur.count = pre.count + ;
q.push(cur);
}
}
return -;
}
int main()
{
char row,end;
int col,ed;
int min;
while(scanf("%c",&row)!=EOF)
{
scanf("%d",&col);
getchar();
scanf("%c%d",&end,&ed);
getchar();
start.x = row-'a'+;
start.y = col;
finish.x = end-'a'+;
finish.y = ed;
if(start.x==finish.x&&start.y==finish.y)
min = ;
else min = bfs();
printf("To get from %c%d to %c%d takes %d knight moves.\n",row,col,end,ed,min);
}
return ;
}
hdu 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
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] 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
其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...
- 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 ...
随机推荐
- 【转】Linux中如何安装.rpm、.tar、.tar.gz和tar.bz2
我以下面三个包为例:(三个包都在/etc/opt下)A.example-1.2.3-1.rpmB.example-1.2.3-1.tarC.example-1.2.3-1.tar.gz 1.对于rpm ...
- MySQL下载安装、配置与使用(win7x64)
用过MySQL之后,不论容量的话,发现比其他两个(sql server .oracle)好用的多,一下子就喜欢上了.下面给那些还不知道怎么弄的童鞋们写下具体的方法步骤. 工具/原料 电脑 win7 6 ...
- codeforces B. Petya and Staircases 解题报告
题目链接:http://codeforces.com/problemset/problem/362/B 题目意思:给出整数n和m,表示有n级楼梯和m级dirty的楼梯,接下来m个数表示对应是哪一个数字 ...
- Ubuntu 13.04安装搜狗输入法
Ubuntu 13.04安装搜狗输入法 [日期:2013-07-08] 来源:Linux公社 作者:LinuxIDC.com [字体:大 中 小] 目标:在Ubuntu 13.04以及基于U ...
- jQuery基础DOM和CSS操作
$('#box').html();//获取 html 内容$('#box').text();//获取文本内容,会自动清理 html 标签$('#box').html('<em>www.li ...
- XX管理系统案例
一.登录界面建立登录文件夹Login,在此目录下面建立如下文件:Index.htm:登录页面ValidateCode.cs:生成验证码ProcessVerification.ashx:处理验证码Com ...
- vector data() [c++11]
Example 12345678910111213141516171819202122 // vector::data #include <iostream> #include <v ...
- 调试工具GDB详解
1 简介 2 生成调试信息 3 启动GDB 的方法 4 程序运行上下文 4.1 程序运行参数 4.2 工作目录 4.3 程序的输入输出 5 设置断点 5.1 简单断点 5.2 多文件设置断点 5.3 ...
- tar split命令
转自:http://www.cnblogs.com/xiaouisme/archive/2011/05/25/2057435.html tar是文件打包工具,split是文件分割工具,在邮件中发送附件 ...
- jquery easy ui 1.3.4 窗口,对话框,提示框(5)
5.1.window(窗口) 窗口我们在程序中会大量的使用,比如我们的添加.编辑都可以使用窗口实现,与winform的程序非常的类似.下面的代码是创建一个基本的窗口 $(function () { $ ...