有一个n*n的矩阵,矩阵由 "0"和"1"组成,检测矩阵中的由"1"组成的图形是一个实心的正方形,结果为true或者false

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Main{


    public static void main(String[] args) throws IOException {
        // true
        int[][] testData1 = {
                {1, 1, 1, 1},
                {1, 1, 1, 1},
                {1, 1, 1, 1},
                {1, 1, 1, 1}
        };


        // false
        int[][] testData2 = {
                {1, 1, 1, 1},
                {1, 0, 0, 1},
                {1, 0, 0, 1},
                {1, 1, 1, 1}
        };


        // true
        int[][] testData3 = {
                {0, 0, 1, 1},
                {0, 0, 1, 1},
                {1, 1, 0, 0},
                {1, 1, 0, 0}
        };


        // false
        int[][] testData4 = {
                {0, 0, 1, 1},
                {0, 0, 1, 1},
                {0, 1, 0, 0},
                {1, 1, 0, 0}
        };


        // true
        System.out.println(findSquare(4, testData1));
        // false
        System.out.println(findSquare(4, testData2));
        // true
        System.out.println(findSquare(4, testData3));
        // false
        System.out.println(findSquare(4, testData4));


    }




    private static boolean findSquare(int n, int[][] data) {
        boolean flag = false;
        int totalCount = 0;
        List<Map<Integer, Integer>> dataXY = new ArrayList<Map<Integer, Integer>>();  //保存统计过的1的位置
        if(data != null || data.length != 0){
        for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
        if(data[i][j] == 1){
        totalCount++;
        }
        }
        }
        if(totalCount == 0){
        return false;
        }else if(totalCount == n*n){
        return true;
        }else{
        for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
        boolean isFindSquare = false;
        int count1 = 0;
               int count2 = 0;
        if(data[i][j] == 1){
        for(int u = 0; u < dataXY.size(); u++){
            if(dataXY.get(u).get(j) != null && dataXY.get(u).get(j) == i){
            isFindSquare = true;
            }
            }
        }
        if(data[i][j] == 1 && !isFindSquare){
        for(int a = i; a < n; a++){
        if(data[a][j] == 1){
        count1++;
        }else{
        break;
        }
        }
        for(int b = j; b < n; b++){
        if(data[i][b] == 1){
        count2++;
        }else{
        break;
        }
        }
        if(count1 == count2){
        boolean tempFlag=true;
        for(int x = i; x < i+count1; x++){
        for(int y = j;y < j+count2; y++){
        if(data[x][y] == 0){
        return false;
        }else{
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();  
        map.put(y, x);
        dataXY.add(map);
        }
        }
        }
        flag = tempFlag;
        }else{
        return false;
        }
        }
        }
        }
       
        }
        }
        return flag;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值