CF590C Three States
题目
见链接。
题解
知识点:BFS。
这道题求连接三个国家的最短路径长度。如果枚举每个点进行bfs,显然不可行,换种思路,从三个国家开始分别进行bfs是可以的。
注意一开始初始化两个距离数组要无穷大 (0x7f/3 ,除三是因为防止加起来炸了)。
国家是个连通块,而连通块内部是不算路径长度的,那么一开始预处理把连通块内所有格子的步数全设为 \(0\),并直接入队,否则会超时。
用 \(vis\) 数组记录状态是否访问过,因为步数都是恒定 \(1\) 的增量不需要考虑维护bfs扩展顺序,也不必考虑扩展的步数是否小于访问过的点的步数,因为时间线是有序的,这样可以直接用 \(vis\) 可以省一点时间。
当起点国家遇到国家的格子时,就不需要继续搜索了,因为国家内部是不算长度的,只需要用另外一个数组记录国家和国家之间的最短路径即可,当作直线连接国家的最短路径长度,不然会超时。
其他情况都是国家之外的点到三个国家的距离,正常记录即可。
最后取三个国家每个与其他两个国家直线距离和(有国家和国家连通的特殊情况,这种情况直线最短)与场上不是国家的点到三个国家的距离和减2(因为国家之外的点,三个国家会多算两次这个点的距离,所以减二;而直线距离中间隔着国家,不会多算点)的最小值。
取直线距离最小值时候要注意,一定每个国家为起点的都看一下。因为比如三个国家在一直线上,顺序为 \(1,2,3\) ,显然 \(1\) 遇到 \(2\) 以后就停了,\(3\) 也一样,那么 \(1\) 到 \(3\) 的距离就是无穷大。但考虑 \(2\) ,则有通路 \(21\) 和 \(23\) 。因此每个国家都要作为起点都看一下直线距离。
时间复杂度 \(O(?)\)
空间复杂度 \(O(nm)\)
代码
#include <bits/stdc++.h>
using namespace std;
int n, m;
char dt[1007][1007];
bool vis[1007][1007][3];
int d[1007][1007][3];
int D[3][3];
const int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
struct node {
int x, y;
};
void bfs(int id) {
queue<node> q;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
if (dt[i][j] == id + '1') {
d[i][j][id] = 0;
q.push({ i,j });
}
}
}
while (!q.empty()) {
node cur = q.front();
q.pop();
for (int i = 0;i < 4;i++) {
int xx = cur.x + dir[i][0];
int yy = cur.y + dir[i][1];
if (xx < 0 || xx >= n || yy < 0 || yy >= m || dt[xx][yy] == '#' || vis[xx][yy][id]) continue;
vis[xx][yy][id] = 1;
if (dt[xx][yy] == '.') {
d[xx][yy][id] = d[cur.x][cur.y][id] + 1;
q.push({ xx, yy });
}
else {
if (D[id][dt[xx][yy] - '1'] > 1e6)
D[id][dt[xx][yy] - '1'] = d[cur.x][cur.y][id];
}
}
}
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
memset(d, 0x7f / 3, sizeof(d));
memset(D, 0x7f / 3, sizeof(D));
cin >> n >> m;
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
cin >> dt[i][j];
}
}
for (int i = 0;i < 3;i++) bfs(i);
int ans = ~(1 << 31);
for (int i = 0;i < 3;i++) {
int sum = 0;
for (int j = 0;j < 3;j++)
if (i != j) sum += min(D[i][j], D[j][i]);
ans = min(ans, sum);
}
for (int i = 0;i < n;i++) {
for (int j = 0;j < m;j++) {
if (dt[i][j] == '.') ans = min(ans, d[i][j][0] + d[i][j][1] + d[i][j][2] - 2);
}
}
cout << (ans > 1e6 ? -1 : ans) << '\n';
return 0;
}
CF590C Three States的更多相关文章
- Life Cycle of Thread – Understanding Thread States in Java
Life Cycle of Thread – Understanding Thread States in Java 深入理解java线程生命周期. Understanding Life Cycle ...
- Hover States - 有趣的用户界面及交互设计
Hover States 一组新潮的和有趣的用户界面和交互设计的集合.Hover States 的目标是要成为设计师和开发人员灵感来源,向人们展示目前人们正在做的各种网站中令人惊奇的效果.他们认为交互 ...
- Channel States
Introduction A channel (a call) will go through many different states during its lifetime. Here we w ...
- flex4的s:states和mx:states的区别
http://help.adobe.com/en_US/Flex/4.0/UsingSDK/WS2db454920e96a9e51e63e3d11c0bf63611-7ffa.html#WS43468 ...
- How to Programmatically Switch between the HubTile Visual States
In this post I am going to talk about how to programmatically switch between different HubTile Visua ...
- PROCESS STATES
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION To understand the ope ...
- [Angular2 Form] Understand the Angular 2 States of Inputs: Pristine and Untouched
Angular 2’s ngModel exposes more than just validity, it even gives you the states of whether the inp ...
- codeforces 590C C. Three States(bfs+连通块之间的最短距离)
题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...
- Codeforces Round #327 (Div. 2) E. Three States
题目链接: 题目 E. Three States time limit per test:5 seconds memory limit per test:512 megabytes 问题描述 The ...
- I.MX6 show battery states in commandLine
#/bin/sh # I.MX6 show battery states in commandLine # 声明: # 在命令行下自动显示电池状态的信息. # # -- # set battery r ...
随机推荐
- 【动画进阶】神奇的 3D 卡片反光闪烁动效
最近,有群里在群里发了这么一个非常有意思的卡片 Hover 动效,来源于此网站 -- key-drop,效果如下: 非常有意思酷炫的效果.而本文,我们不会完全还原此效果,而是基于此效果,尝试去制作这么 ...
- 2023第十四届极客大挑战 — RE WP
RE方向出自:队友. Shiftjmp 去花后按p然后再反编译 最后flag为SYC{W3lc0me_tO_th3_r3veR5e_w0r1d~} 点击就送的逆向题 gcc 1.s -o 1` 生成e ...
- [转帖]rsar - Extract data from plain-text sar files
sar -A -t -f /tmp/sa11 >/tmp/sar11 https://github.com/ryran/rsar When dealing with sysstat sar da ...
- [转帖] 常见的Socket网络异常场景分析
https://www.cnblogs.com/codelogs/p/16001770.html 原创:打码日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介# 在目前微服务的 ...
- 【转帖】io_uring vs epoll ,谁在网络编程领域更胜一筹?
io_uring vs epoll ,谁在网络编程领域更胜一筹? 2021-12-16 1473举报 简介: 从定量分析的角度,通过量化 io_uring 和 epoll 两种编程框架下的相关操作的耗 ...
- 【转帖】【奇技淫巧】Linux | 统计网络-netstat
theme: condensed-night-purple 小知识,大挑战!本文正在参与"程序员必备小知识"创作活动. 在构建生产服务器时,我们有的时候需要统计网络接口状况,比如T ...
- IIS 实现autoindex的简单方法 能够下载文件等.
之前使用nginx 的autoindex on 的参数 能够实现了 nginx的 目录浏览查看文件 但是那是linux上面的 windows 上面很多 使用的 其实是 iis的居多 然后看了下 其实也 ...
- 如何抓取http请求/拦截器用法
我们都知道postman是模拟接口向服务端发送请求的,在编写请求数据的时候非常 麻烦,那么如果我们可以先抓取该接口后直接使用,就方便的很多 抓取http请求 1.我们打开postman时就会看见右上角 ...
- ElasticSearch降本增效常见的方法 | 京东云技术团队
Elasticsearch在db_ranking 的排名不断上升,其在存储领域已经蔚然成风且占有非常重要的地位. 随着Elasticsearch越来越受欢迎,企业花费在ES建设上的成本自然也不少.那如 ...
- 【字符串,哈希】【Yandex】Yandex7736
2023.6.30 Problem Link 定义一个串 \(S\) 是好的,当且仅当 \(S\) 可以不断消去相邻两个相同字符直至消空.给定一个长为 \(n\) 的字符串 \(s\),求有多少个有序 ...