扫雷(dfs)

扫雷(dfs),第1张

扫雷(dfs)
#include "bits/stdc++.h"

using namespace std;
int dir[8][2]={{0,1},{1,0},{0,-1},{-1,0},{1,1},{-1,-1},{1,-1},{-1,1}};
char s[55][55];
int n,m,a,b;
int check(int x,int y){
    int cnt = 0;
    for(int i=0;i<8;i++){
        int tx = dir[i][0] + x;
        int ty = dir[i][1] + y;
        if(tx < 0 || ty < 0 || tx > n-1 || ty > m-1) continue;
        if(s[tx][ty] == 'M') cnt++;
    }
    return cnt;
}
void dfs(int x , int y){
    s[x][y] = check(x,y) + '0';
    if(s[x][y] != '0') return;
    for(int i=0;i<8;i++){
        int tx = dir[i][0] + x;
        int ty = dir[i][1] + y;
        if(tx < 0 || ty < 0 || tx > n-1 || ty > m-1) continue;
        if(s[tx][ty] != 'E') continue;
        dfs(tx,ty);
    }
}

int main()
{
    while (cin >> n >> m){
        for(int i=0;i> s[i][j];
            }
        }
        cin >> a >> b;
        if(s[a][b] == 'M') s[a][b]='X';
        else dfs(a,b);
        for(int i=0;i 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5692293.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存