当前位置:网站首页>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;
}
边栏推荐
- 测试经理要不要做测试执行?
- 2022多校第二场 K题 Link with Bracket Sequence I
- node使用redis
- [230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
- 倒计时1天!8月2日—4日与你聊聊开源与就业那些事!
- node uses redis
- Couple Holding Hands [Greedy & Abstract]
- 阅读笔记:如何理解DevOps?
- 导入JankStats检测卡帧库遇到问题记录
- canvas 高斯模糊效果
猜你喜欢

【云原生--Kubernetes】Pod控制器

进程间通信和线程间通信

MAUI Blazor 权限经验分享 (定位,使用相机)

Three tips for you to successfully get started with 3D modeling

《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术

子连接中的参数传递

Will domestic websites use Hong Kong servers be blocked?

10 个关于 Promise 和 setTimeout 知识的面试题,通过图解一次说透彻

图解 Canvas 入门

QSunSync 七牛云文件同步工具,批量上传
随机推荐
刘润直播预告 | 顶级高手,如何创造财富
软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
NMS原理及其代码实现
【云原生--Kubernetes】Pod控制器
电子行业MES管理系统的主要功能与用途
2022杭电多校第三场 L题 Two Permutations
【LeetCode】滑动窗口题解汇总
【云原生--Kubernetes】调度约束
E - Many Operations (按位考虑 + dp思想记录操作后的结果
网站最终产品页使用单一入口还是多入口?
10 个关于 Promise 和 setTimeout 知识的面试题,通过图解一次说透彻
2022杭电多校第一场 1004 Ball
Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?
lua 如何 实现一个unity协程的工具
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
MAUI Blazor 权限经验分享 (定位,使用相机)
leetcode:267. 回文排列 II
SV 类的虚方法 多态
IDEA file encoding modification
2022 Niu Ke Summer Multi-School Training Camp 5 (BCDFGHK)