cf#514B. Forgery(暴力)

B. Forgery
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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×mn×m grid, where every cell is either filled with ink or empty. Andrey's pen can fill a 3×33×3square 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.

Input
The first line of input contains two integers nn and mm (3≤n,m≤10003≤n,m≤1000).

Then nn 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).

Examples
input
Copy
3 3
###
#.#
###
output
Copy
YES
input
Copy
3 3
###
###
###
output
Copy
NO
input
Copy
4 3
###
###
###
###
output
Copy
YES
input
Copy
5 7
.......
.#####.
.#.#.#.
.#####.
.......
output
Copy
YES
Note
In the first sample Andrey can paint the border of the square with the center in (2,2)(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)(2,2) and (3,2)(3,2):

we have a clear paper:

...
...
...
...
use the pen with center at (2,2)(2,2).

###
#.#
###
...
use the pen with center at (3,2)(3,2).

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

题意:模仿老师的签名,每次写下去都是3*3的矩阵,中间没有墨,给出一个矩阵表示老师的签名,问能不能模仿成功

题解:暴力,输入的时候计算#的个数sum,最外面一圈是不用看的,从第二行第二列开始,假设该位置是3*3矩阵的中间,看该点周围一圈是不是都是#或者已经写过的#,如果是就算写一次,并计算该次覆盖的#个数(之前没有覆盖过的)cnt,并标记已经覆盖过的。最后比较cnt是不是等于sum,如果等于就表示可以模仿成功。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 char c[1010][1010];
 5 int b[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
 6 int main(){
 7     int n,m,i,j,o;
 8     while(cin>>n>>m){
 9         int sum=0;
10         for(i=1;i<=n;i++)
11         for(j=1;j<=m;j++)
12         {
13             cin>>c[i][j];
14             if(c[i][j]=='#') sum++;
15         }
16         int cnt=0; 
17         for(i=2;i<=n-1;i++)
18         {
19             for(j=2;j<=m-1;j++)
20             {   
21               int md=0;
22                 for(o=0;o<8;o++){//遍历周围8个方向,看是否是可以画的,1表示之前已经画过的,但还是可以画的 
23                     if(c[ i+ b[o][0] ][ j+b[o][1] ]=='#'||c[ i+ b[o][0] ][ j+b[o][1] ]=='1')
24                     md++;
25                     else continue;
26                 }
27                 if(md==8) {
28                     for(o=0;o<8;o++){
29                     if(c[ i+ b[o][0] ][ j+b[o][1] ]=='#')//为了不重复计数,只有表示#的可以计数 
30                       cnt++; //表示已经画过的#,如果最后和sum(总的#数)相等,就表示可以画,输出yes 
31                       c[ i+ b[o][0] ][ j+b[o][1] ]='1';
32                     }
33                 }
34             }
35          }
36          if(cnt==sum) cout<<"YES
";
37          else cout<<"NO
"; 
38     }
39         return 0;
40 } 
原文地址:https://www.cnblogs.com/fqfzs/p/9873766.html