当前位置:网站首页>-骑士巡游-
-骑士巡游-
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;
}
边栏推荐
- Pinduoduo store operation must know to leave a little knowledge of operation
- 博弈小游戏
- 《痞子衡嵌入式半月刊》 第 60 期
- 深度剖析 Apache EventMesh 云原生分布式事件驱动架构
- Description of AirFlow
- 基于 LSTM 的分布式能源发电预测(Matlab代码实现)
- [SSH]如何敲一条线
- WPF DataGrid using data templates
- mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)
- 数字孪生电力系统,可视化应用实现科学调度的电子设备
猜你喜欢
随机推荐
Golden Warehouse Database KingbaseGIS User Manual (6.4. Geometry Object Access Function)
Wireshark classic practice and interview 13-point summary
C language learning journey [operator (incomplete version)]
router路由
Linux安装Oracle和postgrepSQL数据库
【问题解决】训练和验证准确率很高,但测试准确率很低
深度剖析 Apache EventMesh 云原生分布式事件驱动架构
arm-4-裸板开发
数字孪生电力系统,可视化应用实现科学调度的电子设备
Creo5.0 introductory tutorial free material
Distributed database problem (2): data replication
New window Display Agreement
重估HR SaaS:一体化后的新三年
Stanford CS143 速通PA1教程
nfs配置
Leetcode82. 删除排序链表中的重复元素 II
【数据存储】signed,unsigned到底怎么区分?如何计算?
Distributed database problem (3): data consistency
Golden Warehouse Database KingbaseGIS User Manual (6.5. Geometry Object Editing Function)
【obs】obsqsv11 硬编 及与metartc codec对比


![MATLB|And her ups and downs and finally reached the peak of life [Romantic Journey]](/img/10/55cc34a929768d89d9ffde8a5b3419.png)






