codeforces 984 B. Minesweeper

题目链接:http://codeforces.com/contest/984/problem/B

题意:给你一个n*m的字符数组,包含1到9,‘*’和‘.’。如果每一个数字所在的九宫格里有数字这么多个‘*’且每一个‘.’的九宫格没有‘*’,输出YES,否则输出NO

分析:直接枚举判断就可以了

AC代码:

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 
 6 char a[105][105];
 7 bool judge(int x,int y,int d){
 8     int num=0;
 9     for(int i=-1;i<=1;i++){
10         for(int j=-1;j<=1;j++){
11             if(a[x+i][y+j]=='*'){
12                 num++;
13             }
14         }
15     }
16     if(num==d) return true;
17     return false;
18 }
19 int main(){
20     ios_base::sync_with_stdio(false);
21     cin.tie(0);
22     int n,m;
23     cin>>n>>m;
24     memset(a,'.',sizeof(a));
25     for(int i=1;i<=n;i++){
26         for(int j=1;j<=m;j++){
27             cin>>a[i][j];
28         }
29     }
30     int p=0;
31     for(int i=1;i<=n;i++){
32             if(p==1) break;
33         for(int j=1;j<=m;j++){
34             if(p==1) break;
35             if(a[i][j]>='1'&&a[i][j]<='8'){
36                 int d=a[i][j]-'0';
37                 if(!judge(i,j,d)){
38                     p=1;
39                     break;
40                 }
41             }
42             else if(a[i][j]=='.'){
43                 int d=0;
44                 if(!judge(i,j,d)){
45                     p=1;
46                     break;
47                 }
48             }
49             if(p==1){
50                 break;
51             }
52         }
53     }
54     if(p==0){
55         cout<<"YES"<<endl;
56     }
57     else {
58         cout<<"NO"<<endl;
59     }
60 
61 return 0;
62 }
View Code

猜你喜欢

转载自www.cnblogs.com/ls961006/p/9055791.html