周末还是懈怠啊,不如有个随想录催促我,今天没咋干活,先把昨天的内容补上,
417. 太平洋大西洋水流问题
溯流而上是需要考虑的内容,其次就是只要他可以,就给他true,我要我四周,都比我高的继续找。比我高他就是true。
cpp
class Solution {
public:
int neighbor[4][2] = {1,0,0,-1,-1,0,0,1};
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
int n = heights.size();
int m = heights[0].size();
vector<vector<int>>result;
vector<vector<bool>>Pacific_visited(n,vector<bool>(m,false));
vector<vector<bool>>Atlantic_visited(n,vector<bool>(m,false));
//横向
for(int i = 0;i < n;i++){
dfs(heights,Pacific_visited,i,0);
dfs(heights,Atlantic_visited,i,m-1);
}
//纵向
for(int j = 0;j < m;j++){
dfs(heights,Pacific_visited,0,j);
dfs(heights,Atlantic_visited,n-1,j);
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(Pacific_visited[i][j] == true && Atlantic_visited[i][j] == true){
result.push_back({i,j});
}
}
}
return result;
}
void dfs(vector<vector<int>> &heights,vector<vector<bool>> &visited,int x,int y){
if(visited[x][y] == true)return;
visited[x][y] = true;
for(int i = 0;i < 4;i++){
int nextx = x + neighbor[i][0];
int nexty = y + neighbor[i][1];
if(nextx < 0 || nexty < 0 || nextx >= heights.size() || nexty >= heights[0].size())continue;
if(heights[x][y] > heights[nextx][nexty])continue;
dfs(heights,visited,nextx,nexty);
}
return;
}
};
827.最大人工岛
卧槽卧槽,这题真有成就感啊,好难啊。
随想录帮忙解决了两件事:
1、isAllGrid的用法
2、这个岛屿是否标记过,visitedGrid
cpp
class Solution
{
public:
int neighbor[4][2] = {1, 0, 0, -1, -1, 0, 0, 1};
int count = 0;
int index = 2;
unordered_map<int, int> map;
int largestIsland(vector<vector<int>> &grid)
{
int result = 0;
int n = grid.size();
int m = grid[0].size();
vector<vector<int>> visited(n, vector<int>(m, 0));
bool isAllGrid = true;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (grid[i][j] == 0)
isAllGrid = false;
if (grid[i][j] == 1 && visited[i][j] == 0)
{
count = 1;
visited[i][j] = index;
bfs(grid, visited, i, j);
map[index] = count;
// cout << index << "是" << map[index] << endl;
index++;
}
}
}
if (isAllGrid)
return n * m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (grid[i][j] == 0)
{
result = max(result, findMax(grid, visited, i, j));
// cout << "grid[" << i << "][" << j << "] 结果是 " << result << endl;
}
}
}
// for (int i = 0; i < n; i++)
// {
// for (int j = 0; j < m; j++)
// {
// cout << "visited[" << i << "][" << j << "]是" << visited[i][j] << endl;
// }
// }
// for (int i = 2; i <= index; i++)
// {
// cout << "map[" << i << "]是" << map[i] << endl;
// }
return result;
}
void bfs(vector<vector<int>> &grid, vector<vector<int>> &visited, int x, int y)
{
visited[x][y] = index;
for (int i = 0; i < 4; i++)
{
int nextx = x + neighbor[i][0];
int nexty = y + neighbor[i][1];
if (nextx < 0 || nexty < 0 || nextx >= grid.size() || nexty >= grid[0].size())
continue;
if (grid[nextx][nexty] == 1 && visited[nextx][nexty] == 0)
{
count++;
bfs(grid, visited, nextx, nexty);
}
}
}
int findMax(vector<vector<int>> &grid, vector<vector<int>> &visited, int x, int y)
{
int sumMax = 1;
unordered_set<int> visitedGrid;
for (int i = 0; i < 4; i++)
{
int nextx = x + neighbor[i][0];
int nexty = y + neighbor[i][1];
if (nextx < 0 || nexty < 0 || nextx >= grid.size() || nexty >= grid[0].size())
continue;
if (visitedGrid.count(visited[nextx][nexty]))
continue;
if (grid[nextx][nexty] == 1)
{
sumMax += map[visited[nextx][nexty]];
visitedGrid.insert(visited[nextx][nexty]);
}
}
return sumMax;
}
};
我的写法应该是有一些冗余,不过基本上可以覆盖思路。
1.首先先做遍历岛屿,标记所有岛屿的大小。
2.然后再遍历海洋,看哪块海洋连接在一起会让岛屿最大。
3.考虑加岛屿的时候要确认别重复加了。
4.考虑全岛屿情况。
5.在搞函数的时候,发生了for循环走不出来,visit没标记已经查过的岛屿的问题(这个查了一段时间才搞出来)
牛逼!睡觉!