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 ...
随机推荐
- 技嘉BIOS超频设置操作路径
关闭超线程 频率电压控制 > 进阶处理器设置 > Hyper_THreading 关小核心 频率电压控制 > GIGABYTE PerfDrive > Ecore Disabl ...
- Masonry在视图相对关系处理中的各种“offset”
如果我们需要设置一个view在另一个view的右边缘距离一定距离的地方,利用Masonry这么写: [a mas_makeConstraints:^(MASConstraintMaker *make) ...
- Atcoder Beginner Contest 324 G Generate Arrays 题解-Treap
为了更好的阅读体验,请点击这里 题目链接 套上平衡树板子就能做的很快的题,然后因为是指针存树,因此交换只需要把序列大小较小的挨个拿出来插到相应的地方即可.复杂度 \(O(N \log^2 N)\). ...
- 移植 uCos-III 3.03 到 STM32F429 上
背景 通过STM32 的学习,我们可以往更深层次的地方走,尝试系统上的一些开发. STM32: F429(StdPeriph) uCos-III : v3.04 + 3.03 有关说明: 在移植 3. ...
- 千万别忽视基础!十张图带你一步步理解Java内存结构!
作为一个Java程序员,在日常的开发中,不必像C/C++程序员那样,为每一个内存的分配而操心,JVM会替我们进行自动的内存分配和回收,方便我们开发.但是一旦发生内存泄漏或者内存溢出,如果对Java内存 ...
- BigCodeBench: 继 HumanEval 之后的新一代代码生成测试基准
HumanEval 是一个用于评估大型语言模型 (LLM) 在代码生成任务中的参考基准,因为它使得对紧凑的函数级代码片段的评估变得容易.然而,关于其在评估 LLM 编程能力方面的有效性越来越多的担忧, ...
- 【论文阅读】VDBFusion: Flexible and Efficient TSDF Integration of Range Sensor Data
Type: Sensors Year: 2022 tag: Mapping 组织: Bonn 参考与前言 论文链接:https://www.ncbi.nlm.nih.gov/pmc/articles/ ...
- 基于NXP i.MX 6ULL——MQTT通信协议的开发案例
前 言 本指导文档适用开发环境: Windows开发环境:Windows 7 64bit.Windows 10 64bit Linux开发环境:Ubuntu 18.04.4 64bit 拟机:VMw ...
- Nginx配置以及热升级
目录 Nginx详解 1. Nginx关键特性 2. Nginx配置 2.1 event 2.2 http 2.2.1 log_format 2.2.2 sendfile 2.2.3 tcp_nopu ...
- 实现ASP.Net Core3.1运行在DockeDesktop下并用Nginx实现负载均衡
一.首先去https://docs.docker.com/get-docker/下载Windows版本的Docker Desktop并安装(需要win10专业版以上操作系统,并启用CPU虚拟化和安装H ...