当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
克服项目管理中恐惧心理
jenkins发送邮件系统配置
关于使用read table 语句
Huggingface入门篇 II (QA)
隐私计算综述
oracle创建用户
IDEA 文件编码修改
[LeetCode] Summary of Matrix Simulation Related Topics
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
再肝3天,整理了90个 NumPy 例子,不能不收藏!
Couple Holding Hands [Greedy & Abstract]
10 种常见的BUG分类
leetcode经典例题——单词拆分
数据类型及输入输出初探(C语言)
SV 类的虚方法 多态
Senior game modelers tell newbies, what are the necessary software for game scene modelers?
在线中文姓名生成工具推荐
#yyds干货盘点#交换设备丢包严重的故障处理
NMS原理及其代码实现
what?测试/开发程序员要被淘汰了?年龄40被砍到了32?一瞬间,有点缓不过神来......