int n, m;//长宽 int cnt;//计数器 int g[N][N];//地图 int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};//方向
voiddfs(int sx, int sy) { g[sx][sy] = 0;//直接变水
for (int i = 0; i < 4; i++)//四个方向 { int a = sx + dx[i], b = sy + dy[i]; if (a < 0 || a >= n || b < 0 || b >= m) continue; if (g[a][b] == 0) continue; g[a][b] = 0;//变水 cnt++;//计数孤岛 dfs(a, b); } }
intmain() { cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> g[i][j];
for (int i = 0; i < n; i++) { if (g[i][0] == 1) dfs(i, 0); //左边 if (g[i][m - 1] == 1) dfs(i, m - 1); // 右边 } for (int i = 0; i < m; i++) { if (g[0][i] == 1) dfs(0, i); // 上边 if (g[n - 1][i] == 1) dfs(n - 1, i); // 下边 } int res = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (g[i][j] == 1) { cnt = 1;//记录当前 dfs(i, j); res += cnt; } } } cout << res << endl; return0; }
#include<iostream> #include<vector> usingnamespace std; int n, m; int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};//方向 voiddfs(vector<vector<int>>& grid, vector<vector<bool>>& visited, int x, int y){ if (visited[x][y]) return;//走过,不用再走
visited[x][y] = true;
for (int i = 0; i < 4; i++) { int nextx = x + dir[i][0]; int nexty = y + dir[i][1]; if (nextx < 0 || nextx >= n || nexty < 0 || nexty >= m) continue; if (grid[x][y] > grid[nextx][nexty]) continue; // 注意:这里是从低向高遍历,逆流而上
dfs (grid, visited, nextx, nexty); } return; }
intmain(){
cin >> n >> m; vector<vector<int>> grid(n, vector<int>(m, 0));
for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> grid[i][j]; } } // 标记从第一组边界上的节点出发,可以遍历的节点 vector<vector<bool>> firstBorder(n, vector<bool>(m, false));
// 从最上和最下行的节点出发,向高处遍历 for (int i = 0; i < n; i++) { dfs (grid, firstBorder, i, 0); // 遍历最左列,接触第一组边界 dfs (grid, secondBorder, i, m - 1); // 遍历最右列,接触第二组边界 }
// 从最左和最右列的节点出发,向高处遍历 for (int j = 0; j < m; j++) { dfs (grid, firstBorder, 0, j); // 遍历最上行,接触第一组边界 dfs (grid, secondBorder, n - 1, j); // 遍历最下行,接触第二组边界 } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // 如果这个节点,从第一组边界和第二组边界出发都遍历过,就是结果 if (firstBorder[i][j] && secondBorder[i][j]) cout << i << " " << j << endl;; } } }
#include<bits/stdc++.h> #define rep(i, a, b) for(int i = a;i < b;i++) usingnamespace std;
int N,M; int cnt;//单个岛屿计数器
int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};//方向 voiddfs(vector<vector<int>> & grid, int x, int y, int mark){ grid[x][y] = mark;//用mark覆盖,相当于着色 cnt++; rep(m, 0, 4) { int i = x + dir[m][0]; int j = y + dir[m][1]; if(i < 0 || i >= N || j < 0 || j >= M || grid[i][j] != 1) continue; dfs(grid, i, j, mark); } }
intmain(){ cin >> N >> M; vector<vector<int>> grid(N, vector<int>(M, 0)); rep(i, 0, N) { rep(j ,0, M) { cin >> grid[i][j]; } } //先遍历地图记录岛屿-面积 unordered_map<int, int> map; int mark = 2; rep(i, 0, N) { rep(j, 0, M) { if(grid[i][j] == 1) { cnt = 0; dfs(grid, i, j, mark); map[mark] = cnt; mark++; } } } //遍历海洋,计算相邻岛屿面积之和,取最大值 int ans = 0, temp = 0; rep(i, 0, N) { rep(j, 0, M) { if(grid[i][j] > 0) continue; //将相邻的不同岛屿面积相加,unordered_set去重岛屿 unordered_set<int> set; temp = 1; rep(m, 0, 4) { int x = i + dir[m][0]; int y = j + dir[m][1]; if(x < 0 || x >= N || y < 0 || y >= M || grid[x][y] == 0) continue; if(set.find(grid[x][y]) != set.end()) continue; else { temp += map[grid[x][y]]; set.insert(grid[x][y]); } } ans = ans > temp ? ans : temp; } } if(ans == 0) cout<< N * M;//没水全是地,直接返回地图大小 else cout << ans; return0; }