Problem - 616C - Codeforces
Problem - 616C - Codeforces
C. The Labyrinth
如果是直接对\(*\)去跑dfs或者bfs的话无疑是会超时的
既然如此,那我们可以去对 \(.\) 跑搜索,将各个连通的 \(.\) 块标号并计算出连通块内的点的数量,然后去遍历\(*\)的时候只需要上下左右跑一下计算即可
啊,在\(bfs\)或\(dfs\)的时候千万不要每次都开\(n\times m\)的空间去标记点,只需要在外面开一个就好!
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
vector<int> ans(1e6);
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<string> g(n);
for (auto &i : g)
cin >> i;
int u[] = {1, -1, 0, 0}, v[] = {0, 0, 1, -1};
vector<vector<int>> G(n,vector<int>(m,0));
int num = 0;
vector vis(n, vector<bool>(m));
auto bfs = [&](int x, int y) {
queue<PII> Q;
G[x][y] = num;
Q.push({x, y});
i64 res = 1;
vis[x][y] = true;
while (Q.size()) {
auto [x1, y1] = Q.front();
Q.pop();
for (int i = 0; i < 4; i ++) {
int dx = x1 + u[i], dy = y1 + v[i];
if (dx < 0 || dy < 0 || dx >= n || dy >= m || vis[dx][dy] || g[dx][dy] == '*')
continue;
Q.push({dx, dy});
vis[dx][dy] = true;
res ++;
G[dx][dy] = num;
}
}
return res % 10;
};
for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
if (g[i][j] == '.' && !G[i][j]) {
++ num;
ans[num] = bfs(i, j);
}
}
}
for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
if (g[i][j] == '*') {
int res = 1;
int f[4]{0};
for (int k = 0; k < 4; k ++) {
int dx = i + u[k], dy = j + v[k];
if (dx < 0 || dy < 0 || dx >= n || dy >= m || g[dx][dy] == '*' )
continue;
if(G[dx][dy] == f[0] || G[dx][dy] == f[1] || G[dx][dy] == f[2] || G[dx][dy] == f[3])
continue;
f[k] = G[dx][dy];
res += ans[G[dx][dy]];
}
cout << res % 10;
} else
cout << g[i][j];
}
cout << '\n';
}
return 0;
}
Problem - 616C - Codeforces的更多相关文章
- Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...
- Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分
Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...
- Codeforces Round #439 (Div. 2) Problem C (Codeforces 869C) - 组合数学
— This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice itself, Onii ...
- Codeforces Round #439 (Div. 2) Problem B (Codeforces 869B)
Even if the world is full of counterfeits, I still regard it as wonderful. Pile up herbs and incense ...
- Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力
Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力
题目传送门 传送门I 传送门II 传送门III 题目大意 求一个满足$d\sum_{i = 1}^{n} \left \lceil \frac{a_i}{d} \right \rceil - \sum ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块
Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...
随机推荐
- CLR via C# 笔记 -- 委托(17)
1. 委托是方法的包装器,使方法能通过包装器来间接回调.在一个类型中通过委托来调用另一个类型的私有成员,只要委托对象是具有足够安全性/可访问性的代码创建,便没有问题. 2. 协变性:方法能返回从委托的 ...
- STM32 CubeMX 学习:05-串口
--- title: mcu-stm32-cube-05-using-serial.md date: 2020-03-09 10:37:34 categories: tags: - stm32 - c ...
- vulhub - INFOSEC PREP: OSCP
vulhub - INFOSEC PREP: OSCP 信息收集 nmap 192.168.157.0/24 nmap -sT --min-rate 10000 -p- 192.168.157.162 ...
- 云服务器从阿里云迁移到华为云,FTP服务器的一些设置处理
由于一些特殊原因,计划从阿里云上把ECS服务器的相关资源资源迁移到华为云上,为了保险起见,先申请一个月的华为云ECS服务器进行测试,首先就是搭建FTP服务器进行文件的上传处理,在使用FileZilla ...
- C# 语言笔记
1. C# 初识 因为先前已经学过 C++ 了,所以在C# 的学习中,大多只记录和 C++ 不同的点,在学习的过程中,感谢刘铁猛老师的教程,您是我C# 入门的领路人. 1.1 使用 .net cli ...
- 关于Windows 10 LTSC 2019无法安装Edge的解决方案
最近新换了Windows 10 LTSC 2019系统,使用体验干净且流畅,但是在更新Edge时遇到了问题:系统内装的是9x版本的Edge浏览器,并且提示更新错误,有system level方面的问题 ...
- 第六节 JMeter基础-中级登录【用户自定义变量】
1.认识JMeter (1)配置元件:配置对应的一些数据 (例如:HTTP请求默认值.用户定义的变量) (2)[HTTP请求默认值]:HTTP请求默认值是设置的Web服务器部分信息,可以贯穿多个接口. ...
- Known框架实战演练——进销存系统需求
概述 该项目是一个开源.简易.轻量级的进销存管理系统,作为Known框架的实战演练项目. 项目代码:JxcLite 开源地址: https://gitee.com/known/JxcLite 功能模块 ...
- JMeter 后置处理器之JSON提取器
后置处理器之JSON提取器 By:授客 QQ:1033553122 测试环境 JMeter 5.4.1 插件介绍 JSON后置处理器(PostProcessor)允许使用 JSON Path 语法从J ...
- ElementUI FORM结合Vue实现横向排列表单项
结合Vue实现横向排列表单项 前言 默认的,ElementUI的Form表单项(控件)是垂直排列,即一行显示一个表单项.但是在实际应用中,有时候会需要一行显示多个表单项.针对这类需求,笔者提供以下解决 ...