mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #2629 from learnerInTheFirstStage/master
更新 0104.建造最大岛屿 DFS解法Java版
This commit is contained in:
@ -281,6 +281,78 @@ for (int j = 0; j < m; j++) {
|
||||
## 其他语言版本
|
||||
|
||||
### Java
|
||||
```Java
|
||||
public class Main {
|
||||
|
||||
// 采用 DFS 进行搜索
|
||||
public static void dfs(int[][] heights, int x, int y, boolean[][] visited, int preH) {
|
||||
// 遇到边界或者访问过的点,直接返回
|
||||
if (x < 0 || x >= heights.length || y < 0 || y >= heights[0].length || visited[x][y]) return;
|
||||
// 不满足水流入条件的直接返回
|
||||
if (heights[x][y] < preH) return;
|
||||
// 满足条件,设置为true,表示可以从边界到达此位置
|
||||
visited[x][y] = true;
|
||||
|
||||
// 向下一层继续搜索
|
||||
dfs(heights, x + 1, y, visited, heights[x][y]);
|
||||
dfs(heights, x - 1, y, visited, heights[x][y]);
|
||||
dfs(heights, x, y + 1, visited, heights[x][y]);
|
||||
dfs(heights, x, y - 1, visited, heights[x][y]);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int m = sc.nextInt();
|
||||
int n = sc.nextInt();
|
||||
|
||||
int[][] heights = new int[m][n];
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
heights[i][j] = sc.nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化两个二位boolean数组,代表两个边界
|
||||
boolean[][] pacific = new boolean[m][n];
|
||||
boolean[][] atlantic = new boolean[m][n];
|
||||
|
||||
// 从左右边界出发进行DFS
|
||||
for (int i = 0; i < m; i++) {
|
||||
dfs(heights, i, 0, pacific, Integer.MIN_VALUE);
|
||||
dfs(heights, i, n - 1, atlantic, Integer.MIN_VALUE);
|
||||
}
|
||||
|
||||
// 从上下边界出发进行DFS
|
||||
for (int j = 0; j < n; j++) {
|
||||
dfs(heights, 0, j, pacific, Integer.MIN_VALUE);
|
||||
dfs(heights, m - 1, j, atlantic, Integer.MIN_VALUE);
|
||||
}
|
||||
|
||||
// 当两个边界二维数组在某个位置都为true时,符合题目要求
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (pacific[i][j] && atlantic[i][j]) {
|
||||
res.add(Arrays.asList(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 打印结果
|
||||
for (List<Integer> list : res) {
|
||||
for (int k = 0; k < list.size(); k++) {
|
||||
if (k == 0) {
|
||||
System.out.print(list.get(k) + " ");
|
||||
} else {
|
||||
System.out.print(list.get(k));
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Python
|
||||
|
||||
|
@ -258,6 +258,111 @@ int main() {
|
||||
## 其他语言版本
|
||||
|
||||
### Java
|
||||
```Java
|
||||
public class Main {
|
||||
// 该方法采用 DFS
|
||||
// 定义全局变量
|
||||
// 记录每次每个岛屿的面积
|
||||
static int count;
|
||||
// 对每个岛屿进行标记
|
||||
static int mark;
|
||||
// 定义二维数组表示四个方位
|
||||
static int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
|
||||
|
||||
// DFS 进行搜索,将每个岛屿标记为不同的数字
|
||||
public static void dfs(int[][] grid, int x, int y, boolean[][] visited) {
|
||||
// 当遇到边界,直接return
|
||||
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) return;
|
||||
// 遇到已经访问过的或者遇到海水,直接返回
|
||||
if (visited[x][y] || grid[x][y] == 0) return;
|
||||
|
||||
visited[x][y] = true;
|
||||
count++;
|
||||
grid[x][y] = mark;
|
||||
|
||||
// 继续向下层搜索
|
||||
dfs(grid, x, y + 1, visited);
|
||||
dfs(grid, x, y - 1, visited);
|
||||
dfs(grid, x + 1, y, visited);
|
||||
dfs(grid, x - 1, y, visited);
|
||||
}
|
||||
|
||||
public static void main (String[] args) {
|
||||
// 接收输入
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int m = sc.nextInt();
|
||||
int n = sc.nextInt();
|
||||
|
||||
int[][] grid = new int[m][n];
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
grid[i][j] = sc.nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化mark变量,从2开始(区别于0水,1岛屿)
|
||||
mark = 2;
|
||||
|
||||
// 定义二位boolean数组记录该位置是否被访问
|
||||
boolean[][] visited = new boolean[m][n];
|
||||
|
||||
// 定义一个HashMap,记录某片岛屿的标记号和面积
|
||||
HashMap<Integer, Integer> getSize = new HashMap<>();
|
||||
|
||||
// 定义一个HashSet,用来判断某一位置水四周是否存在不同标记编号的岛屿
|
||||
HashSet<Integer> set = new HashSet<>();
|
||||
|
||||
// 定义一个boolean变量,看看DFS之后,是否全是岛屿
|
||||
boolean isAllIsland = true;
|
||||
|
||||
// 遍历二维数组进行DFS搜索,标记每片岛屿的编号,记录对应的面积
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (grid[i][j] == 0) isAllIsland = false;
|
||||
if (grid[i][j] == 1) {
|
||||
count = 0;
|
||||
dfs(grid, i, j, visited);
|
||||
getSize.put(mark, count);
|
||||
mark++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int result = 0;
|
||||
if (isAllIsland) result = m * n;
|
||||
|
||||
// 对标记完的grid继续遍历,判断每个水位置四周是否有岛屿,并记录下四周不同相邻岛屿面积之和
|
||||
// 每次计算完一个水位置周围可能存在的岛屿面积之和,更新下result变量
|
||||
for (int i = 0; i < m; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (grid[i][j] == 0) {
|
||||
set.clear();
|
||||
// 当前水位置变更为岛屿,所以初始化为1
|
||||
int curSize = 1;
|
||||
|
||||
for (int[] dir : dirs) {
|
||||
int curRow = i + dir[0];
|
||||
int curCol = j + dir[1];
|
||||
|
||||
if (curRow < 0 || curRow >= m || curCol < 0 || curCol >= n) continue;
|
||||
int curMark = grid[curRow][curCol];
|
||||
// 如果当前相邻的岛屿已经遍历过或者HashMap中不存在这个编号,继续搜索
|
||||
if (set.contains(curMark) || !getSize.containsKey(curMark)) continue;
|
||||
set.add(curMark);
|
||||
curSize += getSize.get(curMark);
|
||||
}
|
||||
|
||||
result = Math.max(result, curSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 打印结果
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Python
|
||||
|
||||
|
@ -153,6 +153,68 @@ int main() {
|
||||
## 其他语言版本
|
||||
|
||||
### Java
|
||||
```Java
|
||||
public class Main {
|
||||
// BFS方法
|
||||
public static int ladderLength(String beginWord, String endWord, List<String> wordList) {
|
||||
// 使用set作为查询容器,效率更高
|
||||
HashSet<String> set = new HashSet<>(wordList);
|
||||
|
||||
// 声明一个queue存储每次变更一个字符得到的且存在于容器中的新字符串
|
||||
Queue<String> queue = new LinkedList<>();
|
||||
|
||||
// 声明一个hashMap存储遍历到的字符串以及所走过的路径path
|
||||
HashMap<String, Integer> visitMap = new HashMap<>();
|
||||
queue.offer(beginWord);
|
||||
visitMap.put(beginWord, 1);
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
String curWord = queue.poll();
|
||||
int path = visitMap.get(curWord);
|
||||
|
||||
for (int i = 0; i < curWord.length(); i++) {
|
||||
char[] ch = curWord.toCharArray();
|
||||
// 每个位置尝试26个字母
|
||||
for (char k = 'a'; k <= 'z'; k++) {
|
||||
ch[i] = k;
|
||||
|
||||
String newWord = new String(ch);
|
||||
if (newWord.equals(endWord)) return path + 1;
|
||||
|
||||
// 如果这个新字符串存在于容器且之前未被访问到
|
||||
if (set.contains(newWord) && !visitMap.containsKey(newWord)) {
|
||||
visitMap.put(newWord, path + 1);
|
||||
queue.offer(newWord);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void main (String[] args) {
|
||||
/* code */
|
||||
// 接收输入
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int N = sc.nextInt();
|
||||
sc.nextLine();
|
||||
String[] strs = sc.nextLine().split(" ");
|
||||
|
||||
List<String> wordList = new ArrayList<>();
|
||||
for (int i = 0; i < N; i++) {
|
||||
wordList.add(sc.nextLine());
|
||||
}
|
||||
|
||||
// wordList.add(strs[1]);
|
||||
|
||||
// 打印结果
|
||||
int result = ladderLength(strs[0], strs[1], wordList);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Python
|
||||
|
||||
|
Reference in New Issue
Block a user