当前位置: 首页 > news >正文

Atcoder-460-D Repeatedly Repainting

Problem Statement

There is a grid with HH rows and WW columns. The cell at the ii-th row from the top and the jj-th column from the left is called cell (i,j)(i,j).

Every cell is colored white or black. The grid is described by HH strings S1,S2,…,SHS1,S2,…,S**H, each of length WW. If the jj-th character of SiS**i is ., cell (i,j)(i,j) is white; if the jj-th character of SiS**i is #, cell (i,j)(i,j) is black.

You perform the following operation 1010010100 times.

  • Simultaneously apply the following rules to all cells.
    • A cell that is white before the operation becomes black if and only if there exists at least one black cell adjacent to it. Here, cells (x,y)(x,y) and (x′,y′)(x′,y′) are adjacent if and only if one of them is within the 88-neighborhood of the other, that is, max⁡(∣x−x′∣,∣y−y′∣)=1max(∣xx′∣,∣yy′∣)=1.
    • A cell that is black before the operation becomes white.

Find the color of each cell after the operations.

Constraints

  • 1≤H×W≤1061≤H×W≤106
  • HH and WW are positive integers.
  • SiS**i is a string of length WW consisting of . and #.

Input

The input is given from Standard Input in the following format:

HH WW
S1S1
S2S2
⋮⋮
SHSH

Output

Output HH lines.

Output one string of length WW consisting of . and # per line.

The jj-th character of the ii-th line should be . if cell (i,j)(i,j) is white after 1010010100 operations, and # if it is black.

Sample 1

Input复制 Output复制
3 4 #.#. .#.. #... #.#. .#.. #..#

Initially, the grid is as follows.

img

After one operation, the grid is as follows.

img

After 1010010100 operations, the grid is as follows.

img

Sample 2

Input复制 Output复制
3 3 ### ### ### ... ... ...

Sample 3

Input复制 Output复制
5 7 .#..... ....... ..#.... ....... ....#.. .#.##.# ....#.. #.#.### #.....# ###.#.#

算法分析

这个题看似很难,实则非常的不简单,因为它的坑点很多,代码量也很大。

首先,直接模拟是不行的,因为如果要模拟的话,照题意,我们要模拟10^100次,肯定会炸。接着我们想一下,不(hen)难发现当一个点变成黑色时,之后每一次操作这个点都是黑白交替的,但是,如果你只想到这个那么恭喜你,成功的掉进了出题人的陷阱!很容易举出反例,比如题目中给出的样例二。我们仔细看一下样例二的输入,可以发现每个点并没有出现黑白交替,操作一次后,之后的每一次都是全白,因为每个点都被它周围一圈的点围着!但是,如果我们先操作一遍,那么就不可能出现一个点被它周围的点围着,因为我们只操作了一次呀!想到这一点后,我们已经成功了一半了,现在我们把思路整理一遍,以便写代码。

1.先手动模拟一次,时间复杂度是 O(nm)。

2.在已经做了一遍之后的数组上,对每个点都做一遍BFS。

3.有一个d数组,存的是每个点和离它最近的黑点的距离。

4.输出时如果d数组的一个点存的值是奇数,输出#,如果是偶数,输出 . 。

坑点

1.由于题目只给出了n*m,但没给出n和m的具体范围,所以每个数组都要开vector。

2.为了防止BFS死循环,要加判断再入队。

3.d数组记得初始化成极大值,且是偶数

4.注意,这题有8个方向可以走,不要写成4个了。

5.在已经进行一次操作的数组上遍历到一个点是黑点时,千万不要急着去做BFS,先把这个点入队了,把d数组更新掉,等遍历完整个数组后,再统一BFS,否则会超时!!!

AC代码

#include<bits/stdc++.h>
using namespace std;
struct node{int x,y;int cnt;
};
int dx[] = {-1,-1,-1,0,0,1,1,1};//八个方向
int dy[] = {-1,0,1,-1,1,-1,0,1};
int n,m;
string a[1000005];
string b[1000005];
vector<int> d[1000005];//每个点和离它最近的黑点的距离
queue<node> q;
void bh(){//一次操作for(int i = 1; i<=n; i++){for(int j = 1; j<=m; j++){if(a[i][j]=='#'){b[i][j] = '.';}else{bool flag = false;for(int k = 0; k<8; k++){int xx = dx[k]+i;int yy = dy[k]+j;if(xx>=1 && xx<=n && yy>=1 && yy<=m && a[xx][yy]=='#'){flag = true;b[i][j] = '#';break;}}if(flag==false){b[i][j] = '.';}}}}for(int i = 1; i<=n; i++){d[i].push_back(INT_MAX-1);for(int j = 1; j<=m; j++){a[i][j] = b[i][j];d[i].push_back(INT_MAX-1);//初始化为极大值,让它-1变成偶数}}
}
void bfs(){//BFSwhile(!q.empty()){node pos = q.front();q.pop();for(int i = 0; i<8; i++){int xx = dx[i]+pos.x;int yy = dy[i]+pos.y;if(xx>=1 && xx<=n && yy>=1 && yy<=m && d[xx][yy]>pos.cnt+1){//条件成立再入队和更新d[xx][yy] = pos.cnt+1;q.push(node{xx,yy,pos.cnt+1});}}}
}
int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);//输入输出加速cin>>n>>m;for(int i = 1; i<=n; i++){string s;cin>>s;s = " "+s;//输入处理a[i] = s;b[i] = s;}bh();//先进行一次操作for(int i = 1; i<=n; i++){for(int j = 1; j<=m; j++){if(a[i][j]=='#'){d[i][j] = 0;q.push(node{i,j,0});//先入队}}}bfs();//统一BFSfor(int i = 1; i<=n; i++){for(int j = 1; j<=m; j++){if(d[i][j]%2==1){cout<<'#';}else{cout<<'.';}}cout<<'\n';//比endl要快得多}return 0;
}
http://www.jsqmd.com/news/951632/

相关文章:

  • YOLOv11涨点改进| CVPR 2025 |独家创新首发、特征融合改进篇|引入GPTB全局感知变换器融合模块,获得更强全局感知和上下文建模能力,助力多模态目标检测、小目标检测、图像超分任务有效涨点
  • Gemini剪贴板集成:零操作接入的AI生产力革命
  • Vue-next-admin:从技术选型到团队协作的全栈管理后台解决方案
  • 深度解析:基于YOLOv5的AI自动瞄准系统3种实战部署方案
  • NPU加速的BERT模型:bert-uncased-keyword-extractor性能优化实战指南 [特殊字符]
  • 2026四六级翻译预测|四级六级汉译英热点+范文PDF
  • Kronos金融大模型:如何用开源AI技术革新股票预测
  • 163MusicLyrics 7.3 版本:跨平台歌词管理工具的终极指南
  • AI工具×智能结算=降本增效新拐点?实测数据:结算周期压缩至17秒,人力成本直降64%
  • 2026年铜铝排浸塑浸粉源头工厂榜单:新能源/折弯/异形/镀锡铜铝排绝缘处理优选品牌推荐 - 品牌企业推荐师(官方)
  • 2026年上海实验室系统/通排风与变风量等十大系统推荐榜单:半导体洁净净化及恒温恒湿专业厂家实力解析 - 品牌企业推荐师(官方)
  • 如何打造个性化音乐播放器:foobar2000界面美化完全指南
  • Vim Vixen:让Firefox秒变Vim操作神器,开启高效网页浏览新纪元
  • ATH协议开源:三方握手解决Agent权限失控,中国信通院联合腾讯华为发布
  • 利用Arduino Uno作为ISP编程器驱动LED点阵屏的完整实践指南
  • 5分钟快速上手:基于Vue.js的可视化流程设计器easy-flow
  • 用YAML文件优雅管理ROS参数:以MoveIt!和导航包配置为例
  • 如何通过OpenCode插件架构构建企业级AI助手扩展平台:完整实施指南
  • Arduino音乐点唱机:从电路设计到模块化编程的嵌入式系统实践
  • UE引擎初始化流程
  • 3步掌握Mermaid Live Editor:用代码思维构建专业图表
  • 新手福音:借助快马AI代码生成,零基础轻松完成第一个Python数据分析项目
  • iOS语音处理新选择:Silero-VAD-v5-CoreML核心功能详解
  • MindSpore框架实战:PanGu Draw V3模型训练与推理教程
  • 2026最新!亲测3款免费实用神器,轻松搞定网页视频提取算完AI款综合得分真香!
  • 2026年北京农村老房翻建换瓦指南:彩石金属瓦/仿古金属瓦/铝镁锰瓦哪个最适合 - 企业深度横评dyy6420
  • 2026年 洒水车厂家推荐排行榜:市政环卫洒水车/工程抑尘洒水车/路面清扫喷洒车品牌优选与深度评测 - 品牌企业推荐师(官方)
  • 3分钟免费掌握Mermaid Live Editor:在线图表编辑器的完整指南
  • 从数字到实体:Bambu Studio如何成为3D打印创作的核心桥梁
  • 2026年PDF压缩免费推荐PDF转图片批量转换,pdf转Excel/pdf转word/pdf转换器/pdf转ppt/命令行版适合批量自动化处理 - 时时资讯