Leetcode-827 Making A Large Island(最大人工島)

來源:互聯網
上載者:User

標籤: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(最大人工島)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.