AcWing687-经典扫雷问题-dfs

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/14 22:05
 * @description https://www.acwing.com/problem/content/169/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 310;
    static char[][] a = new char[N][N];
    static int[][] g = new int[N][N];   //g[i][j]:ij这个周围有多少雷
    static int t,n,res;

    //把所有空位置的连通块和周围的数字点开
    //点到数字后就不能继续往下点了
    public static void dfs(int x,int y){
        int t = g[x][y];
        g[x][y] = -1;
        if (t > 0) return;

        for(int i = x - 1;i <= x + 1;i++){
            for(int j = y - 1;j <= y + 1;j++){
                //把没修改过的空连通块和有数字的点都置为-1
                if (i >= 0 && i < n && j >= 0 && j < n && g[i][j] != -1){
                    dfs(i,j);
                }
            }
        }
    }

    public static void main(String[] args) {
        t = sc.nextInt();
        for(int Case = 1;Case <= t;Case++){
            n = sc.nextInt();
            for(int i = 0;i < n;i++){
                a[i] = sc.next().toCharArray();

            }

            //初始化每个点是0还剩1~8还是负数
            for(int i = 0;i < n;i++){
                for(int j = 0;j < n;j++){
                    if (a[i][j] == '*') g[i][j] = -1;
                    else {
                        g[i][j] = 0;    //先初始化为0,有多组测试数据
                        for(int dx = i - 1;dx <= i + 1;dx++){
                            for(int dy = j - 1;dy <= j + 1;dy++){
                                if (dx >= 0 && dx < n && dy >= 0 && dy < n && a[dx][dy] == '*'){
                                    g[i][j]++;
                                }
                            }
                        }
                    }
                }
            }

           

            //把所有0的连通块给搜出来,最多只能搜到0的连通块外面一层(即有数据那一层)
            int res = 0;
            for(int i = 0;i < n;i++){
                for(int j = 0;j < n;j++){
                    if (g[i][j] == 0){
                        res ++;
                        dfs(i,j);
                    }
                }
            }

            
            
            //如果有一个点(有数字的点)周围被其他数字都保护住了,那么这个点没法被搜到
            for(int i = 0;i < n;i++){
                for(int j = 0;j < n;j++){
                    if (g[i][j] != -1){
                        res ++;
                    }
                }
            }
            out.printf("Case #%d: %d\n",Case,res);
        }
        out.close();
    }


}

class PII{
    int a,b,c;

    public PII(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值