当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
【云原生--Kubernetes】调度约束
Three tips for you to successfully get started with 3D modeling
[Cloud Native--Kubernetes] Pod Controller
gorm联表查询-实战
【idea】idea配置sql格式化
node使用redis
The master teaches you the 3D real-time character production process, the game modeling process sharing
翁恺C语言程序设计网课笔记合集
软件质量评估的通用模型
《WEB安全渗透测试》(28)Burp Collaborator-dnslog外带技术
随机推荐
【Unity编译器扩展之进度条】
性能测试如何准备测试数据
Flask框架 根据源码分析可扩展点
DNS常见资源记录类型详解
找不到DiscoveryClient类型的Bean
一、爬虫基本概念
在线中文姓名生成工具推荐
ansible学习笔记分享-含剧本示例
tiup update
刘润直播预告 | 顶级高手,如何创造财富
Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions
lua 如何 实现一个unity协程的工具
三大技巧让你成功入门3D建模,零基础小白必看
软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
Will domestic websites use Hong Kong servers be blocked?
工业物联网 —— 新型数据库的召唤
MongoDB permission verification is turned on and mongoose database configuration
what is MVCC
2022牛客暑期多校训练营5(BCDFGHK)
元宇宙:未来我们的每一个日常行为是否都能成为赚钱工具?