2798:雄伟的城堡

题目描述
在一个群岛上,有一个富可敌国的大富翁。他打算在这个群岛上建造一个最大城堡,也就是群岛上最大的岛屿。

输入
第一行是一个整数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);
        }
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值