代码随想录算法训练营第58天 [101.孤岛的总面积 102.沉没孤岛 103.水流问题 104.建造最大岛屿]
一、101.孤岛的总面积
链接: 代码随想录.
思路:从四个边缘开始深搜,搜到了标记为2,这样所有为2和0的就不是孤岛,所有为1的就是孤岛
做题状态:看解析后做出来了
#include<iostream>
#include<vector>
using namespace std;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
void dfs(vector<vector<int>> &graph,int x,int y){
graph[x][y] = 0;
for(int i = 0;i<4;i++){
int next_x = x+dir[i][0];
int next_y = y+dir[i][1];
if(next_x<0 || next_x>=graph.size()||next_y<0||next_y>=graph[0].size()){
continue;
}
if(graph[next_x][next_y] == 1){
dfs(graph,next_x,next_y);
}
}
}
int main(){
int n;
int m;
cin >> n >> m;
vector<vector<int>> graph(n,vector<int>(m,0));
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
cin >> graph[i][j];
}
}
for(int i = 0;i<n;i++){
if(graph[i][0] == 1)
dfs(graph,i,0);
}
for(int i = 0;i<n;i++){
if(graph[i][m-1] == 1)
dfs(graph,i,m-1);
}
for(int j = 0;j<m;j++){
if(graph[0][j] == 1)
dfs(graph,0,j);
}
for(int j = 0;j<m;j++){
if(graph[n-1][j] == 1)
dfs(graph,n-1,j);
}
int result = 0;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
// cout << graph[i][j] << " ";
if(graph[i][j] == 1){
result++;
}
}
// cout << endl;
}
cout << result <<endl;
}
二、102.沉没孤岛
链接: 代码随想录.
思路:从四个边缘开始深搜,搜到了标记为2,这样所有为2和0的就不是孤岛,所有为1的就是孤岛,最后把2变为1,把1变为0
做题状态:看解析后做出来了
#include<iostream>
#include<vector>
using namespace std;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
void dfs(vector<vector<int>> &graph,int x,int y){
graph[x][y] = 2;
for(int i = 0;i<4;i++){
int next_x = x+dir[i][0];
int next_y = y+dir[i][1];
if(next_x<0 || next_x>=graph.size()||next_y<0||next_y>=graph[0].size()){
continue;
}
if(graph[next_x][next_y] == 1){
dfs(graph,next_x,next_y);
}
}
}
int main(){
int n;
int m;
cin >> n >> m;
vector<vector<int>> graph(n,vector<int>(m,0));
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
cin >> graph[i][j];
}
}
for(int i = 0;i<n;i++){
if(graph[i][0] == 1)
dfs(graph,i,0);
}
for(int i = 0;i<n;i++){
if(graph[i][m-1] == 1)
dfs(graph,i,m-1);
}
for(int j = 0;j<m;j++){
if(graph[0][j] == 1)
dfs(graph,0,j);
}
for(int j = 0;j<m;j++){
if(graph[n-1][j] == 1)
dfs(graph,n-1,j);
}
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if(graph[i][j] == 1){
graph[i][j] = 0;
}
if(graph[i][j] == 2){
graph[i][j] = 1;
}
cout << graph[i][j] << " ";
}
cout << endl;
}
}
三、103.水流问题
链接: 代码随想录.
思路: 某个点能不能流到第一组边界和第二组边界 转化为 第一组边界和第二组边界能不能流到某个点
做题状态:看解析后做出来了
#include<iostream>
#include<vector>
using namespace std;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
void dfs(vector<vector<int>> &graph,vector<vector<int>> &visited,int x,int y){
if(visited[x][y]){
return;
}
visited[x][y] = 1;
for(int i = 0;i<4;i++){
int next_x = x+dir[i][0];
int next_y = y+dir[i][1];
if(next_x<0 || next_x>=graph.size()||next_y<0||next_y>=graph[0].size()){
continue;
}
if(graph[x][y] <= graph[next_x][next_y]){
dfs(graph,visited,next_x,next_y);
}
}
}
int main(){
int n;
int m;
cin >> n >> m;
vector<vector<int>> graph(n,vector<int>(m,0));
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
cin >> graph[i][j];
}
}
//某个点能不能流到第一组边界和第二组边界
//转化为
//第一组边界和第二组边界能不能流到某个点
vector<vector<int>> first_visited(n,vector<int>(m,0));
vector<vector<int>> second_visited(n,vector<int>(m,0));
for(int i = 0;i<n;i++){
dfs(graph,first_visited,i,0);
dfs(graph,second_visited,i,m-1);
}
for(int j = 0;j<m;j++){
dfs(graph,first_visited,0,j);
dfs(graph,second_visited,n-1,j);
}
vector<vector<int>> result(n,vector<int>(m,0));
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
// cout << second_visited[i][j] <<" ";
if(first_visited[i][j] == 1 && second_visited[i][j] == 1){
cout << i << " " << j <<endl;
}
}
// cout << endl;
}
}
四、104.建造最大岛屿
链接: 代码随想录.
思路:给不同的岛屿标记不同的编号mark,再遍历所有为海水的区域将其变为陆地,计算联通起来的最大面积
做题状态:看解析后做出来了
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
using namespace std;
int n;
int m;
int count = 0;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
void dfs(vector<vector<int>> &graph,vector<vector<int>> &visited,int x,int y,int mark){
if(visited[x][y] == 1 || graph[x][y] == 0){
return;
}
visited[x][y] = 1;
graph[x][y] = mark;
count++;
for(int i = 0;i<4;i++){
int next_x = x+dir[i][0];
int next_y = y+dir[i][1];
if(next_x<0 || next_x>=n ||next_y< 0|| next_y>=m){
continue;
}
dfs(graph,visited,next_x,next_y,mark);
}
}
int main(){
cin >> n >>m;
vector<vector<int>> graph(n,vector<int>(m,0));
vector<vector<int>> visited(n,vector<int>(m,0));
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
cin >> graph[i][j];
}
}
unordered_map<int,int> gridNum;
int mark = 2;
bool isAllGrid = true;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
if(graph[i][j] == 0){
isAllGrid = false;
}
if(graph[i][j] == 1 && visited[i][j] == 0){
count = 0;
dfs(graph,visited,i,j,mark);
gridNum[mark] = count;
mark++;
}
}
}
if(isAllGrid){
cout <<m*n<<endl;
return 0;
}
int result = 0;
unordered_set<int> visitedGraph;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
count = 1;
visitedGraph.clear();
if(graph[i][j] == 0){
for(int k = 0;k<4;k++){
int next_x = i+dir[k][0];
int next_y = j+dir[k][1];
if(next_x<0 || next_x>=n ||next_y< 0|| next_y>=m){
continue;
}
if(visitedGraph.count(graph[next_x][next_y])){
continue;
}
count += gridNum[graph[next_x][next_y]];
visitedGraph.insert(graph[next_x][next_y]);
}
}
result = max(result,count);
}
}
cout << result <<endl;
}