Merge pull request #2777 from Jasonyou-boy/feature/nextString

feat(图论): 字符串接龙 Java优化版 并修复 beginStr==endStr 边界条件
This commit is contained in:
程序员Carl
2024-10-26 18:12:56 +08:00
committed by GitHub
3 changed files with 148 additions and 138 deletions

View File

@ -277,7 +277,7 @@ ACM格式大家在输出结果的时候要关注看看格式问题特别
有录友可能会想ACM格式就是麻烦有空格没有空格有什么影响结果对了不就行了
ACM模式相对于核心代码模式力扣 更考验大家对代码的掌控能力。 例如工程代码里,输输出都是要自己控制的。这也是为什么大公司笔试都是ACM模式。
ACM模式相对于核心代码模式力扣 更考验大家对代码的掌控能力。 例如工程代码里,输输出都是要自己控制的。这也是为什么大公司笔试都是ACM模式。
以上代码中,结果都存在了 result数组里二维数组每一行是一个结果最后将其打印出来。重点看注释

View File

@ -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<pair> queue = new LinkedList<pair>();
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<Node> 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

View File

@ -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<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);
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<String> 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<String> wordList) {
int len = 1;
Set<String> set = new HashSet<>(wordList);
Set<String> visited = new HashSet<>();
Queue<String> 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<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);
}
}
```