diff --git a/problems/kamacoder/0098.所有可达路径.md b/problems/kamacoder/0098.所有可达路径.md index db35524f..a2f60a90 100644 --- a/problems/kamacoder/0098.所有可达路径.md +++ b/problems/kamacoder/0098.所有可达路径.md @@ -277,7 +277,7 @@ ACM格式大家在输出结果的时候,要关注看看格式问题,特别 有录友可能会想,ACM格式就是麻烦,有空格没有空格有什么影响,结果对了不就行了? -ACM模式相对于核心代码模式(力扣) 更考验大家对代码的掌控能力。 例如工程代码里,输出输出都是要自己控制的。这也是为什么大公司笔试,都是ACM模式。 +ACM模式相对于核心代码模式(力扣) 更考验大家对代码的掌控能力。 例如工程代码里,输入输出都是要自己控制的。这也是为什么大公司笔试,都是ACM模式。 以上代码中,结果都存在了 result数组里(二维数组,每一行是一个结果),最后将其打印出来。(重点看注释) diff --git a/problems/kamacoder/0100.岛屿的最大面积.md b/problems/kamacoder/0100.岛屿的最大面积.md index 06319ee3..d86f6cd8 100644 --- a/problems/kamacoder/0100.岛屿的最大面积.md +++ b/problems/kamacoder/0100.岛屿的最大面积.md @@ -222,121 +222,127 @@ public: ## 其他语言版本 -### Java -DFS +### Java + ```java -//这里的实现为主函数处理每个岛屿的第一块陆地 方式 -//所以是主函数直接置count为1,剩余的交给dfs来做。 import java.util.*; +import java.math.*; + +/** + * DFS版 + */ public class Main{ - static int[][] dir = {{0,-1}, {1,0}, {0,1}, {-1, 0}};//四个方向 - static int count = 0; - public static void dfs(boolean[][] visited, int x, int y, int[][] grid){ - for(int i = 0; i < 4; i++){ - int nextX = x + dir[i][0]; - int nextY = y + dir[i][1]; - if(nextX < 0 || nextY < 0 || nextY >= grid[0].length || nextX >= grid.length){ - continue; - } - if(!visited[nextX][nextY] && grid[nextX][nextY] == 1){ - count++; - visited[nextX][nextY] = true; - dfs(visited, nextX, nextY, grid); - } - } - } + + static final int[][] dir={{0,1},{1,0},{0,-1},{-1,0}}; + static int result=0; + static int count=0; + public static void main(String[] args){ - Scanner in = new Scanner(System.in); - int n = in.nextInt(); - int m = in.nextInt(); - int[][] grid = new int[n][m]; - for(int i = 0; i < n; i++){ - for(int j = 0; j < m; j++){ - grid[i][j] = in.nextInt(); + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int[][] map = new int[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + map[i][j]=scanner.nextInt(); } } - - int result = 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] && grid[i][j] == 1){ - visited[i][j] = true; - count = 1; - dfs(visited, i, j, grid); - //dfs遍历完了一座岛屿,就比较count和result,保留最大的 - result = Math.max(result, count); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if(!visited[i][j]&&map[i][j]==1){ + count=0; + dfs(map,visited,i,j); + result= Math.max(count, result); } } } System.out.println(result); } -} -``` -BFS -```java -import java.util.*; -public class Main{ - static int[][] dir = {{0,-1}, {1,0}, {0,1}, {-1, 0}};//下右上左的顺序 - static int count = 0; - public static void bfs(boolean[][] visited, int x, int y, int[][] grid){ - Queue queue = new LinkedList(); - queue.add(new pair(x,y)); - count = 1; //该岛屿的第一块陆地被visit了 - - //对这个岛屿的所有都入队,除非上下左右都没有未访问的陆地 - while(!queue.isEmpty()){ - int curX = queue.peek().x; - int curY = queue.poll().y; - //对每块陆地都进行上下左右的入队和计算(遍历),自然就是按广度优先了 - for(int i = 0; i < 4; i++){ - int nextX = curX + dir[i][0]; - int nextY = curY + dir[i][1]; - if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length){ - continue; - } - if(!visited[nextX][nextY] && grid[nextX][nextY] == 1){ - count++; - queue.add(new pair(nextX, nextY)); - visited[nextX][nextY] = true; + + static void dfs(int[][] map,boolean[][] visited,int x,int y){ + count++; + 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||nextY<0 + ||nextX>=map.length||nextY>=map[0].length + ||visited[nextX][nextY]||map[nextX][nextY]==0)continue; + + dfs(map,visited,nextX,nextY); } } - } - } - - static class pair{ +} +``` + +```java +import java.util.*; +import java.math.*; + +/** + * BFS版 + */ +public class Main { + static class Node { int x; int y; - pair(int x, int y){ + + public Node(int x, int y) { this.x = x; this.y = y; } } - - public static void main(String[] args){ - Scanner in = new Scanner(System.in); - int n = in.nextInt(); - int m = in.nextInt(); - int[][] grid = new int[n][m]; - for(int i = 0; i < n; i++){ - for(int j = 0; j < m; j++){ - grid[i][j] = in.nextInt(); + + static final int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; + static int result = 0; + static int count = 0; + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + int[][] map = new int[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + map[i][j] = scanner.nextInt(); } } - int result = 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] && grid[i][j] == 1){ - visited[i][j] = true; - bfs(visited, i, j, grid); - result = Math.max(result, count); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (!visited[i][j] && map[i][j] == 1) { + count = 0; + bfs(map, visited, i, j); + result = Math.max(count, result); + } } } System.out.println(result); } + + static void bfs(int[][] map, boolean[][] visited, int x, int y) { + Queue q = new LinkedList<>(); + q.add(new Node(x, y)); + visited[x][y] = true; + count++; + while (!q.isEmpty()) { + Node node = q.remove(); + for (int i = 0; i < 4; i++) { + int nextX = node.x + dir[i][0]; + int nextY = node.y + dir[i][1]; + if (nextX < 0 || nextY < 0 || nextX >= map.length || nextY >= map[0].length || visited[nextX][nextY] || map[nextX][nextY] == 0) + continue; + q.add(new Node(nextX, nextY)); + visited[nextX][nextY] = true; + count++; + } + } + } } + ``` ### Python diff --git a/problems/kamacoder/0110.字符串接龙.md b/problems/kamacoder/0110.字符串接龙.md index 308e50b7..ef261c4d 100644 --- a/problems/kamacoder/0110.字符串接龙.md +++ b/problems/kamacoder/0110.字符串接龙.md @@ -152,66 +152,70 @@ int main() { ## 其他语言版本 -### Java +### Java + ```Java +import java.util.*; + public class Main { - // BFS方法 - public static int ladderLength(String beginWord, String endWord, List wordList) { - // 使用set作为查询容器,效率更高 - HashSet set = new HashSet<>(wordList); - - // 声明一个queue存储每次变更一个字符得到的且存在于容器中的新字符串 - Queue queue = new LinkedList<>(); - - // 声明一个hashMap存储遍历到的字符串以及所走过的路径path - HashMap visitMap = new HashMap<>(); - queue.offer(beginWord); - visitMap.put(beginWord, 1); - - while (!queue.isEmpty()) { - String curWord = queue.poll(); - int path = visitMap.get(curWord); + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + scanner.nextLine(); + String beginStr = scanner.next(); + String endStr = scanner.next(); + scanner.nextLine(); + List wordList = new ArrayList<>(); + wordList.add(beginStr); + wordList.add(endStr); + for (int i = 0; i < n; i++) { + wordList.add(scanner.nextLine()); + } + int count = bfs(beginStr, endStr, wordList); + System.out.println(count); + } - 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); + /** + * 广度优先搜索-寻找最短路径 + */ + public static int bfs(String beginStr, String endStr, List wordList) { + int len = 1; + Set set = new HashSet<>(wordList); + Set visited = new HashSet<>(); + Queue q = new LinkedList<>(); + visited.add(beginStr); + q.add(beginStr); + q.add(null); + while (!q.isEmpty()) { + String node = q.remove(); + //上一层结束,若下一层还有节点进入下一层 + if (node == null) { + if (!q.isEmpty()) { + len++; + q.add(null); + } + continue; + } + char[] charArray = node.toCharArray(); + //寻找邻接节点 + for (int i = 0; i < charArray.length; i++) { + //记录旧值,用于回滚修改 + char old = charArray[i]; + for (char j = 'a'; j <= 'z'; j++) { + charArray[i] = j; + String newWord = new String(charArray); + if (set.contains(newWord) && !visited.contains(newWord)) { + q.add(newWord); + visited.add(newWord); + //找到结尾 + if (newWord.equals(endStr)) return len + 1; } } + charArray[i] = old; } } - 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 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); - } } ```