Forgery
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.
xxx
x.x
xxx
Determine whether is it possible to forge the signature on an empty n×mn×m grid.InputThe first line of input contains two integers nn and mm (3≤n,m≤1000).
Input
Then n lines follow, each contains mm characters. Each of the characters is either ‘.’, representing an empty cell, or ‘#’, representing an ink filled cell.
Output
If Andrey can forge the signature, output “YES”. Otherwise output “NO”.You can print each letter in any case (upper or lower).
input
3 3
###
#.#
###
output
YES
input
3 3
###
###
###
output
NO
input
4 3
###
###
###
###
output
YES
input
5 7
.......
.#####.
.#.#.#.
.#####.
.......
output
YES
Note
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):
we have a clear paper:
...
...
...
...
use the pen with center at (2,2).
###
#.#
###
...
use the pen with center at (3,2).
###
###
###
###
In the fourth sample Andrey can paint the borders of the squares with the centers in (3,3) and (3,5).
题意:
给定一个n行m列的图,问是否能由已知的图形1构成
xxx
x.x
xxx
图1
题解:
直接模拟即可,’.‘的八个方向有’.‘那么他的八个方向就不可能有’#’,然后枚举行和列,用temp存可能的图形,最后与原图s比较即可
#include<bits/stdc++.h>
using namespace std;
int n,m;
char s[10005][10005];
char temp[10005][10005];
int dir[8][2]= {-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
int main()
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)scanf("%s",s[i]+1);
for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) temp[i][j]='.';
for(int i=2; i<n; i++)
{
for(int j=2; j<m; j++)
{
int flag=1;
for(int k=0; k<8 && flag; k++)if(s[i+dir[k][0]][j+dir[k][1]]=='.')flag=0;///如果 当前图中'.'的八个方向有一个是'.'那么他的八个方向就不可能有'#'
if(flag) for(int k=0; k<8; k++)temp[i+dir[k][0]][j+dir[k][1]]='#'; ///建立新图
}
}
/*
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
printf("%c",temp[i][j]);
printf("\n");
}
*/
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if(s[i][j]!=temp[i][j])
{
printf("NO\n");
return 0;
}
printf("YES\n");
return 0;
}