当前位置:网站首页>-骑士巡游-
-骑士巡游-
2022-08-09 23:46:00 【-JMY-】
题目描述
马在中国象棋以日字形规则移动,给定n*m大小的棋盘,以及马的初始位置(x,y)和目标位置(s,t),要求不能重复经过棋盘上的同一个点,计算马至少走多少步可以到达目标位置,所有棋盘保证从初始位置到结束位置一定有路径可达。
输入
测试数据包含一行,为六个整数,分别为棋盘的大小以及初始位置坐标nmxyst。(1≤x,s≤n≤5,1≤y,t≤m≤5)
输出
包含一行,为一个整数,表示马能到达目标位置的最小步数。
样例输入
3 3 1 1 1 3
样例输出
2
参考代码:
#include<bits/stdc++.h>
using namespace std;
int n,m;
int q[5000][2],x,y,s,t,hh,tt,kx,ky,l,gx,gy;
int dx[]={1,1,-1,-1,2,2,-2,-2};
int dy[]={2,-2,2,-2,1,-1,1,-1};
int d[50][50];
void bfs(){
while(hh!=tt){
kx=q[hh][1];
ky=q[hh][0];
hh++;
for(int i=0;i<8;i++){
int xx=kx+dx[i];
int yy=ky+dy[i];
if(xx>=0&&xx<m&&yy>=0&&yy<n&&d[yy][xx]==0){
tt++;
q[tt-1][0]=yy;
q[tt-1][1]=xx;
d[yy][xx]=d[ky][kx]+1;
}
}
}
return;
}
int main(){
cin>>n>>m>>y>>x>>t>>s;
tt++;
d[y][x]=1;
q[0][1]=x-1;
q[0][0]=y-1;
bfs();
cout<<d[t-1][s-1];
return 0;
}
边栏推荐
猜你喜欢
手把手教你编写性能测试用例
ECCV 2022 | Microsoft Open Source TinyViT: Pre-training Capabilities for Small Models
安全知识培训——消防安全
[C language] Address book "Static Memory Version"
labelme标注的json标签转txt格式
Redis 非关系型数据库学习(一) ---- Redis 的安装
selenium和驱动安装
Digital wallets, red sea ecological rapid introduction of small programs can help capture device entry wisdom
线程的同步与互斥
openEuler 知:abi 检测
随机推荐
Pinduoduo store operation must know to leave a little knowledge of operation
NTU General Database-Gbase-8a-Learning-04-Deploying Distributed Clusters
router路由
const修饰指针的三种情况
ES6 Beginner to Mastery #15: Generator Usage
Leetcode83. 删除排序链表中的重复元素
安全知识培训——消防安全
deepstream学习笔记(三):deepstream-imagedata-multistream解析与接入适配yolov5模型测试
LSTM-based distributed energy generation prediction (Matlab code implementation)
收银管理软件如何做好员工管理?
构建平衡二叉树「建议收藏」
dlopen failed: library "libtaml.so" not found
数字孪生智慧制造生产线项目实施方案,平台认知与概念
Redis redisTemplate.execute 执行锁
GoLang 使用 goroutine 停止的几种办法
Spark基础【RDD单Value类型转换算子】
YOLOV5 study notes (7) - training your own data set
The older tester has just passed the "hurdle" of being 35 years old, and I want to tell you something from my heart
Fury:一个基于JIT动态编译的高性能多语言原生序列化框架
mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)