#40459: C++


11131039@stu.tshs.tp.edu.tw (二孝25林孟希)

學校 : 不指定學校
編號 : 201083
來源 : [36.227.70.244]
最後登入時間 :
2024-05-22 06:16:38
n700. 蝸牛的踩地雷攻略 3 (點方塊) -- 板橋高中教學題 | From: [36.227.70.244] | 發表日期 : 2024-05-22 06:18

#include <bits/stdc++.h>
using namespace std;

int n,m,r,c;
char board[32][32];
char display[32][32];

int around(int x, int y)//判斷周圍地雷數
{
    int cnt=0;
    for(int a=x-1;a<=x+1;a++)
    {
        for(int b=y-1;b<=y+1;b++)
        {
            if(board[a][b] == '*' && !(a == x && b == y)) cnt++;
        }
    }
    return cnt;
}

void touch(int x, int y)//點擊
{
    if(around(x,y) == 0)
    {
        display[x][y] = '_';
        for(int a=x-1;a<=x+1;a++)
        {
            for(int b=y-1;b<=y+1;b++)
            {
                if(display[a][b] == '#' && x>=1 && x<=n && y>=1 && y<=m) touch(a,b);
            }
        }
    }
    else
    {
        display[x][y] = around(x,y)+'0';
    }
}

int main()
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            board[i][j] = '0';
        }
    }
    cin>>n>>m>>r>>c;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>board[i][j];
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            display[i][j] = '#';
        }
    }
    touch(r,c);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cout<<display[i][j];
        }
        cout<<endl;
    }
}

 
ZeroJudge Forum