[怎样在形参里传数组的引用看到后补充]leetcode - 695. Max Area of Island【回溯法 + 图的遍历 】

转载链接点击打开链接

题目

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,1,1,0,1,0,0,0,0,0,0,0,0],
 [0,1,0,0,1,1,0,0,1,0,1,0,0],
 [0,1,0,0,1,1,0,0,1,1,1,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,0,0,0,0,0,0,1,1,0,0,0,0]]
Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

Example 2:

[[0,0,0,0,0,0,0,0]]
Given the above grid, return 0.

Note: The length of each dimension in the given grid does not exceed 50.

题意

给定一个二维数组,计算其中最大岛屿的面积。(注意形成岛屿的规则)

分析及解答

  • 遍历法】下面的解法,将问题当作图来进行求解(使用了visiteds数组用来记录访问状态)。
  • 实现方式】深度优先遍历 ,回溯法。

我自己的方法,效率比价低

class Solution {
public:
    int go[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//下上右左
    int count;
    void dfs(vector<vector<int>>& grid,vector<vector<bool>> &mark,int x,int y,int& count)
    {
        
        for(int i=0;i<4;i++)
       {
           int nx=x+go[i][0];
           int ny=y+go[i][1];
           if(nx<0||ny<0||nx>=grid.size()||ny>=grid[0].size()) continue;
           if(mark[nx][ny]==true) continue;
           if(grid[nx][ny]==0) continue;
           mark[nx][ny]=true;
           count++;
           dfs(grid,mark,nx,ny,count);
       }
    }
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        //grid={{1}};
        if(grid.empty()) return 0;
        int row=grid.size();
        int col=grid[0].size();
        vector<vector<bool>> mark(row,vector<bool>(col,false));
        int max=0;
        for(int i=0;i<=row-1;i++){
            for(int j=0;j<=col-1;j++){
                if(mark[i][j]==true) continue;
                if(grid[i][j]==0) continue;
                count=1;
                mark[i][j]=true;
                dfs(grid,mark,i,j,count);
                max=count>max?count:max;
            }
        } 
        return max;
    }
};

在discuss上看到的一个效率高的

class Solution {
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int max = 0;
        for (int i = 0 ; i < grid.size() ; ++i) {
            for (int j = 0 ; j < grid[i].size() ; ++j) {
                if (grid[i][j] == 1) {
                    int tmp = count(grid, i, j);
                    if (tmp > max) {
                        max = tmp;
                    }
                }
            }
        }
        return max;
    }
    
    int count(vector<vector<int>>& grid, int i, int j) {
        if (i < 0 || i == grid.size() || j < 0 || j == grid[i].size() || grid[i][j] == 0) {
            return 0;
        }
        grid[i][j] = 0;
        int num = 1;
        num +=
            count(grid, i - 1, j) +
            count(grid, i + 1, j) +
            count(grid, i, j + 1) +
            count(grid, i, j - 1);
        return num;
    }
};

还有这个类似

class Solution {
public:
    int search(int row, int column, vector<vector<int>>& grid) {
        if (row >= 0 && column >= 0 && row < grid.size() && column < grid[row].size() && grid[row][column] > 0) {
            grid[row][column] = 0;
            return 1 + search(row + 1, column, grid) + search(row - 1, column, grid) + search(row, column + 1, grid) + search(row, column - 1, grid);
        }
        return 0;
    }
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int maxArea = 0;
        for (int i = 0; i < grid.size(); i++) {
            for (int j = 0; j < grid[i].size(); j++) {
                maxArea = max(search(i, j, grid), maxArea);
            }
        }
        return maxArea;
    }
};

[java]  view plain  copy
  1. class Solution {  
  2.       
  3.     public int maxAreaOfIsland(int[][] grid) {  
  4.         if (grid == null || grid.length == 0) {  
  5.             return 0;  
  6.         }  
  7.           
  8.         int[][] visited = new int[grid.length][grid[0].length];  
  9.         int max = 0;  
  10.         for (int i = 0; i < grid.length; i++) {  
  11.             for (int j = 0; j < grid[0].length; j++) {  
  12.   
  13.                 if (grid[i][j] == 0) {  
  14.                     continue;  
  15.                 }  
  16.                 int result = find(grid,i,j,visited);  
  17.                 if(max < result){  
  18.                     max = result;  
  19.                 }  
  20.             }  
  21.         }  
  22.         return max;  
  23.     }  
  24. public int find(int[][] grid ,int x,int y, int[][] visited){  
  25.         if(grid[x][y] == 0 || visited[x][y] == 1){ //边界溢出。  
  26.             return 0;  
  27.         }  
  28.         int result = 1;  
  29.         visited[x][y] = 1;  
  30.         if(x + 1 < grid.length){  
  31.             result += find(grid, x+1, y, visited);  
  32.         }  
  33.         if(x -1 >= 0){  
  34.             result += find(grid, x-1, y, visited);  
  35.         }  
  36.         if(y + 1 < grid[0].length){  
  37.             result += find(grid,x,y+1,visited);  
  38.         }  
  39.         if(y -1 >= 0){  
  40.             result += find(grid,x,y-1,visited);  
  41.         }  
  42.         return result;  
  43.     }  
  44. }  
[java]  view plain  copy
  1. class Solution {  
  2.       
  3.     public int maxAreaOfIsland(int[][] grid) {  
  4.         if (grid == null || grid.length == 0) {  
  5.             return 0;  
  6.         }  
  7.           
  8.         int[][] visited = new int[grid.length][grid[0].length];  
  9.         int max = 0;  
  10.         for (int i = 0; i < grid.length; i++) {  
  11.             for (int j = 0; j < grid[0].length; j++) {  
  12.   
  13.                 if (grid[i][j] == 0) {  
  14.                     continue;  
  15.                 }  
  16.                 int result = find(grid,i,j,visited);  
  17.                 if(max < result){  
  18.                     max = result;  
  19.                 }  
  20.             }  
  21.         }  
  22.         return max;  
  23.     }  
  24. public int find(int[][] grid ,int x,int y, int[][] visited){  
  25.         if(grid[x][y] == 0 || visited[x][y] == 1){ //边界溢出。  
  26.             return 0;  
  27.         }  
  28.         int result = 1;  
  29.         visited[x][y] = 1;  
  30.         if(x + 1 < grid.length){  
  31.             result += find(grid, x+1, y, visited);  
  32.         }  
  33.         if(x -1 >= 0){  
  34.             result += find(grid, x-1, y, visited);  
  35.         }  
  36.         if(y + 1 < grid[0].length){  
  37.             result += find(grid,x,y+1,visited);  
  38.         }  
  39.         if(y -1 >= 0){  
  40.             result += find(grid,x,y-1,visited);  
  41.         }  
  42.         return result;  
  43.     }  
  44. }  

猜你喜欢

转载自blog.csdn.net/momo_mo520/article/details/80097612
今日推荐