1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7 
 8 class Solution 
 9 {
10 public:
11     int maxAreaOfIsland(vector<vector<int>>& grid) 
12     {
13         int x=grid.size();
14         int y=grid[0].size();
15         int res=0;
16         for(int i=0;i<x;i++)
17         {
18             for(int j=0;j<y;j++)
19             {
20                 res=max(res,countarea(grid,i,j));
21             }
22         }
23         return res;
24     }
25     
26     int countarea(vector<vector<int>> &grid,int i,int j)
27     {
28         int szx=grid.size();
29         int szy=grid[0].size();
30         int count=0;
31         if(grid[i][j]==1)
32         {
33             count++;
34             grid[i][j]=0;
35             if(valid(i-1,j,szx,szy)) count+=countarea(grid,i-1,j);
36             if(valid(i,j-1,szx,szy)) count+=countarea(grid,i,j-1);
37             if(valid(i+1,j,szx,szy)) count+=countarea(grid,i+1,j);
38             if(valid(i,j+1,szx,szy)) count+=countarea(grid,i,j+1);
39             return count;
40         }      
41         return 0;    
42     }
43     
44     bool valid(int i,int j,int szx,int szy)
45     {
46         return i>=0&&i<szx&&j>=0&&j<=szy;
47     }
48     
49 };

递归,问题不大

posted on 2018-06-10 15:56  高数考了59  阅读(133)  评论(0)    收藏  举报