BFS算法经典例题(1)(Java实现)

目录

一、走迷宫

二、长草

三、 迷宫


一、走迷宫

题目链接:走迷宫​​​​​​

这题我愿称之为bfs最最最经典的例题!

import java.sql.Struct;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
  static int n,m;
  static int[][] v=new int[110][110];
  static class Node{
    int x,y;
    int ans;//表示步数
    Node(int x,int y,int ans){
      this.x=x;
      this.y=y;
      this.ans=ans;
    }
  }
  static int[][] mp=new int[110][110];//地图
  static int sa,sb,ta,tb;
  static Queue<Node> queue=new LinkedList<>();
 static int f=0;
 static int[] xx={1,-1,0,0};
 static int[] yy={0,0,1,-1};
  public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    n=scanner.nextInt();
    m=scanner.nextInt();
    for(int i=1;i<=n;i++){
      for(int j=1;j<=m;j++){
        mp[i][j]=scanner.nextInt();
      }
    }
    sa=scanner.nextInt();
    sb=scanner.nextInt();
    ta=scanner.nextInt();
    tb=scanner.nextInt();
    bfs();
    if(f==0){
      System.out.println(-1);
    }
  }
  public static void bfs(){
    v[sa][sb]=1;
    queue.add(new Node(sa,sb,0));
    while (!queue.isEmpty()){
      Node p=queue.poll();
      if(p.x==ta&&p.y==tb){
        System.out.println(p.ans);
        f=1;
        return;
      }
      for(int i=0;i<4;i++){
        int dx=xx[i]+p.x;
        int dy=yy[i]+p.y;
        if(check(dx,dy)){
          v[dx][dy]=1;
          queue.add(new Node(dx,dy,p.ans+1));
        }
      }
    }
  }
  //判断该点是否可以走
  public static boolean check(int x,int y){
    if(x>=1&&x<=n&&y>=1&&y<=m&&v[x][y]==0&&mp[x][y]==1){
      return true;
    }
    return false;
  }
}

二、长草

题目链接:长草

import java.sql.Struct;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
 static int n,m,k;
 static char[][] mp=new char[1100][1100];
 static int[][] v=new int[1100][1100];//表示该点如今是第几层
 static int[] xx={1,-1,0,0};
 static int[] yy={0,0,1,-1};

  public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    n=scanner.nextInt();
    m=scanner.nextInt();
    for(int i=0;i<n;i++){
      String s=scanner.next();
      mp[i]=s.toCharArray();
    }
    k=scanner.nextInt();
    //将各点如今的层数先初始化
    for(int i=0;i<n;i++){
      for(int j=0;j<m;j++){
        if(mp[i][j]=='g'){
          v[i][j]=1;
        }
      }
    }
    for(int p=1;p<=k;p++){
       for(int i=-0;i<n;i++){
         for(int j=0;j<m;j++){
           if(mp[i][j]=='g'&&v[i][j]==p){
              bfs(i,j,p);
           }
         }
      }
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<m;j++){
        System.out.print(mp[i][j]);
      }
      System.out.println();
    }
  }
  public static void bfs(int x,int y,int p){
    for(int i=0;i<4;i++){
      int dx=xx[i]+x;
      int dy=yy[i]+y;
      if(check(dx,dy)&&mp[dx][dy]=='.'){
        mp[dx][dy]='g';
        v[dx][dy]=p+1;
      }
    }
  }
  public static boolean check(int x,int y){
    if(x>=0&&x<n&&y>=0&&y<m){
      return true;
    }
    return false;
  }
}

三、 迷宫

题目链接:迷宫

import java.sql.Struct;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
  //30 51的长度
  static String[] s={"01010101001011001001010110010110100100001000101010",
          "00001000100000101010010000100000001001100110100101",
          "01111011010010001000001101001011100011000000010000",
          "01000000001010100011010000101000001010101011001011",
          "00011111000000101000010010100010100000101100000000",
          "11001000110101000010101100011010011010101011110111",
          "00011011010101001001001010000001000101001110000000",
          "10100000101000100110101010111110011000010000111010",
          "00111000001010100001100010000001000101001100001001",
          "11000110100001110010001001010101010101010001101000",
          "00010000100100000101001010101110100010101010000101",
          "11100100101001001000010000010101010100100100010100",
          "00000010000000101011001111010001100000101010100011",
          "10101010011100001000011000010110011110110100001000",
          "10101010100001101010100101000010100000111011101001",
          "10000000101100010000101100101101001011100000000100",
          "10101001000000010100100001000100000100011110101001",
          "00101001010101101001010100011010101101110000110101",
          "11001010000100001100000010100101000001000111000010",
          "00001000110000110101101000000100101001001000011101",
          "10100101000101000000001110110010110101101010100001",
          "00101000010000110101010000100010001001000100010101",
          "10100001000110010001000010101001010101011111010010",
          "00000100101000000110010100101001000001000000000010",
          "11010000001001110111001001000011101001011011101000",
          "00000110100010001000100000001000011101000000110011",
          "10101000101000100010001111100010101001010000001000",
          "10000010100101001010110000000100101010001011101000",
          "00111100001000010000000110111000000001000000001011",
          "10000001100111010111010001000110111010101101111000"};

  static char[][] mp=new char[100][100];
  static int n=30,m=50;
  static int[][] v=new int[100][100];//判断该点是否访问
  static class Node{
    int x,y,ans;
    char pos;
    Node(int x,int y,int ans){
      this.x=x;
      this.y=y;
      this.ans=ans;
    }
    Node(){

    }
  }
  static int[] xx={1,0,0,-1};
  static int[] yy={0,-1,1,0};
  static   Queue<Node> queue=new LinkedList<>();
  static Node father[][] =new Node[1100][1100];//父亲节点
  public static void main(String[] args) {
    for(int i=0;i<n;i++){
      for(int j=0;j<m;j++){
        mp[i+1][j+1]=s[i].toCharArray()[j];
      }
    }
   bfs();
   dfs(n,m);
  }
  public static void dfs(int x,int y){
    if(x==1&&y==1){
      return;
    }
    else{
      dfs(father[x][y].x,father[x][y].y);
    }
    System.out.print(father[x][y].pos);
  }
  public static void bfs(){
    queue.add(new Node(1,1,0));
    v[1][1]=1;
    while(!queue.isEmpty()){
      Node now=queue.poll();
      if(now.x==n&&now.y==m){
        return;
      }
      for(int i=0;i<4;i++){
        int dx=xx[i]+now.x;
        int dy=now.y+yy[i];
        if(in(dx,dy)&&v[dx][dy]==0&&mp[dx][dy]=='0'){
          v[dx][dy]=1;
          queue.add(new Node(dx,dy,now.ans+1));
          father[dx][dy]=new Node();
          father[dx][dy].x=now.x;
          father[dx][dy].y=now.y;
          if(i==0){
            father[dx][dy].pos='D';
          }
          else if(i==1){
            father[dx][dy].pos='L';
          }
          else if(i==2){
            father[dx][dy].pos='R';
          }else {
            father[dx][dy].pos='U';
          }

        }
      }
    }
  }
  public static boolean in(int x,int y){
    if(x>=1&&x<=n&&y>=1&&y<=m){
      return true;
    }
    return false;
  }

}

下面是Java实现BFS算法的示例代码: ``` import java.util.*; public class BFS { // 定义图的邻接矩阵表示 private int[][] graph; // 定义存储节点是否被访问的数组 private boolean[] visited; // 定义存储遍历结果的队列 private Queue<Integer> result; public BFS(int[][] graph) { this.graph = graph; visited = new boolean[graph.length]; result = new LinkedList<>(); } // BFS算法实现 public void bfsTraversal(int startNode) { Queue<Integer> queue = new LinkedList<>(); visited[startNode] = true; queue.offer(startNode); while (!queue.isEmpty()) { int node = queue.poll(); result.offer(node); for (int i = 0; i < graph[node].length; i++) { if (graph[node][i] == 1 && !visited[i]) { visited[i] = true; queue.offer(i); } } } } // 获取遍历结果 public Queue<Integer> getResult() { return result; } public static void main(String[] args) { // 创建示例图的邻接矩阵表示 int[][] graph = { {0, 1, 1, 0, 0}, {1, 0, 0, 1, 1}, {1, 0, 0, 1, 0}, {0, 1, 1, 0, 1}, {0, 1, 0, 1, 0} }; // 创建BFS对象并进行遍历 BFS bfs = new BFS(graph); bfs.bfsTraversal(0); // 输出遍历结果 Queue<Integer> result = bfs.getResult(); while (!result.isEmpty()) { System.out.print(result.poll() + " "); } } } ``` 该代码实现BFS算法,并对一个示例图进行了遍历。其,`graph`数组表示示例图的邻接矩阵,`visited`数组表示节点是否被访问,`result`队列用于存储遍历结果。`bfsTraversal`方法实现BFS算法,`getResult`方法用于获取遍历结果。在`main`方法创建BFS对象并进行遍历,最后输出遍历结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜到极致就是渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值