复赛二:扫雷游戏

洛谷:P2670
OJ:P4947

泛洪算法:

写法一:

 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;    
}

写法二:

 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**************************************************************** 
 * Description: 2015_J_semi_2  写法二
 * Author: Alex Li
 * Date: 2024-09-18 17:13:33
 * LastEditTime: 2024-09-18 17:13:37
****************************************************************/
#include <iostream>
using namespace std;

const int MAX_SIZE = 101;

char grid[MAX_SIZE][MAX_SIZE]; // 原始雷区数据
int count[MAX_SIZE][MAX_SIZE]; // 每个非地雷格周围的地雷数

int main() {
    int n, m; // n: 行数,m: 列数
    cin >> n >> m;

    // 读取雷区数据
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> grid[i][j];
            count[i][j] = 0; // 初始化计数数组
        }
    }

    // 定义八个方向的偏移量
    int dx[] = { -1, -1, -1,  0, 0, 1, 1, 1 };
    int dy[] = { -1,  0,  1, -1, 1, -1, 0, 1 };

    // 遍历每个格子
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            // 如果当前格子是地雷
            if (grid[i][j] == '*') {
                // 检查周围八个方向
                for (int k = 0; k < 8; k++) {
                    int ni = i + dx[k];
                    int nj = j + dy[k];
                    // 判断是否在雷区范围内且不是地雷格
                    if (ni >= 0 && ni < n && nj >= 0 && nj < m && grid[ni][nj] != '*') {
                        count[ni][nj]++;
                    }
                }
            }
        }
    }

    // 输出结果
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (grid[i][j] == '*')
                cout << '*';
            else
                cout << count[i][j];
        }
        cout << endl;
    }

    return 0;
}
Scroll to Top