复赛2:扫雷游戏

洛谷:P2670

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**************************************************************** 
 * Description: 2015年普及组复赛第二题,扫雷游戏
 * Author: Alex Li
 * Date: 2022-10-06 08:05:45
 * LastEditTime: 2023-10-30 11:31:17
****************************************************************/
#include <iostream>
using namespace std;
char a[101][101]; //原数据
int b[101][101];  //成生数据
int main(){
    int c,d;
    cin>>c>>d;
    for (int i =1; i <=c; i++){
        for (int j= 1; j <=d; j++){
            cin>>a[i][j];
        }        
    }
    //如果a[i][j]是*,上、下、左、右,下右、下左、上右、上左,共8个方向判断
    for (int i =1; i <=c; i++){
        for (int j =1 ; j<=d; j++){
             if((j-1)>0&&a[i][j]=='*'&&a[i][j-1]!='*')b[i][j-1]++;
             if((i-1)>0&&a[i][j]=='*'&&a[i-1][j]!='*')b[i-1][j]++;
             if((j+1)<=d&&a[i][j]=='*'&&a[i][j+1]!='*')b[i][j+1]++;
             if((i+1)<=c&&a[i][j]=='*'&&a[i+1][j]!='*')b[i+1][j]++;
             if((j-1)>0&&(i-1)>0&&a[i][j]=='*'&&a[i-1][j-1]!='*')b[i-1][j-1]++;
             if((j+1)<=d&&(i-1)>0&&a[i][j]=='*'&&a[i-1][j+1]!='*')b[i-1][j+1]++;
             if((j-1)>0&&(i+1)<=c&&a[i][j]=='*'&&a[i+1][j-1]!='*')b[i+1][j-1]++;
             if((i+1)<=c&&(j+1)<=d&&a[i][j]=='*'&&a[i+1][j+1]!='*')b[i+1][j+1]++;
        }
    }
    //输出 
     for (int i = 1; i <=c; i++) {
        for (int j =1; j<=d; j++){
            if(a[i][j]=='*')cout<<'*';
            else cout<<b[i][j];
            }
        cout<<endl;
    }
return 0;    
}
Scroll to Top