当前位置:网站首页>七月第一周
七月第一周
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
边栏推荐
- It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
- Innovation today | five key elements for enterprises to promote innovation
- QT graphicsview graphical view usage summary with flow chart development case prototype
- Two minutes, talk about some wrong understandings of MySQL index
- Software evaluation center ▏ what are the basic processes and precautions for automated testing?
- Cause analysis and solution of too laggy page of [test interview questions]
- Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2
- Wechat forum exchange applet system graduation design completion (6) opening defense ppt
- JS triangle
- 嵌入式音频开发中的两种曲线
猜你喜欢
The wonderful relationship between message queue and express cabinet
Introduction to redis and jedis and redis things
Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~
leetcode-520. 检测大写字母-js
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2
消息队列与快递柜之间妙不可言的关系
十四、数据库的导出和导入的两种方法
V20变频器手自动切换(就地远程切换)的具体方法示例
Unity and webgl love each other
聊聊 Dart 的空安全 (null safety) 特性
随机推荐
微信论坛交流小程序系统毕业设计毕设(5)任务书
Exploratory data analysis of heartbeat signal
消费品企业敏捷创新转型案例
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
Brush question 5
微生物健康網,如何恢複微生物群落
PMP项目管理考试过关口诀-1
./ setup. Insufficient sh permission
2022 words for yourself
Software evaluation center ▏ what are the basic processes and precautions for automated testing?
Cause analysis and solution of too laggy page of [test interview questions]
肠道里的微生物和皮肤上的一样吗?
网络安全-安装CentOS
Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~
Network security CSRF
海内外技术人们“看”音视频技术的未来
Transparent i/o model from beginning to end
GEE(三):计算两个波段间的相关系数与相应的p值
Handling file exceptions