Forgery(染色问题bfs_有点搞

Forgery

题面翻译

给定一个 n , m n,m n,m的目标矩阵。

初始矩阵全为 ′ . ′ '.' .

你可以染色无数次,每次选定一个格子将它周围的八个格子(除它自己)的3*3矩阵覆盖成’#'。

这个3*3的矩阵只能出现在 n ∗ m n*m nm的矩阵内部。不能越界。

询问是否能构成目标矩阵。

题目描述

Student Andrey has been skipping physical education lessons for the whole term, and now he must somehow get a passing grade on this subject. Obviously, it is impossible to do this by legal means, but Andrey doesn’t give up. Having obtained an empty certificate from a local hospital, he is going to use his knowledge of local doctor’s handwriting to make a counterfeit certificate of illness. However, after writing most of the certificate, Andrey suddenly discovered that doctor’s signature is impossible to forge. Or is it?

For simplicity, the signature is represented as an $ n\times m $ grid, where every cell is either filled with ink or empty. Andrey’s pen can fill a $ 3\times3 $ square without its central cell if it is completely contained inside the grid, as shown below.

<br></br>xxx<br></br>x.x<br></br>xxx<br></br>Determine whether is it possible to forge the signature on an empty $ n\times m $ grid.

输入格式

The first line of input contains two integers $ n $ and $ m $ ( $ 3 \le n, m \le 1000 $ ).

Then $ n $ lines follow, each contains $ m $ characters. Each of the characters is either ‘.’, representing an empty cell, or ‘#’, representing an ink filled cell.

输出格式

If Andrey can forge the signature, output “YES”. Otherwise output “NO”.

You can print each letter in any case (upper or lower).

样例 #1

样例输入 #1

3 3
###
#.#
###

样例输出 #1

YES

样例 #2

样例输入 #2

3 3
###
###
###

样例输出 #2

NO

样例 #3

样例输入 #3

4 3
###
###
###
###

样例输出 #3

YES

样例 #4

样例输入 #4

5 7
.......
.#####.
.#.#.#.
.#####.
.......

样例输出 #4

YES

提示

In the first sample Andrey can paint the border of the square with the center in $ (2, 2) $ .

In the second sample the signature is impossible to forge.

In the third sample Andrey can paint the borders of the squares with the centers in $ (2, 2) $ and $ (3, 2) $ :

  1. we have a clear paper: <br></br>...<br></br>...<br></br>...<br></br>...<br></br>
  2. use the pen with center at $ (2, 2) $ . <br></br>###<br></br>#.#<br></br>###<br></br>...<br></br>
  3. use the pen with center at $ (3, 2) $ . <br></br>###<br></br>###<br></br>###<br></br>###<br></br>

In the fourth sample Andrey can paint the borders of the squares with the centers in $ (3, 3) $ and $ (3, 5) $ .

#include<iostream>
#include<cstring>
#include<queue> 
using namespace std;
#define x first
#define y second
const int N=1010;
typedef pair<int,int> PII;
char g[N][N];
char g2[N][N];
bool st[N][N];
PII q[N*N];
int hh=0,tt=-1;
int dx[]={-1,-1,-1,0,1,1,1,0};
int dy[]={0,1,-1,1,0,-1,1,-1};
int n,m;
bool simlar=true;
void bfs(){

	while(hh<=tt){
		PII t=q[hh++];
		for(int k=0;k<8;k++){
			g2[t.x+dx[k]][t.y+dy[k]]='#'; 
		}
	
		
	}
	
	for(int i=1;i<=n&&simlar;i++)
		for(int j=1;j<=m&&simlar;j++){
			if(g[i][j]!=g2[i][j])simlar=false;
		}
}

int main(){
	cin>>n>>m;

	int cnt=0;
	 	
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++){
		cin>>g[i][j];
		g2[i][j]='.';
		if(g[i][j]=='.')cnt++;
	}
	
	if(cnt){
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++){
				int hefa=true;
				
			for(int k=0;k<8&&hefa;k++){
				int x2=i+dx[k];
				int y2=j+dy[k];
				if(x2<1||x2>n||y2<1||y2>m){hefa=false;continue;}
				if(g[x2][y2]!='#'){hefa=false;continue;}
				}
			if(hefa)q[++tt]={i,j};
			
			}
		
		
	}else{
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++){
				int hefa=true;
				
			for(int k=0;k<8&&hefa;k++){
				int x2=i+dx[k];
				int y2=j+dy[k];
				if(x2<1||x2>n||y2<1||y2>m){hefa=false;continue;}
				}
				
			if(hefa)q[++tt]={i,j};
				
			}
		
	}
	bfs();	
	
	if(simlar)cout<<"YES";
	else cout<<"NO";
	
	return 0;
}

感觉代码很乱,题解看完再改下更新下

改进版(只需判断八个方向是否为#,是就扔进队列里。。也可以强行枚举每一个点在判断,在染色

#include<iostream>
#include<cstring>
#include<queue> 
using namespace std;
#define x first
#define y second
const int N=1010;
typedef pair<int,int> PII;
char g[N][N];
char g2[N][N];
bool st[N][N];
PII q[N*N];
int hh=0,tt=-1;
int dx[]={-1,-1,-1,0,1,1,1,0};
int dy[]={0,1,-1,1,0,-1,1,-1};
int n,m;
bool simlar=true;
void bfs(){

	while(hh<=tt){
		PII t=q[hh++];
		for(int k=0;k<8;k++){
			g2[t.x+dx[k]][t.y+dy[k]]='#'; 
		}
	
		
	}
	
	for(int i=1;i<=n&&simlar;i++)
		for(int j=1;j<=m&&simlar;j++){
			if(g[i][j]!=g2[i][j])simlar=false;
		}
}

int main(){
	cin>>n>>m;
	 	
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++){
		cin>>g[i][j];
		g2[i][j]='.';
	}
	
		for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++){
			int hefa=true;
				
			for(int k=0;k<8&&hefa;k++){
				int x2=i+dx[k];
				int y2=j+dy[k];
				if(x2<1||x2>n||y2<1||y2>m){hefa=false;continue;}
				if(g[x2][y2]!='#'){hefa=false;continue;}
				}
			if(hefa)q[++tt]={i,j};
			
			}
		
		

	
	bfs();	
	
	if(simlar)cout<<"YES";
	else cout<<"NO";
	
	return 0;
}
  • 24
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值