Perimeter of island

Keywords: network

Given a two-dimensional grid map containing 0 and 1, where 1 represents land and 0 represents waters.

Lattices in the grid are connected horizontally and vertically (not diagonally). The whole grid is completely surrounded by water, but there happens to be an island (or one or more islands connected by a grid representing land).

There is no "lake" in the island ("lake" means that the waters are within the island and not connected to the water around the island). A lattice is a square whose length is 1. The grid is rectangular, and its width and height are not more than 100. Calculate the circumference of the island.

 

Example:

Input:
[[0,1,0,0],
 [1,1,1,0],
 [0,1,0,0],
 [1,1,0,0]]

Output: 16

Explanation: Its circumference is 16 yellow edges in the following picture:

Source: LeetCode
Link: https://leetcode-cn.com/problems/island-perimeter
Copyright belongs to the seizure network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

 

class Solution {
public:
    int ret;
    int islandPerimeter(vector<vector<int>>& image) {
        ret = 0;
        vector<vector<int>> visited;
        vector<int> tmp(image[0].size(), 0);
        int newColor = 3;
        
        for(int i = 0; i < image.size(); i++)
        {
            visited.push_back(tmp);
        }
        int sr = 0, sc = 0;
        for(int i = 0; i < image.size(); i++)
        {
            int j = 0;
            for( ; j < image[i].size(); j++)
            {
                if(image[i][j] == 1)
                {
                    sr = i;
                    sc = j;
                    break;
                }
            }
            if(j < image[i].size())
                break;
        }
        int ori = image[sr][sc];

        
        fill(image, sr, sc, ori, newColor,visited);
        return ret;   
    }
    
    int fill(vector<vector<int>>& image, int sr, int sc, int ori, int newColor, vector<vector<int>>& visited)
    {
        if(!inarea(image, sr, sc))
        {
            return 0;
        }
        if(visited[sr][sc])
        {
            return 1;
        }
        if(image[sr][sc] != ori)
        {
            return 0;
        }
        
        visited[sr][sc] = 1;
        
        
        int num = fill(image, sr+1, sc, ori, newColor,visited) + 
        fill(image, sr, sc+1, ori, newColor,visited) + 
        fill(image, sr-1, sc, ori, newColor,visited) +
        fill(image, sr, sc-1, ori, newColor,visited);
        
        switch(num)
        {
            case 0:
                {
                    ret += 4;
                    break;
                }
            case 1:
                {
                    ret += 3;
                    break;
                }
            case 2:
                {
                    ret += 2;
                    break;
                }
            case 3:
                {
                    ret += 1;
                    break;
                }
            case 4:
                {
                    ret += 0;
                    break;
                }
                
        }
        return 1;
    }
    bool inarea(vector<vector<int>>& image, int x, int y)
    {
        return x >= 0 && x <= image.size() -1 &&
               y >= 0 && y <= image[0].size()-1;
    }
};

 

Posted by Warptweet on Fri, 04 Oct 2019 21:12:29 -0700