当前位置:网站首页>七月第一周
七月第一周
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
边栏推荐
- 【微服务|SCG】gateway整合sentinel
- This time, let's clear up: synchronous, asynchronous, blocking, non blocking
- OC variable parameter transfer
- ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值
- Grid
- Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框
- [language programming] exe virus code example
- 网络安全-burpsuit
- JMeter interface automated test read case, execute and write back result
- 数字藏品加速出圈,MarsNFT助力多元化文旅经济!
猜你喜欢

Wechat forum exchange applet system graduation design completion (7) Interim inspection report

成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚

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

Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2

肠道里的微生物和皮肤上的一样吗?

Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors

iNFTnews | NFT技术的广泛应用及其存在的问题

Wechat forum exchange applet system graduation design (5) assignment

Sword finger offer 55 - I. depth of binary tree

What is fake sharing after filling the previous hole?
随机推荐
Wechat forum exchange applet system graduation design completion (4) opening report
微信论坛交流小程序系统毕业设计毕设(4)开题报告
Mitsubishi PLC SLmP (MC) protocol
Years of summary, some core suggestions for learning programming
海内外技术人们“看”音视频技术的未来
Sword finger offer 27 Image of binary tree
What is fake sharing after filling the previous hole?
十四、数据库的导出和导入的两种方法
Why is network i/o blocked?
There is another problem just online... Warm
Brush question 5
Specific method example of V20 frequency converter manual automatic switching (local remote switching)
The author of LinkedList said he didn't use LinkedList himself
Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
Cases of agile innovation and transformation of consumer goods enterprises
About idea cannot find or load the main class
Brush question 6
Kubernetes' simplified data storage storageclass (creation, deletion and initial use)
leetcode-520. 检测大写字母-js
[language programming] exe virus code example