LGBT玩扫雷

LGBT玩扫雷

pic


题目

【题目描述】

在一个n * m 的棋盘上,有位置上有雷(用“*” 表示),其他位置是空地(用“.” 表示)。

LGTB 想在每个空地上写下它周围8 个方向相邻的格子中有几个雷。

请帮助他输出写了之后的棋盘

【输入输出格式】

输入格式:
第一行包含两个整数n, m 代表棋盘大小

接下来n 行,每行m 个字符,代表棋盘

1 n,m 1000

输出格式:
输出包含n 行,每行m 个字符,代表LGTB 写了数字之后的棋盘

【输入输出样例】

输入样例#1:

3 3
*.*
...
*.*

输出样例#1:

*2*
242
*2*

思路

一边输入一边更新:每出现一个新的地雷,就要把他周围的不是地雷的点都加一,然后直接输出就行啦!

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
#define FILE freopen("mine.in","r",stdin);freopen("mine.out","w",stdout);
using namespace std;
const int maxn=1005;
const int dirx[8]={-1,-1,-1, 0, 0, 1, 1, 1};
const int diry[8]={-1, 0, 1,-1, 1,-1, 0, 1};

int n,m,map[maxn][maxn];
string s;

inline bool can(int x,int y){
    return (x>=1 && x<=n && y>=1 && y<=m);
}

inline void Work(int a,int b){
    b=b+1;
    map[a][b]=-19260817;
    for(int i=0;i<=7;i++){
        int x=a+dirx[i],y=b+diry[i];
        if(map[x][y]>=0 && can(x,y))
            map[x][y]++;
    }
}

int main(){
    ios::sync_with_stdio(false);
//    FILE;
    memset(map,0,sizeof(map));
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>s;
        for(int j=0;j<m;j++){
            if(s[j]=='*')
                Work(i,j);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(map[i][j]<0)
                cout<<"*";
            else
                cout<<map[i][j];
        }
        cout<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/YQAccelerator/p/7413804.html