文章目录
- [卡码网 101. 孤岛的总面积](#卡码网 101. 孤岛的总面积)
- [卡码网 102. 沉没孤岛](#卡码网 102. 沉没孤岛)
- [卡码网 103. 高山流水](#卡码网 103. 高山流水)
- [卡码网 104. 建造最大岛屿](#卡码网 104. 建造最大岛屿)
卡码网 101. 孤岛的总面积
可以从行和列的遍历开始,从边缘向中间遍历 。
java
import java.util.*;
class Main{
static int res = 0;
static int count = 0;
static boolean flag = false;
public static void dfs(int[][] graph, boolean[][] visited, int x, int y) {
if (visited[x][y] || graph[x][y] == 0) {
return;
}
if (x == 0 || y == 0 || x == graph.length - 1 || y == graph[0].length - 1) {
flag =true;
}
count++;
visited[x][y] =true;
int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
for (int i = 0; i < 4; i++) {
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if (nextx < 0 || nexty < 0 || nextx >= graph.length || nexty >= graph[0].length) {
continue;
}
dfs(graph, visited, nextx, nexty);
}
}
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] graph = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
graph[i][j] = sc.nextInt();
}
}
boolean[][] visited = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && graph[i][j] == 1) {
dfs(graph, visited, i, j);
}
if (flag == false) {
res += count;
}
count = 0;
flag = false;
}
}
System.out.println(res);
}
}
卡码网 102. 沉没孤岛
java
import java.util.*;
public class Main{
public static void dfs(int[][] graph, int x, int y) {
if (graph[x][y] == 0 || graph[x][y] == 2) {
return;
}
graph[x][y] = 2;
int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
for (int i = 0; i < 4; i++) {
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if (nextx < 0 || nexty < 0 || nextx >= graph.length || nexty >= graph[0].length) {
continue;
}
dfs(graph, nextx, nexty);
}
}
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] graph = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
graph[i][j] = sc.nextInt();
}
}
for (int i = 0; i < n; i++) {
if (graph[i][0] == 1) {
dfs(graph, i, 0);
}
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);
}
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] == 2) {
graph[i][j] = 1;
}
else if (graph[i][j] == 1) {
graph[i][j] = 0;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(graph[i][j] + " ");
}
System.out.println();
}
}
}
卡码网 103. 高山流水
从边界开始遍历,逆向思维。
java
import java.util.*;
public class Main{
public static void dfs(int[][] graph,boolean[][] visited, int x, int y) {
if (visited[x][y]) {
return;
}
visited[x][y] = true;
int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
for (int i = 0; i < 4; i++) {
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if (nextx < 0 || nexty < 0 || nextx >= graph.length || nexty >= graph[0].length) {
continue;
}
if (graph[x][y] <= graph[nextx][nexty]) {
dfs(graph, visited, nextx, nexty);
}
}
}
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] graph = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
graph[i][j] = sc.nextInt();
}
}
boolean[][] firstborder = new boolean[n][m];
boolean[][] secondborder = new boolean[n][m];
for (int i = 0; i < n; i++) {
dfs(graph, firstborder, i, 0);
dfs(graph, secondborder, i, m - 1);
}
for (int j = 0; j < m; j++) {
dfs(graph,firstborder, 0, j);
dfs(graph, secondborder, n - 1, j);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (firstborder[i][j] == true && secondborder[i][j] == true) {
System.out.print(i + " " + j);
System.out.println();
}
}
}
}
}
卡码网 104. 建造最大岛屿
先计算出所有岛屿的面积,用map存储,之后再把0的位置设置为1看周围面积变化。
java
import java.util.*;
public class Main{
static int mark = 2;
static int count = 0;
static int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
public static void dfs(int[][] graph,boolean[][] visited, int x, int y, int mark) {
if (visited[x][y]) {
return;
}
visited[x][y] = true;
graph[x][y] = mark;
count++;
for (int i = 0; i < 4; i++) {
int nextx = x + dir[i][0];
int nexty = y + dir[i][1];
if (nextx < 0 || nexty < 0 || nextx >= graph.length || nexty >= graph[0].length) {
continue;
}
if (graph[nextx][nexty] == 1) {
dfs(graph, visited, nextx, nexty, mark);
}
}
}
public static void main(String[] agrs) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] graph = new int[n][m];
HashMap<Integer, Integer> mapsize = new HashMap<>();
HashSet<Integer> setsize = new HashSet<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
graph[i][j] = sc.nextInt();
}
}
int maxIsland = 0;
boolean[][] visited = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && graph[i][j] == 1) {
count = 0;
dfs(graph, visited, i, j, mark);
mapsize.put(mark, count);
mark++;
}
maxIsland = Math.max(maxIsland, count);
}
}
int res = maxIsland;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int size = 1;
if (graph[i][j] == 0) {
setsize.clear();
for (int k = 0; k < 4; k++) {
int neari = i + dir[k][0];
int nearj = j + dir[k][1];
if (neari < 0 || nearj < 0 || neari >= graph.length || nearj >= graph[0].length) {
continue;
}
Integer cur_key = graph[neari][nearj];
if (mapsize.containsKey(cur_key) && !setsize.contains(cur_key)) {
size += mapsize.get(cur_key);
setsize.add(cur_key);
}
}
res = Math.max(res, size);
}
}
}
System.out.println(res);
}
}