题目链接

题目

见链接。

题解

知识点: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的更多相关文章

  1. Life Cycle of Thread – Understanding Thread States in Java

    Life Cycle of Thread – Understanding Thread States in Java 深入理解java线程生命周期. Understanding Life Cycle ...

  2. Hover States - 有趣的用户界面及交互设计

    Hover States 一组新潮的和有趣的用户界面和交互设计的集合.Hover States 的目标是要成为设计师和开发人员灵感来源,向人们展示目前人们正在做的各种网站中令人惊奇的效果.他们认为交互 ...

  3. Channel States

    Introduction A channel (a call) will go through many different states during its lifetime. Here we w ...

  4. flex4的s:states和mx:states的区别

    http://help.adobe.com/en_US/Flex/4.0/UsingSDK/WS2db454920e96a9e51e63e3d11c0bf63611-7ffa.html#WS43468 ...

  5. 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 ...

  6. PROCESS STATES

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION To understand the ope ...

  7. [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 ...

  8. codeforces 590C C. Three States(bfs+连通块之间的最短距离)

    题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...

  9. Codeforces Round #327 (Div. 2) E. Three States

    题目链接: 题目 E. Three States time limit per test:5 seconds memory limit per test:512 megabytes 问题描述 The ...

  10. I.MX6 show battery states in commandLine

    #/bin/sh # I.MX6 show battery states in commandLine # 声明: # 在命令行下自动显示电池状态的信息. # # -- # set battery r ...

随机推荐

  1. spring注入的几种方式

    本文为博主原创,未经允许不得转载: Spring注入有以下几种方式: 构造方法注入:通过构造方法实现依赖注入.在类的构造方法中使用@Autowired注解注入需要的依赖类. Setter方法注入:通过 ...

  2. [转帖]云数据库是杀猪盘么,去掉中间商赚差价,aws数据库性能提升 10 倍!价格便宜十倍。

    https://tidb.net/blog/021059f1 于是乎dba中的冯大嘴喊出了云数据库就是杀猪盘.让每个公司自建数据库. 那么有没有一种数据库又便宜又好用呢.有 哪就是tidb数据库. 之 ...

  3. [转帖]为非root用户添加NOPASSWD权限

    https://www.jianshu.com/p/d1e71bda4b34 查看树莓派默认是怎么为pi用户免去密码 所有配置文件都在 /etc 目录下,免去密码配置文件也不例外.在/etc/sudo ...

  4. [转帖]Windows自带MD5 SHA1 SHA256命令行工具

    https://www.cnblogs.com/huangrt/p/13961399.html 检验工具http://www.zdfans.com/html/4346.html HashMyFiles ...

  5. [转帖]tidb backup

    https://docs.pingcap.com/zh/tidb/v4.0/sql-statement-restore BACKUP 语句使用的引擎与 BR 相同,但备份过程是由 TiDB 本身驱动, ...

  6. 遇到疯狂GC时进行判断然后重启服务的方法-GPT学习使用之三

    遇到疯狂GC时进行判断然后重启服务的方法-GPT学习使用之三 背景 最近怀疑产品遇到了第三方组建的bug Groupdocs转换渲染某些文件时出现了严重的FullGC的情况 而且出现的奇怪的功效学GC ...

  7. [转帖]15.1. 插件dblink简介

    https://help.kingbase.com.cn/v8.6.7.12/development/sql-plsql/ref-extended-plug-in/dblink.html dblink ...

  8. [转帖]/dev/null 2>&1详解

    https://www.diewufeiyang.com/post/1045.html shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形 ...

  9. 【转帖】MySQL InnoDB存储原理深入剖析与技术分析

    一.MySQL记录存储: MySQL InnoDB的数据由B+树来组织,数据记录存储在B+树数据页(page)中,每个数据页16kb,数据页 包括页头.虚记录.记录堆.自由空间链表.未分配空间.slo ...

  10. css伪类和伪元素在项目中的使用-红色*显示

    CSS使用伪类给表单添加星号 <style type="text/css"> .form-item label::before { content: '*'; colo ...