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;
}
}
AcWing687-经典扫雷问题-dfs
最新推荐文章于 2024-07-20 09:51:47 发布