题目描述
在一个群岛上,有一个富可敌国的大富翁。他打算在这个群岛上建造一个最大城堡,也就是群岛上最大的岛屿。
输入
第一行是一个整数T,代表测试数据的组数。每组数据中第一行是两个整数n,m,代表地图的大小。接下来n行每行共m个整数。0代表海洋,1代表陆地。其中T<=50,n,m<=200
输出
共T行,最大的面积。
样例输入
1
5 5
0 1 1 0 0
1 1 0 0 0
0 0 1 1 0
0 1 1 1 1
0 0 1 1 0
样例输出
8
import java.util.*; public class Main { static int [][]next=new int[][]{{0,1},{1,0},{0,-1},{-1,0}}; public static void main(String[] args) { Scanner cin = new Scanner(System.in); int t=cin.nextInt(); while (t-->0) { int n=cin.nextInt(); int m=cin.nextInt(); List<Pos> pst=new Vector<Pos>(); for(int x=0;x<n;x++){ for(int y=0;y<m;y++){ int k=cin.nextInt(); if(k==1){ pst.add(new Pos(x,y)); } } } Map<Pos,Boolean> V=new HashMap<Pos,Boolean>(); for(Pos pt:pst) { V.put(pt, false); } int count=0; for(Pos pt:pst){ if(V.get(pt)) continue; Queue<Pos> Q=new LinkedList<>(); Q.offer(pt); int tot=0; while(!Q.isEmpty()){ Pos p=Q.peek(); int x=p.x; int y=p.y; V.put(p,true); Q.poll(); ++tot; for(int i=0;i<next.length;i++){ int nx=x+next[i][0]; int ny=y+next[i][1]; Pos np=new Pos(nx,ny); if(V.containsKey(np)&&!V.get(np)){ Q.offer(np); V.put(np,true); } } } if(count<tot) count=tot; } System.out.println(count); } cin.close(); } private static class Pos { int x; int y; public Pos(int x, int y) { this.x=x; this.y=y; } public boolean equals(Object that) { if(that instanceof Pos) { Pos pos = (Pos) that; return this.x == pos.x && this.y == pos.y; } return false; } public int hashCode() { return x+1000*y; } public String toString() { return String.format("(%d %d)",x,y); } } }