返回顶部

Minesweeper

一个简单的扫雷游戏模拟。

#include <bits/stdc++.h>
using namespace std;
int h,w;
char s[51][51];
int f(int x,int y){
    int ans=0;
    if (s[x-1][y-1] == '#') ans++;
    if (s[x-1][y] == '#') ans++;
    if (s[x-1][y+1] == '#') ans++;
    if (s[x][y-1] == '#') ans++;
    // if (s[x][y] == '#') // 中间不算
    if (s[x][y+1] == '#') ans++;
    if (s[x+1][y-1] == '#') ans++;
    if (s[x+1][y] == '#') ans++;
    if (s[x+1][y+1] == '#') ans++;
    return ans;
}
int main(){
    cin>>h>>w;
    for (int i=0;i<h;i++){
        for (int j=0;j<w;j++){
            cin>>s[i][j];
        }
    }
    for (int i=0;i<h;i++){
        for (int j=0;j<w;j++){
            if (s[i][j] == '.'){
                int temp = f(i,j);
                s[i][j] = temp + '0';
            }
        }
    }
    for (int i=0;i<h;i++){
        for (int j=0;j<w;j++){
            cout<<s[i][j];
        }
        cout<<endl;
    }
    // system("pause");
    return 0;
}

这里是用了f函数以检测四周的地雷情况,返回地雷数量。

for循环中通过temp+'0'将数字转字符并储存

就可以愉快的AC啦~

posted @ 2022-01-20 21:51  zrc4889  阅读(87)  评论(0编辑  收藏  举报