917:Knight Moves

简介: 题目链接:http://noi.openjudge.cn/ch0205/917/原题应该是hdu 1372总时间限制: 1000ms  内存限制: 65536kB描述BackgroundMr Somurolov, fabulous chess-gamer indeed, asserts ...

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

算法分析:

这个题明显用广搜效率比较高。深搜的话,要搜索完所有路径然后才知道最优解。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 
 7 struct obj
 8 {
 9     int xx,yy,step;
10 };
11 
12 int T,n;
13 queue<struct obj> q;
14 struct obj S,E;
15 int used[303][303];
16 
17 int dx[8]={-2,-1,1,2,2,1,-1,-2};//???????????????????8???? 
18 int dy[8]={1,2,2,1,-1,-2,-2,-1};
19 void BFS();
20 
21 int main(int argc, char *argv[])
22 {
23     freopen("917.in","r",stdin);
24     scanf("%d",&T);
25     while(T)
26     {
27         T--;
28         scanf("%d",&n);
29         scanf("%d%d%d%d",&S.xx,&S.yy,&E.xx,&E.yy);
30         S.step=0;
31         E.step=-1;
32         
33         if(S.xx==E.xx&&S.yy==E.yy) printf("0\n");
34         else 
35         {
36             memset(used,0,sizeof(used));
37             BFS();
38             if(E.step==-1) printf("no way!\n");
39             else printf("%d\n",E.step);
40         }
41     }
42     return 0;
43 }
44 
45 void BFS()
46 {
47     int i,txx,tyy;
48     struct obj temp;
49     
50     while(!q.empty()) q.pop();
51     
52     used[S.xx][S.yy]=1;
53     q.push(S);
54     while(!q.empty())
55     {
56         for(i=0;i<8;i++)
57         {
58             txx=q.front().xx+dx[i];
59             tyy=q.front().yy+dy[i];
60             if(txx>=0&&txx<n&&tyy>=0&&tyy<n&&used[txx][tyy]==0)
61             {
62                 temp.xx=txx;
63                 temp.yy=tyy;
64                 temp.step=q.front().step+1;
65                 q.push(temp);
66                 used[txx][tyy]=1;
67                 if(temp.xx==E.xx&&temp.yy==E.yy)
68                 {
69                     E.step=temp.step;
70                     return;
71                 }
72             }
73         }
74         q.pop();
75     }
76 }

 

相关文章
|
10月前
|
缓存 人工智能 程序员
CodeFuse「编码挑战季」:冲刺最后1个月!MelGeek磁轴键盘、Beats耳机等你来拿~
从1024程序员节起至12月底,CodeFuse「编码挑战季」火热进行中!参与muAgent、MFTCoder、ModelCache、CodeFuse-IDE四个项目的编码挑战,不仅能够深化对CodeFuse项目及开源社区的理解,还能赢取定制周边及高端奖品,如MelGeekMADE68 PRO磁轴键盘、Beats Studio Pro无线蓝牙耳机等。活动期间,开发者可根据任务难度获取积分,兑换丰富奖品。立即加入,让我们一起探索技术的无限可能!
168 11
|
Java API 微服务
Java微服务架构:原理与实践
【4月更文挑战第15天】本文介绍了Java微服务架构的原理和实践,包括服务拆分、注册与发现、API网关、配置中心和分布式链路追踪。重点提及Spring Boot和Spring Cloud作为开发工具,以及Docker和Kubernetes用于容器化和集群管理。Java微服务架构旨在应对大规模、复杂业务系统的挑战,提升系统可用性和可扩展性。
351 2
|
消息中间件 监控 数据挖掘
Elasticsearch 使用误区之二——频繁更新文档
【8月更文挑战第15天】在大数据与搜索技术日益成熟的今天,Elasticsearch 作为一款分布式、RESTful 风格的搜索与数据分析引擎,凭借其强大的全文搜索能力和可扩展性,成为了众多企业和开发者的首选。然而,在使用 Elasticsearch 的过程中,一些常见的误区可能会导致性能下降或数据不一致等问题,其中“频繁更新文档”便是一个不容忽视的误区。本文将深入探讨这一误区的根源、影响及解决方案,帮助读者更好地利用 Elasticsearch。2
388 0
VBA如何用Excel数据批量生成Word文档
VBA|用Excel数据批量生成并修改用模板创建的Word文档
|
存储 API 开发工具
CreateCollection API执行流程_milvus源码解析
CreateCollection API执行流程_milvus源码解析
381 0
|
存储 弹性计算 算法
倚天ECS加速国密算法性能
倚天ECS是阿里云基于平头哥自研数据中心芯片倚天710推出arm架构实例,采用armv9架构,支持SM3/SM4指令,可以加速国密算法性能。本文基于OpenSSL 3.2和Tongsuo 实测对比了倚天ECS g8y实例和Intel g7 实例国密性能。为用户选择ECS提供参考。
|
JavaScript 数据安全/隐私保护 前端开发
js中return,return true,return false三者的用法及区别
return其实就是return undefined; 1.语法及返回方式 ①返回控制与函数结果         语法为:return 表达式;         语句结果函数的执行,返回调用函数,而且把表达式的值作为函数结果返回出去 ②返回控制无函数结果         语法为:return;         在大多数情况下,为事件处理函数如果让其返回false,可以防止默认的事件行为.
1819 0
|
自然语言处理 API
CATIA二次开发—参数那点事
CATIA二次开发—参数那点事
CATIA二次开发—参数那点事
|
存储 运维 监控
阿里云原生Lindorm TSDB数据库,驱动工业IT&OT超融合数字化系统升级
阿里云 Lindorm 数据库面向工业场景的最佳实践案例。
875 0
阿里云原生Lindorm TSDB数据库,驱动工业IT&OT超融合数字化系统升级
|
前端开发
CSS - 行内元素的 padding、margin、width、height、line-height 是否无效?
CSS - 行内元素的 padding、margin、width、height、line-height 是否无效?
610 0
CSS - 行内元素的 padding、margin、width、height、line-height 是否无效?