Java solves island perimeter problem

Java solves island perimeter problem

01 Question

Given a two-dimensional grid map of row x col grid , where: grid[i][j] = 1 represents land, grid[i][j] = 0 represents water.

The cells in the grid are connected horizontally and vertically (diagonally not). The entire grid is completely surrounded by water, but there happens to be one island (or, one or more connected islands that represent land).

There is no "lake" in the island ("lake" means the water is inside the island and is not connected to the water around the island). A grid is a square with side length 1. The grid is rectangular, with width and height no greater than 100 . Calculate the perimeter of this island.

Example 1:

img

输入:grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
输出:16
解释:它的周长是上面图片中的 16 个黄色的边

Example 2:

输入:grid = [[1]]
输出:4

Example 3:

输入:grid = [[1,0]]
输出:4

hint:

  • row == grid.length
  • col == grid[i].length
  • 1 <= row, col <= 100
  • grid[i][j]0 1

02 Knowledge points

  • Two-dimensional array
  • DFS, iterative loop solution

03 My solution

public class shuzu06 {
    
    
	public static void main(String[] args) {
    
    
        //测试数据
		int[][] grid=new int[][] {
    
    
			{
    
    0,1,0,0},
			{
    
    1,1,1,0},
			{
    
    0,1,0,0},
			{
    
    1,1,0,0}
		};
		System.out.println(numSpecial(grid));
	}
public static int numSpecial(int[][] grid) {
    
    
	int count=0;//用于记录周长
	int row = grid.length;//获取排数
	int col = grid[0].length;//获取列数
	if (row==1&&col==1) {
    
    
		count=4;//当只有一格时,返回周长4
		return count;
	}
	for (int i = 0; i < row; i++) {
    
    
        //循环每一排
		boolean flag=false;//用于判断是否数组值为一,即满足第一条件
		for (int j = 0; j < col; j++) {
    
    
            //循环每列,默认初始每块小岛周长为4
			int bolder=4;
			if (grid[i][j]==1) {
    
    
                //满足第一条件,执行第二语句
				flag=true;
			}
			if (flag) {
    
    
    //分两种情况讨论,周围两排是否有小岛和周围两列是否有小岛
				if (row>1) {
    
    
                    //当处于中间排而不是两边排时,判断两边是否有小岛,有则该小岛的周长减一
					if (i>0&&i<row-1) {
    
    
						if (grid[i-1][j]==1) {
    
    
							bolder--;
						}
						if (grid[i+1][j]==1) {
    
    
							bolder--;
						} //当处于两边排时,判断另边是否有小岛,有则该小岛的周长减一
					}else if (i==0) {
    
    
						if (grid[i+1][j]==1) {
    
    
							bolder--;
						}
					} else {
    
    
						if (grid[i-1][j]==1) {
    
    
							bolder--;
						}
					}		
				}
				//周围两列与周围两排原理相同
				if (col>1) {
    
    
					if (j>0&&j<col-1) {
    
    
						if (grid[i][j-1]==1) {
    
    
							bolder--;
						}
						if (grid[i][j+1]==1) {
    
    
							bolder--;
						}
					}else if (j==0) {
    
    
						if (grid[i][j+1]==1) {
    
    
							bolder--;
						}
					} else {
    
    
						if (grid[i][j-1]==1) {
    
    
							bolder--;
						}
					}
				}
				}
				//总周长加上该小岛周长,并把循环默认改为false
			if (flag) {
    
    
				count+=bolder;
				flag=false;
			}
		}
		}
	return count;
}
}

Guess you like

Origin blog.csdn.net/2302_77182979/article/details/134915998