当前位置:网站首页>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;
}
边栏推荐
- Three tips for you to successfully get started with 3D modeling
- 怎样进行在不改变主线程执行的时候,进行日志的记录
- gorm联表查询-实战
- redis可视化管理软件Redis Desktop Manager2022
- #yyds dry goods inventory #Switching equipment serious packet loss troubleshooting
- 【LeetCode】矩阵模拟相关题目汇总
- 软件测试面试题:网络七层协仪具体?
- 倒计时1天!8月2日—4日与你聊聊开源与就业那些事!
- tiup status
- 2022杭电多校第一场 1004 Ball
猜你喜欢

10 种常见的BUG分类

看图识字,DELL SC4020 / SCv2000 控制器更换过程
![[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots](/img/fa/5bdc81b1ebfc22d31f42da34427f3e.png)
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots

gorm joint table query - actual combat

电子行业MES管理系统的主要功能与用途

标识符、关键字、常量 和变量(C语言)

【idea】idea配置sql格式化

【LeetCode】Summary of Two Pointer Problems

STC89C52RC的P4口的应用问题

leetcode经典例题——单词拆分
随机推荐
tiup telemetry
软件测试面试题:软件测试类型都有哪些?
tiup telemetry
网站最终产品页使用单一入口还是多入口?
《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术
The master teaches you the 3D real-time character production process, the game modeling process sharing
ARC129E Yet Another Minimization 题解 【网络流笔记】
Mysql_12 多表查询
关于使用read table 语句
倒计时1天!8月2日—4日与你聊聊开源与就业那些事!
leetcode:267. 回文排列 II
2022牛客多校训练第二场 H题 Take the Elevator
TinyMCE禁用转义
论文解读( AF-GCL)《Augmentation-Free Graph Contrastive Learning with Performance Guarantee》
【云原生--Kubernetes】Pod控制器
gorm joint table query - actual combat
软件质量评估的通用模型
10 个关于 Promise 和 setTimeout 知识的面试题,通过图解一次说透彻
2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
【云原生--Kubernetes】调度约束