首页 技术 正文
技术 2022年11月15日
0 收藏 911 点赞 2,712 浏览 2308 个字

题目链接: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.

917:Knight Moves

输入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();
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919