POJ 1915 Knight Moves(BFS+STL)

简介:

                          Knight Moves
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions:20913   Accepted: 9702

Description

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.

                      

Input

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.

Output

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.

Sample Input

3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1

Sample Output

5
28
0

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define maxn 350
using namespace std;

int n;
int x1,x2,y1,y2;


struct node{
    int x;
    int y;
    int sum;
    node(int a,int b,int c)
    {
        x=a;
        y=b;
        sum=c;
    }

};

int go[8][2]={{1,2},{1,-2},{2,1},{2,-1},{-1,2},{-1,-2},{-2,1},{-2,-1}};
int vis[310][310];

int BFS()
{
    queue<node> que;
    memset(vis,0,sizeof vis);
    que.push(node(x1,y1,0));
    vis[x1][y1]=1;
    while(!que.empty())
    {
        node temp=que.front();
        for(int i=0;i<8;i++)
        {
            int x0=temp.x+go[i][0];
            int y0=temp.y+go[i][1];
            int sum0=temp.sum+1;
            if(x0>=0&&x0<n&&y0>=0&&y0<n)
            {
                if(x0==x2&&y0==y2) return sum0;
                else if(!vis[x0][y0])
                {
                    que.push(node(x0,y0,sum0));
                    vis[x0][y0]=1;
                }
            }
        }
        que.pop();
    }

    return 0;

}



int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d%d",&n,&x1,&y1,&x2,&y2);

        if(x1==x2&&y1==y2)
            printf("0\n");
        else
            printf("%d\n",BFS());
    }

    return 0;

}


版权声明:本文博客原创文章,博客,未经同意,不得转载。







本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4751873.html,如需转载请自行联系原作者


相关文章
|
SQL 前端开发 JavaScript
基于java+springboot的求职招聘网站-求职招聘管理系统
该系统是基于java+springboot开发的求职招聘网站、网上招聘管理系统、网上人才招聘系统、毕业生求职招聘系统、大学生求职招聘系统、校园招聘系统、企业招聘系统。是给师弟开发的毕业设计。
495 1
|
存储 人工智能 OLAP
LangChain+通义千问+AnalyticDB向量引擎保姆级教程
本文以构建AIGC落地应用ChatBot和构建AI Agent为例,从代码级别详细分享AI框架LangChain、阿里云通义大模型和AnalyticDB向量引擎的开发经验和最佳实践,给大家快速落地AIGC应用提供参考。
131530 94
|
8月前
|
人工智能 自然语言处理 API
UI-TARS:字节跳动开源专注于多平台 GUI 自动化交互的视觉语言模型
UI-TARS 是字节跳动推出的新一代原生图形用户界面(GUI)代理模型,支持跨平台自动化交互,具备强大的感知、推理、行动和记忆能力,能够通过自然语言指令完成复杂任务。
2277 16
UI-TARS:字节跳动开源专注于多平台 GUI 自动化交互的视觉语言模型
|
机器学习/深度学习 算法 图形学
华为、腾讯开源AniPortrait:用音频、图片生成会说话的视频
【7月更文挑战第17天】华为腾讯联合开源AniPortrait,技术利用音频和图片生成栩栩如生的说话视频。通过音频分析面部表情,结合扩散模型与运动模块创建2D动画,实现自然的肖像动效。虽有高质量表现,但尚处研究阶段,面临隐私、伦理及应用局限性挑战。[论文链接](https://arxiv.org/abs/2403.17694)**
259 5
|
8月前
|
存储 缓存 分布式计算
Checkpoint 和持久化机制的区别?
Checkpoint 和持久化机制是分布式计算中的重要概念。Checkpoint 定期保存应用状态,用于故障恢复,特点是定期保存、状态恢复和一定的性能开销,广泛应用于流处理系统。持久化机制将数据从内存保存到磁盘等持久存储,确保数据在系统重启或故障后可用,特点是实时保存、数据持久性和较大的性能开销,常见于数据库系统。两者主要区别在于目的(故障恢复 vs 数据持久性)、频率(低频 vs 高频)和数据范围(中间状态 vs 最终结果)。
|
9月前
|
存储 云安全 安全
云标准:云计算标准
云计算标准涵盖基础设施、服务、安全、应用等多个方面,旨在提高系统的互操作性、可靠性和安全性。标准分为国际、区域、国家、行业和企业五个层次,由中国ISO、TC260、TC28等组织制定,如GB/T 29194-2012、GB/T 31168-2014等,为云计算技术的发展提供支持。
542 5
|
10月前
|
机器学习/深度学习 人工智能 运维
智能化运维:提升IT服务效率的新引擎###
本文深入浅出地探讨了智能化运维(AIOps)如何革新传统IT运维模式,通过大数据、机器学习与自动化技术,实现故障预警、快速定位与处理,从而显著提升IT服务的稳定性和效率。不同于传统运维依赖人工响应,AIOps强调预测性维护与自动化流程,为企业数字化转型提供强有力的支撑。 ###
|
存储 算法
图解Kmp算法——配图详解(超级详细)
图解Kmp算法——配图详解(超级详细)
|
11月前
|
Linux Python
Python获得本机本地ip地址的方法
【10月更文挑战第8天】 socket模块包含了丰富的函数和方法,可以获取主机的ip地址,例如gethostbyname方法可以根据主机名获取ip地址,gethostbyname_ex方法可以获得本机所有ip地址列表,也可以使用netifaces模块获取网卡信息。
283 0
|
监控 安全 Linux
在Linux中,如何查看和审计系统日志文件以检测异常活动?
在Linux中,如何查看和审计系统日志文件以检测异常活动?