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的更多相关文章

  1. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

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

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

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

  5. Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力

    Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...

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

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

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

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

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

随机推荐

  1. python 二次封装logging,导致日志输出的filename错误及优化封装

    问题 封装logging文件名称为:A.py 调用A模块的文件名称为:B.py 二次封装了logging日志模块,根据需要,传入level,判断等级,调用logging模块的info.debug等日志 ...

  2. textFieldShouldReturn: 方法无效化!

    问题描述 不管如何在键盘上点击return,textFieldShouldReturn:方法一直没有调用. 问题代码 @interface ViewController : UIViewControl ...

  3. 了解Microsoft Media Foundation

    关于Microsoft Media Foundation 是什么 Microsoft Media Foundation是用来处理(创建.修改.传输.合成)多媒体数据(音视频)的一个平台. 有什么用 M ...

  4. 在高通Fastmmi模式中增强交互方式

    在高通Fastmmi模式中增强交互方式 背景 由于之前工厂抱怨 FCT模式不好用. 之前的FCT测试是这样子的:PCBA上夹具,连接USB. 同时,使用上位机程序(ATE)发送指令,人工判断结果以后, ...

  5. Linux中的IDR机制

    # Linux中的IDR机制 背景 最近在学习 Linux的i2c子系统,看到代码中有关于IDR的调用.了解了一下有关的文档,发现是用来管理指针(对象实例). //based on linux V3. ...

  6. Freertos学习:00-介绍

    --- title: rtos-freertos-000-介绍 EntryName: rtos-freertos-00-about date: 2020-06-09 23:21:44 categori ...

  7. 3562-Qt工程编译说明

  8. 案例源码公开!分享瑞芯微RK3568J与FPGA的PCIe通信案例,嵌入式必读!

    ​ ARM + FPGA架构有何种优势 近年来,随着中国新基建.中国制造2025的持续推进,单ARM处理器越来越难满足工业现场的功能要求,特别是能源电力.工业控制.智慧医疗等行业通常需要ARM + F ...

  9. Apache Hudi X Apache Kyuubi,中国移动云湖仓一体的探索与实践

    分享嘉宾:孙方彬 中国移动云能力中心 软件开发工程师 编辑整理:Hoh Xil 出品平台:DataFunTalk 导读:在云原生 + 大数据的时代,随着业务数据量的爆炸式增长以及对高时效性的要求,云原 ...

  10. System.NotSupportedException:“无法显式设置 SplitterPanel 的高度。改在 SplitContainer 上设置 SplitterDistance。”

    System.NotSupportedException:"无法显式设置 SplitterPanel 的高度.改在 SplitContainer 上设置 SplitterDistance.& ...