当前位置:网站首页>七月第一周
七月第一周
2022-07-07 21:50:00 【zjsru_Beginner】
骑士移动
题目描述:写程序,计算骑士从一个位置移动到另一个位置所需的最少移动次数。骑士移动的规则如下如所示。

每一次跳动,都可以有八种选择,经过多次正确选择后可以找到一个最近的路程。
算法设计:
这是一个求最短距离的问题,可以使用queue进行广度优先搜索步骤,现将起点放入队列,如果队列不空,则对头出对,否则扩展8个方向,如果找到目标,则立即返回步长+1并放入队列,标记其已访问,如果骑士的当前位置为(x,y),则移动时当前位置坐标加上偏移量即可。
输入:
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
输出:
5
28
0
代码:
#include<bits/stdc++.h>
int size ;
struct status{ #结构体
int x;
int y;
int step;
}q[100006];
int vec[8][2]={
{-2,-1},{-1,-2},{-2,1},{-1,2},{2,-1},{1,-2},{2,1},{1,2}}; #方向
int is[333][333];
void bfs(int sx,int sy,int ex,int ey){ #广度优先搜索
int head =0,tail=0;
memset(is,0,sizeof(is));
q[tail].x=sx;
q[tail].y=sy;
is[sx][sy]=1;
tail++;
while (head<tail){
if (q[head].x==ex&&q[head].y==ey){
printf("%d\n",q[head].step);
break ;
}
for(int i=0;i<8;i++){ #对各个方向进行遍历
int nx=q[head].x+vec[i][0];
int ny=q[head].y+vec[i][1];
int step=q[head].step;
if (nx>=0&&nx<size&&ny>=0&&ny<size&&is[nx][ny]==0){
q[tail].x=nx;
q[tail].y=ny;
q[tail].step=step+1;
is[nx][ny]=1;
tail++;
}
}
head ++;
}
}
int main(){
int k;
scanf("%d",&k);
int sx,sy,ex,ey;
while (k--){ #对每个要实现最短距离的目标进行遍历
scanf("%d",&size);
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
bfs(sx,sy,ex,ey);
}
return 0;
}
YJG
边栏推荐
- Unity与WebGL的相爱相杀
- Locate to the bottom [easy to understand]
- Database daily question --- day 22: last login
- Handling file exceptions
- 微信论坛交流小程序系统毕业设计毕设(4)开题报告
- Kubernetes' simplified data storage storageclass (creation, deletion and initial use)
- Installing vmtools is gray
- Software test classification
- GEE(三):计算两个波段间的相关系数与相应的p值
- Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框
猜你喜欢

微信论坛交流小程序系统毕业设计毕设(5)任务书

Sword finger offer 27 Image of binary tree

ArcGIS: two methods of attribute fusion of the same field of vector elements

Wechat forum exchange applet system graduation design completion (8) graduation design thesis template

Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~

Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020

Line test - graphic reasoning - 1 - Chinese character class

The author of LinkedList said he didn't use LinkedList himself

Unity与WebGL的相爱相杀

Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
随机推荐
Network security - phishing
定位到最底部[通俗易懂]
Talk about DART's null safety feature
What does the model number of asemi rectifier bridge kbpc1510 represent
Years of summary, some core suggestions for learning programming
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
Two kinds of curves in embedded audio development
肠道里的微生物和皮肤上的一样吗?
二叉树(Binary Tree)
小程序多种开发方式对比-跨端?低代码?原生?还是云开发?
Cases of agile innovation and transformation of consumer goods enterprises
Wechat forum exchange applet system graduation design (3) background function
Specific method example of V20 frequency converter manual automatic switching (local remote switching)
opencv scalar传入三个参数只能显示黑白灰问题解决
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to JSP-1
ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
Personal statement of testers from Shuangfei large factory: is education important for testers?
Unity与WebGL的相爱相杀
Brush question 5
微信论坛交流小程序系统毕业设计毕设(3)后台功能