标签:public oid visit set tco style div nbsp mit
1 int x[] = {1,-1,0,0}; 2 int y[] = {0,0,1,-1}; 3 4 class Solution 5 { 6 private: 7 int acreList[1500]; 8 int acreListEnd; 9 int Maxresult; 10 int rowSize; 11 int colSize; 12 public: 13 int largestIsland(vector<vector<int>>& grid) 14 { 15 acreListEnd = 2; 16 acreList[0] = 0; 17 Maxresult = 1; 18 rowSize = grid.size(); 19 colSize = grid[0].size(); 20 for(int i = 0;i < grid.size();i ++) 21 { 22 for(int j = 0;j < grid[0].size();j ++) 23 { 24 int tmpAcre = 0; 25 if(grid[i][j] == 1) 26 { 27 preDfs(i,j,grid,tmpAcre); 28 acreList[acreListEnd ++] = tmpAcre; 29 } 30 } 31 } 32 33 /* 34 for(int i = 0;i < rowSize;i ++) 35 { 36 for(int j = 0;j < colSize;j ++) 37 { 38 cout << grid[i][j] << " " ; 39 } 40 cout << endl; 41 } 42 */ 43 // cout << acreList[3] << endl; 44 // memset(0,visit,sizeof(visit)); 45 int flag = 0; 46 for(int i = 0;i < grid.size();i ++) 47 { 48 for(int j = 0;j < grid[0].size();j ++) 49 { 50 if(grid[i][j] == 0) 51 { 52 Dfs(i,j,grid); 53 flag = 1; 54 } 55 } 56 } 57 if(!flag) 58 Maxresult = rowSize*colSize; 59 return Maxresult; 60 } 61 bool limit(int a,int b) 62 { 63 return (a>=0 && a<rowSize && b>=0 && b<colSize); 64 } 65 void preDfs(int curX,int curY,vector<vector<int>>& grid,int &acre) 66 { 67 acre ++; 68 grid[curX][curY] = acreListEnd; 69 for(int i = 0;i < 4;i ++) 70 { 71 if(limit(curX+x[i],curY+y[i]) && grid[curX+x[i]][curY+y[i]] == 1) 72 { 73 preDfs(curX+x[i],curY+y[i],grid,acre); 74 } 75 } 76 } 77 void Dfs(int curX,int curY,vector<vector<int>> grid) 78 { 79 int result[4] = {0}; 80 for(int i = 0;i < 4;i ++) 81 { 82 int flag = 1; 83 if(limit(curX+x[i],curY+y[i])) 84 { 85 for(int j = 0;j < i;j ++) 86 { 87 if(result[j]==grid[curX+x[i]][curY+y[i]]) 88 { 89 flag = 0; 90 break; 91 } 92 } 93 if(flag) 94 result[i] = grid[curX+x[i]][curY+y[i]]; 95 else 96 result[i] = 0; 97 } 98 else 99 result[i] = 0; 100 } 101 for(int i = 0;i < 4;i ++) 102 result[i] = acreList[result[i]]; 103 sort(result,result+4); 104 if(result[0]+result[1]+result[3]+result[2]+1>Maxresult) 105 { 106 Maxresult = result[0]+result[1]+result[3]+result[2]+1; 107 } 108 cout << result[2] << " " << result[3] << endl; 109 } 110 };
Leetcode-827 Making A Large Island(最大人工岛)
标签:public oid visit set tco style div nbsp mit
原文地址:https://www.cnblogs.com/Asurudo/p/9771714.html