当前位置:网站首页>信息学奥赛一本通(1257:Knight Moves)
信息学奥赛一本通(1257:Knight Moves)
2022-08-02 20:02:00 【橙子教师】
1257:Knight Moves
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 6149 通过数: 3100
【题目描述】
输入nn代表有个n×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步。
【输入】
首先输入一个nn,表示测试样例的个数。
每个测试样例有三行。
第一行是棋盘的大小L(4≤L≤300);
第二行和第三行分别表示马的起始位置和目标位置(0..L−1)。
【输出】
马移动的最小步数,起始位置和目标位置相同时输出00。
【输入样例】
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
【输出样例】
5
28
0
【分析】
马走日,8个方向,方向数组(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)。
【参考代码】
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N=310;
struct node
{
int x,y;
int t;
};
int sx,sy; //起点坐标
int ex,ey; //终点坐标
int l; //棋盘大小
int dx[]={1,1,-1,-1,2,2,-2,-2}; //方向数组
int dy[]={2,-2,2,-2,1,-1,1,-1};
bool vis[N][N]; //访问数组
void bfs()
{
queue <node> q; //申请队列
node st;
st.x=sx;
st.y=sy;
st.t=0;
q.push(st); //起点入队
while(!q.empty())
{
node nt=q.front();
if(nt.x==ex && nt.y==ey)
{
cout<<nt.t<<endl;
break;
}
for(int i=0;i<8;i++)
{
int nx=nt.x+dx[i];
int ny=nt.y+dy[i];
if(vis[nx][ny]==false && nx>=0 && nx<l && ny>=0 && ny<l)
{
vis[nx][ny]=true;
node tmp;
tmp.x=nx;
tmp.y=ny;
tmp.t=nt.t+1;
q.push(tmp);
}
}
q.pop();
}
return;
}
int main()
{
int t;
cin>>t;
while(t--)
{
memset(vis,false,sizeof(vis));
cin>>l;
cin>>sx>>sy>>ex>>ey;
vis[sx][sy]=true;
bfs();
}
}
边栏推荐
- Translate My Wonderful | July Moli Translation Program Winners Announced
- Thread线程类基本使用(下)
- 解析List接口中的常用的被实现子类重写的方法
- LeetCode - 105. 从前序与中序遍历序列构造二叉树;023.合并K个升序链表
- Implement fashion_minst clothing image classification
- Parse common methods in the Collection interface that are overridden by subclasses
- J9数字论:互联网跨链桥有什么作用呢?
- Wintun:一款惊艳的 WireGuard 虚拟网卡接口驱动
- AI Scientist: Automatically discover hidden state variables of physical systems
- The so-called fighting skill again gao also afraid of the chopper - partition, depots, table, and the merits of the distributed
猜你喜欢
扫码预约 | 观看Apache Linkis数据处理实践以及计算治理能力
特拉维夫大学 | Efficient Long-Text Understanding with Short-Text Models(使用短文本模型进行高效的长文本理解)
如何解决图像分类中的类别不均衡问题?不妨试试分开学习表征和分类器
SCANIA SCANIA OTL tag is introduced
You want the metagenomics - microbiome knowledge in all the (2022.8)
LeetCode 622 设计循环队列[数组 队列] HERODING的LeetCode之路
idea 配置resin
4KMILES加入艾盛集团,以更强劲的数字商务能力,加速中国跨境电商的全域全效增长
LeetCode:622. 设计循环队列【模拟循环队列】
程序员也许都缺一个“二舅”精神
随机推荐
分布式事务
解析List接口中的常用的被实现子类重写的方法
Caldera(一)配置完成的虚拟机镜像及admin身份简单使用
LM小型可编程控制器软件(基于CoDeSys)笔记二十五:plc的数据存储区(数字量输入通道部分)
Translate My Wonderful | July Moli Translation Program Winners Announced
【Psychology · Characters】Issue 1
Likou Question of the Day - Day 46 - 344. Reverse Strings
美国爱荷华州立大学| Improving Distantly Supervised Relation Extraction by Natural Language Inference(通过自然语言推理改进远程监督关系提取)
SQL Server数据类型转换函数cast()和convert()详解
软考 ----- UML设计与分析(下)
Flutter with internationalized adapter automatically generated
[安洵杯 2019]easy_web
【LeetCode】1374. 生成每种字符都是奇数个的字符串
新增指令 v-memo
AI Scientist: Automatically discover hidden state variables of physical systems
ALV report learning summary
ALV概念讲解
Qt提升自定义控件,找不到头文件
LeetCode - 105. 从前序与中序遍历序列构造二叉树;023.合并K个升序链表
shell:条件语句