当前位置:网站首页>2022牛客多校第三场 J题 Journey
2022牛客多校第三场 J题 Journey
2022-08-05 00:18:00 【Rain Sure】
题目链接
题目大意
给了我们一堆十字路口,问我们从一条走到另一条路消耗的最少的体力,每次向右转可以不消耗体力,其余方向均为消耗 1 1 1点体力。
题解
理解清楚题意后,直接双端队列广搜即可。
代码
#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_map会快超多!
#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 控制器更换过程

隐私计算综述

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

matlab中rcosdesign函数升余弦滚降成型滤波器

在线中文姓名生成工具推荐
![[idea] idea configures sql formatting](/img/89/98cd23aff3e2f15ecb489f8b3c50e9.png)
[idea] idea configures sql formatting

仿网易云音乐小程序-uniapp

How to automatically push my new articles to my fans (very simple, can't learn to hit me)

First, the basic concept of reptiles

三大技巧让你成功入门3D建模,零基础小白必看
随机推荐
tiup telemetry
动态上传jar包热部署
论文解读( AF-GCL)《Augmentation-Free Graph Contrastive Learning with Performance Guarantee》
建模师经验分享:模型学习方法
Modelers experience sharing: model study method
Cloud native - Kubernetes 】 【 scheduling constraints
看图识字,DELL SC4020 / SCv2000 控制器更换过程
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
图解 Canvas 入门
Couple Holding Hands [Greedy & Abstract]
2022牛客暑期多校训练营5(BCDFGHK)
D - I Hate Non-integer Number (选数的计数dp
tiup status
隐私计算综述
About I double-checked and reviewed the About staff page, returning an industry question
MVCC是什么
中日颜色风格
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
数据类型及输入输出初探(C语言)
what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......