B. Forgery
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
【Problem】
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×m grid, where every cell is either filled with ink or empty. Andrey's pen can fill a 3×3 square without its central cell if it is completely contained inside the grid, as shown below.
http://codeforces.com/contest/1059/problem/B
【题解】
从上到下,从左到右地遍历。
若当前点为‘#’字符,则将它作为这个3*3矩阵的左上角覆盖,即可完成本问题。
【AC代码】
#include<cstdio>
#include<iostream>
int n,m,p[1005][1005];
char a[1005][1005];
void BFS(int x,int y)
{
if(x+1>n||y+1>m||x+2>n||y+2>m) return;
if(a[x][y]=='.'||a[x+1][y]=='.'||a[x+2][y]=='.'||a[x][y+1]=='.'||a[x][y+2]=='.'||a[x+1][y+2]=='.'||a[x+2][y+1]=='.'||a[x+2][y+2]=='.') return;
p[x][y]=p[x+1][y]=p[x+2][y]=p[x][y+1]=p[x][y+2]=p[x+1][y+2]=p[x+2][y+1]=p[x+2][y+2]=true;
}
int main()
{
scanf("%d%d",&n,&m);
for(register int i=1;i<=n;++i)
for(register int j=1;j<=m;++j)
std::cin>>a[i][j];
for(register int i=1;i<=n;++i)
for(register int j=1;j<=m;++j)
if(a[i][j]=='#') BFS(i,j);
for(register int i=1;i<=n;++i)
for(register int j=1;j<=m;++j)
if(a[i][j]=='#'&&!p[i][j])
{
puts("NO");
return 0;
}
puts("YES");
return 0;
}