目录
一、走迷宫
题目链接:走迷宫
这题我愿称之为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; } }