深搜之水池数目

编程入门 行业动态 更新时间:2024-10-27 20:26:32

深搜之水池<a href=https://www.elefans.com/category/jswz/34/1765664.html style=数目"/>

深搜之水池数目

RT  .php?pid=27

直接贴代码:

import java.util.*;
import java.io.*;


public class Main {
private static int[][] d = { { 0, -1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } };

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();// 表示有n组测试数据
while (N-- > 0) {
int m = sc.nextInt();// 表示行数
int n = sc.nextInt();// 表示列数
int count = 0;
int[][] map = new int[m][n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
map[i][j] = sc.nextInt();// 1表示有水池 0表示没有水池
}


for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
if (map[i][j] == 1) {
count++;
DFS(i, j, map,m,n);

}

}
System.out.println(count);


}


}


public static void DFS(int x0, int y0, int[][] map,int m,int n) {
map[x0][y0] = -1; // 表示已经访问过
for (int i = 0; i < 4; i++) {
int x1=x0+d[i][0];
int y1=y0+d[i][1];
if(x1>=0&&x1<m&&y1>=0&&y1<n&&map[x1][y1]==1)
{
DFS(x1, y1, map,m,n);
}


}


}
}

更多推荐

深搜之水池数目

本文发布于:2024-03-12 08:29:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1731106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:水池   数目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!