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 ...
随机推荐
- Linux 提权-MySQL UDF
本文通过 Google 翻译 MySQL User Defined Functions – Linux Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭的字词 ...
- 使用 nginx 共享文件
1. 安装nginx 2. 在nginx的配置文件的server部分加上如下的配置: location /shared/ { autoindex on; autoindex_exact_size on ...
- Netty(一)IO模型
1. Netty介绍 Netty 是由JBOSS提供的一个Jave开源框架,是一个异步地.基于事件驱动的网络应用框架,用以快速开发高性能.高可靠的网络IO程序. Netty主要针对在TCP协议下,面向 ...
- Ubuntu 安装 gitweb + Apache2
背景 之前已经使用了gerrit进行代码管理,但是在有些代码由于内部技术管理不当而丢失了Review记录. 因此找到了通过gitweb弥补的问题. 做法 安装 sudo apt-get install ...
- Oracle自动化编译无效对象
问题描述:使用存储过程的方式对oracle数据库的无效对象,如视图或者同义词进行定期的编译,让他变成一个有效的对象,加上定时任务可以实现自动化的处理.同时在数据库内部创建一个记录表,用来记录被编译过的 ...
- 【论文阅读】TRO2022: A Two-Stage Optimization-Based Motion Planner for Safe Urban Driving
TRO2022: A Two-Stage Optimization-Based Motion Planner for Safe Urban Driving Summary: 探讨planning过程中 ...
- Python入门学习介绍
什么是Python? Python它是一种直译式,面向对象,解释式的脚本语言.它和Java,C/C++,Go语言一样都是高级语言,但由于它是解释式语言,所以运行速度会比Java,C/C++等语言慢(虽 ...
- 请解释一下 JavaScript 的同源策略?
概念:同源策略是客户端脚本(尤其是Netscape Navigator2.0,其目的是防止某个文档或脚本从多个不同源装载.这里的同源策略指的是: 协议,域名,端口 相同,同源策略是一种安全协议.指一 ...
- MySQL索引种类
normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号用作索引时,可设置为unique full textl: 表示 全文搜索的索引. FULL ...
- .NET6 API 部署标准流程
一.安装dotnet环境 #第一步:将 Microsoft 包签名密钥添加到受信任密钥列表,并添加 Microsoft 包存储库 sudo rpm -Uvh https://packages.micr ...