其他分享
首页 > 其他分享> > leetcode求岛屿的个数和最大周长

leetcode求岛屿的个数和最大周长

作者:互联网

leetcode求岛屿的个数和最大周长

题目:

给定一个0和1组成的网格,0表示水域,1表示岛屿。岛屿的组成只能是垂直方向相连或者水平方向相连。组成岛屿的1是正方形。
求:网格中岛屿的个数和岛屿最大的周长

解题思路:

Java代码:

package com.zl.test;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @Author : archer
 * @Date :
 * @Description :
 * @Version : 1.0
 */
public class Test {
	public static void main(String[] args) {
		String a = "1/4/4/0000000001100000";
		String b = "2/3/4/000001000011";
		String c = "1/3/4/000001000110";

		test(a);
		System.out.println();
		test(b);
		System.out.println();
		test(c);

	}

	private static void test(String a){
		String[] split = a.split("/");
		// 岛屿数量
		int islandisNumber = Integer.parseInt(split[0]);
		// 行
		int row = Integer.parseInt(split[1]);
		// 列
		int col = Integer.parseInt(split[2]);

		// 创建网格岛屿二维数组
		int[][] grid = new int[row][col];

		// 初始化网格数据
		int h = 0;
		for (int i = 0; i < row ; i++) {
			for (int j = 0; j < col ; j++) {

				h += 1;
				grid[i][j] = Integer.parseInt(split[3].substring(h-1,h));
			}
		}

		// 遍历网格,查看数据是否封装正确
		traverseArray(grid);

		// 判断网格是否满足条件:即网格中是否有岛屿,也即二维数组中是否有1
		boolean island = island(grid);
		if(island){// 满足
			// 周长

			ArrayList<Integer> perimeters = getPerimeter(grid);
			int perimeter = perimeters.stream().max(Integer::compareTo).get();

			System.out.println("岛屿个数:" + islandisNumber);
			System.out.println("岛屿周长:" + perimeter);
		}else {// 不满足
			System.out.println("format error missing data!");
		}


	}

	/**
	 * 遍历数组
	 * @param array
	 */
	private static void traverseArray(int[][] array){
		for (int i = 0; i <array.length ; i++) {
			System.out.println();
			for (int j = 0; j <array[i].length ; j++) {
				System.out.print(array[i][j] + " ");
			}
		}
	}

	/**
	 * 判断网格是否满足条件
	 * @param array
	 * @return
	 */
	private static boolean island(int[][] array){
		for (int i = 0; i <array.length ; i++) {
			for (int j = 0; j <array[i].length ; j++) {
				if(array[i][j] == 0 && array[i][j] == 1){
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * 计算周长
	 * @param grid
	 * @return
	 */
	private static ArrayList<Integer> getPerimeter(int[][] grid) {

		int perimeter = 0;
		int island = 0;


		ArrayList<Integer> perimeters = new ArrayList<>();
		for (int i = 0; i <grid.length ; i++) {
			for (int j = 0; j <grid[i].length ; j++) {
				if(grid[i][j] == 1) {
					HashMap<String, Integer> map = new HashMap<>();
					map.put("perimeter",perimeter);
					map.put("island",island);
					map.put("island",map.get("island")+1);
					dfs(grid,i,j,map);
					perimeters.add(map.get("perimeter"));
				}

			}

		}
		return perimeters;
	}

	private static void dfs(int[][] grid, int i, int j,HashMap<String, Integer> map){
		if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != 1){
			return;
		}

		if(i==0 || (i-1 >= 0 && grid[i-1][j] == 0)) map.put("perimeter",map.get("perimeter") + 1);// 上
		if(j==grid[i].length -1 || (j+-1 <= grid[i].length-1 && grid[i][j+1] == 0)) map.put("perimeter",map.get("perimeter") + 1); // left
		if(i==grid.length -1 || (i+1 <= grid.length -1 && grid[i+1][j] == 0)) map.put("perimeter",map.get("perimeter") + 1);// down
		if(j==0 || (j-1 >= 0 && grid[i][j-1] == 0)) map.put("perimeter",map.get("perimeter") + 1);// right

		grid[i][j] = 2;

		dfs(grid, i - 1, j,map); // 上
		dfs(grid, i,j+1, map);// 右
		dfs(grid, i+1, j ,map);// 下
		dfs(grid, i, j - 1,map);// 左

	}


}



标签:perimeter,map,周长,int,island,个数,grid,岛屿,leetcode
来源: https://blog.csdn.net/qq_42709766/article/details/117151524