台州2481 && POJ 1915 Knight Moves

917:Knight Moves(翻译) 描述 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? S先生是一位出名的国际象棋选手,他断言没人能比他更快地将一个骑士移向另一位置,你能打败他吗?The Problem Your task is to write a program t 阅读详情
Knight Moves
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 18304 Accepted: 8386

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

Source

TUD Programming Contest 2001, Darmstadt, Germany


直接BFS


#include<stdio.h>

int st_x,st_y,la_x,la_y,n;
int flag[301][301];
int Stack[90001][2];
int con[90001];
int ss[8][2]={ 1, 2,
               1,-2,
              -1, 2,
              -1,-2,
               2, 1,
               2,-1,
              -2, 1,
              -2,-1};

int is_(int x)
{
    if(x>=0 && x<n)
        return 1;
    return 0;
}

int bfs()
{
    int base=0,top=1;
    Stack[0][0]=st_x;
    Stack[0][1]=st_y;
    flag[st_x][st_y]=1;
    con[0]=0;
    while(base<top)
    {
        int i;
        if(Stack[base][0]==la_x && Stack[base][1]==la_y)
            return con[base];
        for(i=0;i<8;i++)
        {
            int a=Stack[base][0]+ss[i][0];
            int b=Stack[base][1]+ss[i][1];
            if( is_(a) && is_(b) && !flag[a][b])
            {
                flag[a][b]=1;
                con[top]=con[base]+1;
                Stack[top][0]=a;
                Stack[top++][1]=b;
               
            }
        }
        base++;
    }
    return -1;
}


int main(void)
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int i,j;
        scanf("%d",&n);
        scanf("%d%d%d%d",&st_x,&st_y,&la_x,&la_y);
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
                flag[i][j]=0;
        printf("%d\n",bfs());
    }
    return 0;
}




POJ - 1915 象棋中马的走法(BFS) 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 prog... 阅读详情

相关推荐

NOI2.5.917 Knight Moves题解(C++)

NOI2.5.917 Knight Moves题解(C++) 题目 背景 确实如此出色的国际象棋博弈者索穆罗洛夫先生断言,除了他以外,没有其他人可以如此快速地将骑士从一个位置转移到另一个位置。你能打败他吗? 问题 您的任务是编写一个程序,计算一个骑士从一个点到另一个点所需要的最少移动次数,以便您有机会比Somurolov更快。 对于不熟悉国际象棋的人,可能的骑士动作如图1所示。 输入值 输入以单...

jerry20183的博客 1266

POJ 1915 Knight Moves【广搜】

Description BackgroundMr 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 ProblemYour task is to wr...

weixin_33971205的博客 527

汇川H5U搭配步进伺服电机使用CANopen通讯控制电机案例

摘要:本文介绍了基于汇川H5U PLC和STM4260B-CANopen-M步进伺服电机的控制系统搭建流程。主要内容包括:硬件准备(PLC、伺服电机、24V电源)、电机参数默认设置(地址1号、1M波特率)、使用AutoShop软件编写程序(配置CANopen参数、导入电机描述文件、设置PDO参数)以及功能块编程(使能控制、速度控制、绝对位置控制等)。通过查阅通讯手册完成各功能块参数配置,最终在主程序中调用功能块实现控制系统开发。该方案适用于需要精确运动控制的工业自动化场景。

m0_46512607的博客 1596

POJ 1915 Knight Moves

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18211   Accepted: 8335 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts

闲暇时光写代码 868

Stata森林图进阶:个性化定制与高级技巧

本文详细介绍了Stata森林图的进阶技巧,包括个性化定制与高级应用。通过meta分析工具如metan命令,用户可以优化字体、颜色、坐标轴等视觉元素,提升森林图的专业性和可读性。文章还提供了黑白印刷设置、高分辨率导出等实用技巧,帮助科研人员制作符合学术出版要求的森林图。

ios99的博客 254

【Anaconda】解决“libGL error: MESA-LOADER: failed to open iris”问题

stackoverflow中的“libGL error: MESA-LOADER: failed to open iris”问题。而在自己创建的conda环境中的lib下出现`GLIBCXX_3.4.30’ not found 的情况。在上述解决方案中,将第一行命令的lib路径换成自己创建的conda环境lib路径,运行指令,问题解决。目前问题解决,总结一下查找资料以及解决方法。运行如下命令,发现本地支持。

quintus0505的博客 5024

Minimum Knight Moves

In aninfinitechess board with coordinates from-infinityto+infinity, you have aknightat square[0, 0]. A knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogona...

leetcode 解题思路 580

【PyTorch】CNN(一维卷积Conv1D)实现时间序列预测

本文介绍了一维卷积神经网络(Conv1D)在时间序列预测中的应用,展示了其基本原理及实现方式。通过合适的数据预处理和模型调参,Conv1D可以有效地捕捉时间序列数据中的模式,从而进行精准预测。

走向CTO的路上... 1173

骑士的移动 Knight Moves

输入8*8的国际象棋棋盘上的2个格子(列:a~h,行:1~8),求马至少多少步从起点(键盘输入的第一个位置)跳到终点(键盘输入的第二个位置)。

Xiao_juice的博客 1012

27、振动信号实时并行处理与风力发电机模型鲁棒控制设计

本文探讨了振动信号实时并行处理与风力发电机模型鲁棒控制设计两大核心技术。在振动信号处理方面,提出基于FPGA、RT和PC三级架构的并行采集与处理系统,实现高效、实时的振动分析,适用于电机状态监测与预测性维护。在风力发电控制方面,研究风力农场布局优化(WFLO)问题,比较九种多目标元启发式算法性能,并提出基于扰动观察法的最大功率点跟踪(MPPT)控制策略,提升发电效率与系统鲁棒性。研究成果为工业设备智能监控与可再生能源优化提供了有效技术路径。

sunny的博客 72

深度学习-161-Dify工具之对比使用工作流和聊天流生成图表可视化的html文件

分别使用聊天流和工作流进行数据分析结果的可视化,生成html文件。聊天流可以配置带记忆,这样就可以多次交互优化结果。

qq_20466211的博客 1709

Knight Moves

题目描述 原题来自:POJ 1915 编写一个程序,计算一个骑士从棋盘上的一个格子到另一个格子所需的最小步数。骑士一步可以移动到的位置由下图给出。 输入 第一行给出骑士的数量n。 在接下来的3n行中,每3行描述了一个骑士。其中, 第一行一个整数L表示棋盘的大小,整个棋盘大小为L×L; 第二行和第三行分别包含一对整数(x,y),表示骑士的起始点和终点。假设对于每一个骑士,起...

算法小猪的博客 5041

Python库CloudScraper详细使用(绕过 Cloudflare 的反机器人页面的 Python 模块)

CloudScraper是一个专门用于绕过Cloudflare反机器人保护的Python库,它基于requests库构建,能够处理Cloudflare的5秒盾、WAF和人机验证等防护机制。

数据知道的博客 1万+

1.24英文题面翻译

描述 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 f

qq_64149314的博客 397

如何在 Mac 上下载安装 Xcode——并为 iOS 开发更新它原文

下载 Xcode安装命令行工具打开新版本删除文件注意,我在下面的步骤中列出了一些终端命令。这些命令可以被输入到你现在的工作目录中。这意味着你不需要导航到任何特定的文件夹。如果你真的想这样做,你可以在输入下面的步骤中的命令之前先输入== cd==。这将使你返回到主文件夹。

专注于AI领域前沿知识,业余爱好开发软件 4667

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

poj1915--Knight Moves(初级BFS)

Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15940 Accepted: 7144 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but

牧野之歌 448

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

poj1915骑士移动

骑士移动

qq_36335827的博客 435

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

bfs模板

最小步数模板题: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

Dave_lzw的博客 652

Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from on

Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from on

m0_62282806的博客 275

【1372】Knight Moves

1372 标准的bfs做法,注意这个棋是走“日”字 #include<bits/stdc++.h> using namespace std; //这题需要注意棋子走法 int t[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1}; int mep[15][15],X,Y; char s1[10],s2[10]; struct node{ int x,y,step; }; int check(int a,int b){//是否需要访问 if(

weixin_49646622的博客 166

【BFS】Knight Moves(C++)

Description 贝茜和她的表妹在玩一个简化版的国际象棋。棋盘如图所示: 贝茜和表妹各有一颗棋子。棋子每次移一步,且棋子只能往如图所示的八个方向移动。比赛的规则很简单,两个人需要从起点将棋子移到终点,谁能花最少的步数从起点走到终点,就是赢家。为了确保能赢表妹,贝茜希望每次都能算出最少的步数,你能帮助她么 Input 输入起点和终点,用一个空格隔开。(确保起点一定能走到终点) Output ...

liuzich的博客 1228

poj 1915 Knight Moves

poj   1915   Knight Moves                     题目链接:http://poj.org/problem?id=1915 题目大意:象棋里马走日,此题中给出棋盘大小,给出马的起止位置,问马跳过去最少要跳多少步。 题目分析:简单BFS,与以前稍有不同之处在dir数组。 code: #include #include #include usin

slicer的技术交流栈 821

T - 20

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 ta

lml11111的博客 528

信息学奥赛一本通 1257:Knight Moves 广度优先搜索算法

输入n代表有个n×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步。首先输入一个n,表示测试样例的个数。每个测试样例有三行。第一行是棋盘的大小L(4≤L≤300);第二行和第三行分别表示马的起始位置和目标位置(0..L−1)。马移动的最小步数,起始位置和目标位置相同时输出00。 3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1 【输出样例】

长春高老师编程 1311

poj1915(Knight Moves)_BFS_Java(已AC)

poj1915(Knight Moves)_BFS_Java poj1915代码Java版,使用广度优先遍历

weixin_44995274的博客 235
上一篇: Hdu 1556 Color the ball
下一篇: Hdu 1372 Knight Moves
Magic_____
博客等级 码龄14年 11粉丝 160原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值