【NHOI2018】扫雷完成图

【题目描述】
扫雷游戏完成后会显示一幅图,图中标示了每个格子的地雷情况。现在,一个 n * n 方阵中有 k 个地雷,请你输出它的扫雷完成图。
【输入数据】
输入共 k+1 行:
第 1 行为 2 个整数 n、k,用一个空格隔开,表示扫雷图方阵的规模和地雷的总数。接下来 k 行,每行 2 个整数,表示一个地雷的行、列坐标。
【输出数据】
输出共 n 行,每行连续 n 个字符,每个字符对应扫雷完成图的一个格子。字符内容定义如下:(1)若该位置对应的格子是地雷,则输出“*”(英文星号);(2)若该位置对应的格子不是地雷,其相邻格(指环绕它的左上、上、右上、右、右下、下、左下、左,共 8 个格子)有地雷,则输出相邻格地雷总数;(3)若该位置对应的格子不是地雷,其相邻格也没有地雷,则输出空格。
【样例输入】
9 5
2 7
3 7
8 9
9 9
3 5
【样例输出】

【解题思路】
模拟,周围八格判断,送分题
【参考程序】

#include<iostream>
#include<cstdio>
using namespace std;
const int dx[8]={0,-1,0,1,1,-1,-1,1};
const int dy[8]={-1,0,1,0,-1,-1,1,1};
int n,k,x,y,a[40][40];
int main()
{
    //freopen("minemap.in","r",stdin);
    //freopen("minemap.out","w",stdout);
    scanf("%d%d",&n,&k);
    for (int i=1;i<=k;i++) 
    {
        scanf("%d%d",&x,&y);
        a[x][y]=-1;
    }
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            if (a[i][j]!=-1)
            {
                for (int k=0;k<8;k++)
                    if (a[i+dx[k]][j+dy[k]]==-1) a[i][j]++; 
            }
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=n;j++)
        {
            if (a[i][j]==-1) cout<<'*';
            else if (a[i][j]==0) cout<<' ';
            else cout<<a[i][j];
        }
        cout<<endl;
    }
    return 0;
}
posted @ 2018-06-07 09:43  Nanjo  阅读(439)  评论(0编辑  收藏  举报