917:Knight Moves
题目链接:http://noi.openjudge.cn/ch0205/917/
原题应该是hdu 1372
- 总时间限制: 1000ms 内存限制: 65536kB
- 描述
- Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.

输入The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.输出For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.样例输入
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
样例输出
5
28
0
来源TUD Programming Contest 2001, Darmstadt, Germany
题目大意:
输入n表示有一个n*n的棋盘,输入开始坐标和结束坐标,问一个骑士朝着棋盘的8个方向走马字步,从起点到终点最少需要多少步。
首先输入T表示有T个测试样例,然后输入n表示棋盘规模,然后输出起点和终点的坐标(坐标从0开始到n-1)
输出马移动的最少步数,起点和终点相同则输出0.
算法分析:
这个题明显用广搜效率比较高。深搜的话,要搜索完所有路径然后才知道最优解。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std; struct obj
{
int xx,yy,step;
}; int T,n;
queue<struct obj> q;
struct obj S,E;
int used[][]; int dx[]={-,-,,,,,-,-};//???????????????????8????
int dy[]={,,,,-,-,-,-};
void BFS(); int main(int argc, char *argv[])
{
freopen("917.in","r",stdin);
scanf("%d",&T);
while(T)
{
T--;
scanf("%d",&n);
scanf("%d%d%d%d",&S.xx,&S.yy,&E.xx,&E.yy);
S.step=;
E.step=-; if(S.xx==E.xx&&S.yy==E.yy) printf("0\n");
else
{
memset(used,,sizeof(used));
BFS();
if(E.step==-) printf("no way!\n");
else printf("%d\n",E.step);
}
}
return ;
} void BFS()
{
int i,txx,tyy;
struct obj temp; while(!q.empty()) q.pop(); used[S.xx][S.yy]=;
q.push(S);
while(!q.empty())
{
for(i=;i<;i++)
{
txx=q.front().xx+dx[i];
tyy=q.front().yy+dy[i];
if(txx>=&&txx<n&&tyy>=&&tyy<n&&used[txx][tyy]==)
{
temp.xx=txx;
temp.yy=tyy;
temp.step=q.front().step+;
q.push(temp);
used[txx][tyy]=;
if(temp.xx==E.xx&&temp.yy==E.yy)
{
E.step=temp.step;
return;
}
}
}
q.pop();
}
}
917:Knight Moves的更多相关文章
- Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 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 ...
- UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...
- 【POJ 2243】Knight Moves
题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...
- hdu Knight Moves
这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...
- HDU 1372 (搜索方向稍有改变) Knight Moves
其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
随机推荐
- [转]Sqoop-1.4.4工具import和export使用详解
FROM :http://shiyanjun.cn/archives/624.html Sqoop可以在HDFS/Hive和关系型数据库之间进行数据的导入导出,其中主要使用了import和export ...
- STM32学习笔记之EXTI(外部中断)
參考资料:STM32数据手冊.网络资料 =========================================切割线==================================== ...
- solr建立pdf/word/excel索引的方法
PS: 本文假设你已经成功的搭建了一个Solr服务器步骤如下:(1)准备好一份Solr的源码,假设现在保存在c:\apache-solr-1.4.1\目录下(2)从https://issues.apa ...
- Windows server 2012 R2 与 Windows 2016 的双系统重启选项
一台主机上,同时安装了Windows 2012R2还有Windows 2016, 但是如何能在任意一个系统重启到另一个呢? 下图中,在Win2012R2中,无法选择重启到2016中. 解决方案 === ...
- ASP.NET MVC3 学习心得------路由机制
9.1 理解URL URL满足的要求: l 域名易于记忆和拼写 l 简短.易输入 l 可以反应出站点的结构 l 可破解,用户可以通过移除URL的末尾,进而达到更高层次的信息体系结构 l 持久.不能变化 ...
- Kalman滤波器从原理到实现
Kalman滤波器的历史渊源 We are like dwarfs on the shoulders of giants, by whose grace we see farther than the ...
- C++类中的访问权限问题
纠结的东西: private,public,protected方法的访问范围.(public继承下)private: 只能由该类中的函数.其友元函数访问,不能被任何其他访问,该类的对象也不能访问. p ...
- 【python】安装bcoding
C:\Users\horn1>pip install bcodingCollecting bcoding Downloading https://files.pythonhosted.org/p ...
- (纪录片)你必须知道的科学 The Science That You Have To Know (2014)
简介: 类型: 纪录片制片国家/地区: 英国语言: 英语上映日期: 2014-2片长: 150分钟 主要内容: 你有没有认真想过,自己的身体有多奇妙?人体是世间最复杂的一项工程杰作,他也带来了一些非常 ...
- Android自定义一款带进度条的精美按键
Android中自定义View并没有什么可怕的,拿到一个需要自定义的View,首先要做的就是把它肢解,然后思考每一步是怎样实现的,按分析的步骤一步一步的编码实现,最后你就会发现达到了你想要的效果.本文 ...