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月前
|
Web App开发 安全 数据建模
四个步骤,教会你怎么选择SSL证书
SSL证书是保护网站数据安全的核心工具,只需四步匹配适合的证书:1. 明确需求场景,选择DV、OV或EV证书;2. 确认域名覆盖范围,选单域名、通配符或多域名证书;3. 选择可信CA机构,确保浏览器兼容性;4. 对比价格与服务,考虑售后和技术支持。
|
Java API 微服务
Java微服务架构:原理与实践
【4月更文挑战第15天】本文介绍了Java微服务架构的原理和实践,包括服务拆分、注册与发现、API网关、配置中心和分布式链路追踪。重点提及Spring Boot和Spring Cloud作为开发工具,以及Docker和Kubernetes用于容器化和集群管理。Java微服务架构旨在应对大规模、复杂业务系统的挑战,提升系统可用性和可扩展性。
421 2
|
消息中间件 监控 数据挖掘
Elasticsearch 使用误区之二——频繁更新文档
【8月更文挑战第15天】在大数据与搜索技术日益成熟的今天,Elasticsearch 作为一款分布式、RESTful 风格的搜索与数据分析引擎,凭借其强大的全文搜索能力和可扩展性,成为了众多企业和开发者的首选。然而,在使用 Elasticsearch 的过程中,一些常见的误区可能会导致性能下降或数据不一致等问题,其中“频繁更新文档”便是一个不容忽视的误区。本文将深入探讨这一误区的根源、影响及解决方案,帮助读者更好地利用 Elasticsearch。2
476 0
|
存储 JSON 前端开发
了解什么是JWT的原理及实际应用
了解什么是JWT的原理及实际应用
1570 0
R语言EG(Engle-Granger)两步法协整检验、RESET、格兰杰因果检验、VAR模型分析CPI和PPI时间序列关系
R语言EG(Engle-Granger)两步法协整检验、RESET、格兰杰因果检验、VAR模型分析CPI和PPI时间序列关系
|
消息中间件 分布式计算 大数据
【大数据技术】Spark+Flume+Kafka实现商品实时交易数据统计分析实战(附源码)
【大数据技术】Spark+Flume+Kafka实现商品实时交易数据统计分析实战(附源码)
572 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,可以防止默认的事件行为.
1900 0
|
存储 API 开发工具
CreateCollection API执行流程_milvus源码解析
CreateCollection API执行流程_milvus源码解析
451 0
|
存储 安全 数据管理
[读书][笔记]WINDOWS PE权威指南《三》PE的原理和基础 之 第三章 PE文件头(上)
[读书][笔记]WINDOWS PE权威指南《三》PE的原理和基础 之 第三章 PE文件头
224 0

热门文章

最新文章