当前位置:网站首页>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经典例题——单词拆分
- 软件测试面试题:设计测试用例时应该考虑哪些方面,即不同的测试用例针对那些方面进行测试?
- "No title"
- 软件测试面试题:软件测试类型都有哪些?
- 关于使用read table 语句
- jenkins send mail system configuration
- Mysql_12 多表查询
- 2022杭电多校第一场 1004 Ball
- How to automatically push my new articles to my fans (very simple, can't learn to hit me)
- The applicable scenarios and common product types of the KT148A electronic voice chip ic solution
猜你喜欢
典型相关分析CCA计算过程
Handwritten Distributed Configuration Center (1)
redis可视化管理软件Redis Desktop Manager2022
怎样进行在不改变主线程执行的时候,进行日志的记录
进程间通信和线程间通信
Metasploit-域名上线隐藏IP
Redis visual management software Redis Desktop Manager2022
[230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
【LeetCode】双指针题解汇总
[idea] idea configures sql formatting
随机推荐
国内网站用香港服务器会被封吗?
Redis visual management software Redis Desktop Manager2022
Raw and scan of gorm
tiup status
LeetCode Hot 100
Three tips for you to successfully get started with 3D modeling
2022杭电多校第一场 1004 Ball
软件测试面试题:关于自动化测试工具?
2022杭电多校训练第三场 1009 Package Delivery
2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
IDEA file encoding modification
SV 类的虚方法 多态
【Valentine's Day special effects】--Canvas realizes full screen love
What is next-generation modeling (with learning materials)
【LeetCode】Summary of Two Pointer Problems
子连接中的参数传递
电赛必备技能___定时ADC+DMA+串口通信
"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology
2 用D435i运行VINS-fusion
Modelers experience sharing: model study method