题解 | 扫雷

扫雷

https://www.nowcoder.com/practice/d5f277427d9a4cd3ae60ea6c276dddfd

#include <iostream>
using namespace std;
char arr[1007][1007];

int main() {
    int n,m;
    cin>>n>>m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> arr[i][j];
        }
    }
    int dx[]={-1,-1,-1,0,0,1,1,1};
    int dy[]={-1,0,1,-1,1,-1,0,1};
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(arr[i][j]=='*'){
                cout<<'*';
                }else{
                int num=0;
                for(int k=0;k<8;k++){
                    int x=i+dx[k];
                    int y=j+dy[k];
                    if(x>=1&&x<=n&&y>=1&&y<=m){
                        if(arr[x][y]=='*'){
                            num++;
                        }
                    }
                }
                cout<<num;
             }   
        }
        cout<<endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务