HDU 1372 Knight Moves(最简单也是最经典的bfs)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1372
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13384 Accepted Submission(s): 7831
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
#include<bits/stdc++.h>
using namespace std;
#define max_v 10
int G[max_v][max_v];
int dir[][]= {{,-},{,-},{,},{,},{-,},{-,},{-,-},{-,-}};
int step;
int sx,sy,fx,fy;
struct node
{
int x,y,step;
};
void bfs()
{
memset(G,,sizeof(G));
queue<node> q;
node p,next;
p.x=sx;
p.y=sy;
p.step=;
G[p.x][p.y]=;
q.push(p); while(!q.empty())
{
p=q.front();
q.pop(); if(p.x==fx&&p.y==fy)
{
step=p.step;
return ;
} for(int i=; i<; i++)
{
next.x=p.x+dir[i][];
next.y=p.y+dir[i][]; if(next.x>=&&next.y>=&&next.x<=&&next.y<=&&G[next.x][next.y]==)
{
next.step=p.step+;
G[next.x][next.y]=;
q.push(next);
}
}
}
}
int main()
{
char c1,c2;
int y1,y2;
while(~scanf("%c%d %c%d",&c1,&y1,&c2,&y2))
{
getchar();
sx=c1-'a'+;
sy=y1;
fx=c2-'a'+;
fy=y2;
bfs();
printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,y1,c2,y2,step);
}
return ;
}
HDU 1372 Knight Moves(最简单也是最经典的bfs)的更多相关文章
- 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 (广搜)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- [宽度优先搜索] 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【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 ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
随机推荐
- STL:vector用法总结
一:介绍 vector是C++标准模板库,是一个容器,底层是数组,为连续内存.命名空间为std,所属头文件为<vector> 注意:不是<vector.h>vector存储 ...
- 游戏源码--Unity开源Moba游戏-服务器-客户端完整V1.0
http://www.manew.com/thread-111658-1-1.html
- mavne 工程jsp页面首行报错
在pom.xml文件中加入servlet依赖 <dependency> <groupId>javax.servlet</groupId> <artifactI ...
- Beyond Compare 4试用期已过
Beyond Compare 很好用,但是只有一段时间的试用时间,当试用期过了之后就提示不能试用了 怎么办呢? 我在网上找到了两个方法: 1.直接用注册码(来自:https://blog.csdn.n ...
- CF 304B——Calendar——————【年月日计算】
B - Calendar Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...
- flask表单flask-wtf
一.安装pip install flask-wtf 二.创建一个flask的项目引入相对应的包 from flask import Flask,render_template import flask ...
- jquery监控input输入框的变化
(function($) { $.fn.watch = function(callback) { return this.each(function() { //缓存以前的值 $.data(this, ...
- IIS发布常见错误-HTTP 错误 404.0 - Not-Found
错误信息:HTTP 错误 404.0 - Not-Found 错误代码:0x80070002 原 因:IIS配置错误. 解决方法:我配置IIS时漏掉了下面几项,一定要记得勾选.
- linux shell基础编程2
while循环 语法1: while [ 条件 ] do 命令序列 done 语法2: while read -r line do 命令序列 done (切记while和左中括号一定要有空格) 例子 ...
- 服务器word权限添加
我们的程序部署到IIS上会出现上图所示的错误. 这个是由于IIS没有权限访问word所致. 解决的方法: 在运行对话框中输入: mmc -32 出来如下图的界面: 然后点击"文件&q ...