当前位置:网站首页>2022 The Third J Question Journey
2022 The Third J Question Journey
2022-08-05 00:23:00 【Rain Sure】
题目链接
题目大意
Gave us a bunch of intersections,Ask us the least amount of stamina we use to walk from one route to the other,Every time you turn right, you don't need to consume stamina,All other directions are consumption 1 1 1点体力.
题解
理解清楚题意后,Direct double-ended queue search can be done.
代码
#include<iostream>
#include<cstring>
#include<queue>
#include<deque>
#include<map>
using namespace std;
const int maxn = 500010;
#define x first
#define y second
typedef pair<int,int> PII;
int n;
int sx, sy, ex, ey;
int w[maxn][4];
map<PII, int> dist;
typedef struct node
{
int x, y, d;
}Node;
int get_direction(int a, int b)
{
for(int i = 0; i < 4; i ++){
if(w[b][i] == a) {
return (i + 1) % 4;
}
}
return -1;
}
int bfs()
{
deque<Node> q;
q.push_back({
sx, sy, 0});
while(q.size())
{
auto t = q.front();
q.pop_front();
if(t.x == ex && t.y == ey) return t.d;
int d = get_direction(t.x, t.y);
for(int i = 0; i < 4; i ++){
int j = w[t.y][i];
if(j == 0) continue;
int v = 1;
if(i == d) v = 0;
if(!dist.count({
t.y, j}) || dist[{
t.y, j}] > t.d + v) {
dist[{
t.y, j}] = t.d + v;
if(v == 0) q.push_front({
t.y, j, dist[{
t.y, j}]});
else q.push_back({
t.y, j, dist[{
t.y, j}]});
}
}
}
return -1;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++){
for(int j = 0; j < 4; j ++){
scanf("%d", &w[i][j]);
}
}
scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
printf("%d\n", bfs());
return 0;
}
把map换成unordered_mapwill be much faster!
#include<iostream>
#include<cstring>
#include<queue>
#include<deque>
#include<map>
#include<unordered_map>
using namespace std;
const int maxn = 500010;
#define x first
#define y second
typedef pair<int,int> PII;
int n;
int sx, sy, ex, ey;
int w[maxn][4];
unordered_map<int, int> dist[maxn];
typedef struct node
{
int x, y, d;
}Node;
int get_direction(int a, int b)
{
for(int i = 0; i < 4; i ++){
if(w[b][i] == a) {
return (i + 1) % 4;
}
}
return -1;
}
int bfs()
{
deque<Node> q;
q.push_back({
sx, sy, 0});
while(q.size())
{
auto t = q.front();
q.pop_front();
if(t.x == ex && t.y == ey) return t.d;
int d = get_direction(t.x, t.y);
for(int i = 0; i < 4; i ++){
int j = w[t.y][i];
if(j == 0) continue;
int v = 1;
if(i == d) v = 0;
if(!dist[t.y].count(j) || dist[t.y][j]> t.d + v) {
dist[t.y][j] = t.d + v;
if(v == 0) q.push_front({
t.y, j, dist[t.y][j]});
else q.push_back({
t.y, j, dist[t.y][j]});
}
}
}
return -1;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; i ++){
for(int j = 0; j < 4; j ++){
scanf("%d", &w[i][j]);
}
}
scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
printf("%d\n", bfs());
return 0;
}
边栏推荐
- 图解 Canvas 入门
- 2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
- The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
- Flask框架 根据源码分析可扩展点
- Couple Holding Hands [Greedy & Abstract]
- Mysql_13 事务
- 2022牛客多校训练第二场 L题 Link with Level Editor I
- 【Valentine's Day special effects】--Canvas realizes full screen love
- 2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
- leetcode:269. 火星词典
猜你喜欢
Essential knowledge for entry-level 3D game modelers
redis可视化管理软件Redis Desktop Manager2022
leetcode:266. 回文全排列
10 种常见的BUG分类
数据类型-整型(C语言)
电赛必备技能___定时ADC+DMA+串口通信
在线中文姓名生成工具推荐
【LeetCode】双指针题解汇总
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
QSunSync Qiniu cloud file synchronization tool, batch upload
随机推荐
What is next-generation modeling (with learning materials)
RK3399平台开发系列讲解(内核调试篇)2.50、嵌入式产品启动速度优化
oracle创建用户以后的权限问题
tiup status
【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP
软件测试面试题:关于自动化测试工具?
软件测试面试题:LoadRunner 分为哪三个模块?
2022杭电多校训练第三场 1009 Package Delivery
2022杭电多校第三场 L题 Two Permutations
KT148A voice chip ic working principle and internal architecture description of the chip
The master teaches you the 3D real-time character production process, the game modeling process sharing
MongoDB permission verification is turned on and mongoose database configuration
【LeetCode】滑动窗口题解汇总
LeetCode Hot 100
Handwritten Distributed Configuration Center (1)
电子行业MES管理系统的主要功能与用途
oracle创建表空间
【LeetCode】矩阵模拟相关题目汇总
#yyds dry goods inventory #Switching equipment serious packet loss troubleshooting
"No title"