当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

leetcode: 266. All Palindromic Permutations

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

Mysql_14 存储引擎

could not build server_names_hash, you should increase server_names_hash_bucket_size: 32

怎样进行在不改变主线程执行的时候,进行日志的记录
![[idea] idea configures sql formatting](/img/89/98cd23aff3e2f15ecb489f8b3c50e9.png)
[idea] idea configures sql formatting

仿网易云音乐小程序-uniapp

测试经理要不要做测试执行?

Cloud native - Kubernetes 】 【 scheduling constraints
![Couple Holding Hands [Greedy & Abstract]](/img/7d/1cafc000dc58f1c5e2e92150be7953.png)
Couple Holding Hands [Greedy & Abstract]
随机推荐
看图识字,DELL SC4020 / SCv2000 控制器更换过程
About I double-checked and reviewed the About staff page, returning an industry question
【Unity编译器扩展之进度条】
日志(logging模块)
克服项目管理中恐惧心理
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?
2022 Niu Ke Summer Multi-School Training Camp 5 (BCDFGHK)
tiup status
gorm联表查询-实战
2022杭电多校第三场 L题 Two Permutations
软件测试面试题:测试生命周期,测试过程分为几个阶段,以及各阶段的含义及使用的方法?
leetcode:266. 回文全排列
【idea】idea配置sql格式化
LeetCode Hot 100
leetcode经典例题——单词拆分
tiup uninstall
[idea] idea configures sql formatting
Redis visual management software Redis Desktop Manager2022
软件测试面试题:系统测试的策略有?
10 种常见的BUG分类