参考代码:
// 初始化是真TM重要,被折磨死了
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main
{ static int N = 55;
static int m, n;
static boolean vis[][] = new boolean[N][N], used[][] = new boolean[N][N];
static char[][] map = new char[N][N];
public static void main(String[] args)
{ Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
while(T-- > 0)
{ m = scanner.nextInt();
n = scanner.nextInt();
for(int i = 0; i < m; i++)
map[i] = scanner.next().toCharArray();
int res = 0;
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
if(!vis[i][j] && map[i][j] == '1')
{ bfs_island(i, j);
if(bfs_sea(i, j))//每次去对岛屿进行bfs,然后再去对周围的海水bfs,判断海水是否能流到地图边界
res++;
}
System.out.println(res);
for(boolean[] t: vis)
Arrays.fill(t, false);
}
}
static int dx[]={1,-1,0,0,1,1,-1,-1}, dy[]={0,0,1,-1,1,-1,1,-1};;
static void bfs_island(int x, int y)
{ Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{x, y});
vis[x][y] = true;
while(!queue.isEmpty())
{ int[] t = queue.remove();
for(int i = 0; i < 4; i++)
{ int tx = t[0] + dx[i];
int ty = t[1] + dy[i];
if(tx >= 0 && tx < m && ty >= 0 && ty < n && !vis[tx][ty] && map[tx][ty] == '1')
{ vis[tx][ty] = true;
queue.add(new int[]{tx, ty});
}
}
}
}
static boolean bfs_sea(int x, int y)
{ for(boolean[] t: used)
Arrays.fill(t, false);
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{x, y});
used[x][y] = true;
while(!queue.isEmpty())
{ int[] t = queue.remove();
if(t[0] == 0 || t[0] == m - 1 || t[1] == 0 || t[1] == n - 1)
return true;
for(int i = 0; i < 8; i++)
{ int tx = t[0] + dx[i];
int ty = t[1] + dy[i];
if(tx >= 0 && tx < m && ty >= 0 && ty < n && !used[tx][ty] && map[tx][ty] == '0')
{ queue.add(new int[]{tx, ty});
used[tx][ty] = true;
}
}
}
return false;
}
}0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复