当前位置:网站首页>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;
}
边栏推荐
- 看图识字,DELL SC4020 / SCv2000 控制器更换过程
- tensor.nozero(), mask, [mask]
- jenkins send mail system configuration
- leetcode经典例题——单词拆分
- More than 2022 cattle school training topic Link with the second L Level Editor I
- leetcode: 267. Palindromic permutations II
- How to automatically push my new articles to my fans (very simple, can't learn to hit me)
- "No title"
- gorm joint table query - actual combat
- 【LeetCode】滑动窗口题解汇总
猜你喜欢

2 用D435i运行VINS-fusion

TinyMCE禁用转义

【论文笔记】—低照度图像增强—Unsupervised—EnlightenGAN—2019-TIP

【LeetCode】矩阵模拟相关题目汇总

what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......
![[Cloud Native--Kubernetes] Pod Controller](/img/e1/1a8cc82223f9a9be79ebbf1211e9a4.png)
[Cloud Native--Kubernetes] Pod Controller

Cloud native - Kubernetes 】 【 scheduling constraints

Metasploit-域名上线隐藏IP

软件质量评估的通用模型

Redis visual management software Redis Desktop Manager2022
随机推荐
leetcode: 269. The Martian Dictionary
tiup telemetry
机器学习(公式推导与代码实现)--sklearn机器学习库
.net (C#) get year month day between two dates
Metasploit-域名上线隐藏IP
tensor.nozero(), mask, [mask]
Software Testing Interview Questions: What's the Key to a Good Test Plan?
MongoDB permission verification is turned on and mongoose database configuration
10 个关于 Promise 和 setTimeout 知识的面试题,通过图解一次说透彻
2022杭电多校 第三场 B题 Boss Rush
TinyMCE disable escape
Essential knowledge for entry-level 3D game modelers
日志(logging模块)
"No title"
Detailed explanation of common DNS resource record types
电赛必备技能___定时ADC+DMA+串口通信
国内网站用香港服务器会被封吗?
【unity编译器扩展之模型动画拷贝】
克服项目管理中恐惧心理
【云原生--Kubernetes】调度约束