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 unset命令用法
Linux unset命令用于删除变量或函数. unset为shell内建指令,可删除变量或函数 参数: -f 仅删除函数 -v 仅删除变量 [root@localhost ~]# yangzc=&q ...
- dotnet 融合 Avalonia 和 UNO 框架
现在在 .NET 系列里面,势头比较猛的 UI 框架中,就包括了 Avalonia 和 UNO 框架.本文将告诉大家如何尝试在一个解决方案里面融合 Avalonia 和 UNO 两个框架,即在一个进程 ...
- 在VisualStudio中WPF应用程序在打开窗体界面设计时报错<发生了未经处理的异常>的解决方法
在网上找了一个wpf的开源项目,在打开窗体,点击设计的时候,提示错误信息如下 System.Resources.MissingSatelliteAssemblyExceptionThe satelli ...
- SQL如何优化和设计索引
SQL优化 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引: 避免使用 NULL 字段,很难查询优化且占用额外索引空间,可以设置默认值0或'': ...
- Centos7离线安装gcc4.8
有时候CentOS工作在无互联网的环境下,需要在离线环境下安装一些组件,这次实现的是模拟在离线环境下安装gcc4.8. 第一步: 先去http://mirrors.aliyun.com/centos/ ...
- vue3 'alex' is defined but never used
解决方法 在package.json中的rules下加入 "no-unused-vars":"off" 即可
- c 语言学习第四天
if 语句 格式: // 1 // 其他语句... if(表达式){ // 其他语句... } // 其他语句... // 2 if(表达式){ }else{ } // 3 if(表达式1){ }el ...
- python tkinter | 如何使得entry文本框靠右显示,从右向左填充,显示文本末尾
from tkinter import * from tkinter import filedialog app = Tk() app.title("ABC") app.geome ...
- webpack性能优化方式之dll--- webpack.dll.config.js
通常来说,我们的代码都可以至少简单区分成业务代码和第三方库.如果不做处理,每次构建时都需要把所有的代码重新构建一次,耗费大量的时间.然后大部分情况下,很多第三方库的代码并不会发生变更(除非是版本升级) ...
- 常回家看看之largebin_attack
常回家看看之largebin_attack 先简单介绍一下什么是largebin largebin 是 glibc 的 malloc 实现中用于管理大块内存的一种数据结构.在 glibc 的内存分配中 ...