计蒜客迷宫求解,求出迷宫最短步数,点击此处链接
import java. util. Scanner;
public class Main {
public static int n, m, p, q, r, s;
public static int min= 50000000 ;
public static int [ ] [ ] dis= { { 0 , 1 } , { 1 , 0 } , { 0 , - 1 } , { - 1 , 0 } } ;
public static char [ ] [ ] map= new char [ 100 ] [ 100 ] ;
public static int [ ] [ ] vis= new int [ 100 ] [ 100 ] ;
public static boolean flag= false ;
public static String [ ] Map= new String [ 100 ] ;
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++ )
{
Map[ i] = scanner. next ( ) ;
}
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< m; j++ )
{
if ( Map[ i] . charAt ( j) == '@' )
{
p= i;
q= j;
}
}
}
dfs ( p, q, 0 ) ;
if ( flag== true )
System. out. println ( min) ;
else
{
System. out. println ( "-1" ) ;
}
}
public static void dfs ( int x, int y, int step)
{
int xn, yn, i;
if ( x== 0 || x== n- 1 || y== 0 || y== m- 1 )
{
flag= true ;
min= Math. min ( min, step) ;
return ;
}
for ( i= 0 ; i< 4 ; i++ )
{
xn= dis[ i] [ 0 ] + x;
yn= dis[ i] [ 1 ] + y;
if ( xn>= 0 && xn< n&& yn>= 0 && yn< m&& vis[ xn] [ yn] == 0 && Map[ xn] . charAt ( yn) != '#' )
{
vis[ xn] [ yn] = 1 ;
dfs ( xn, yn, step+ 1 ) ;
vis[ xn] [ yn] = 0 ;
}
}
}
}
[ 计蒜客迷宫求解,是否可以出迷宫,点击此处链接] ( https: / / nanti. jisuanke. com/ t/ T1595)
import java. util. Scanner;
public class Main {
public static int n, m, p, q, r, s;
public static int min= 50000000 ;
public static int [ ] [ ] dis= { { 0 , 1 } , { 1 , 0 } , { 0 , - 1 } , { - 1 , 0 } } ;
public static char [ ] [ ] map= new char [ 100 ] [ 100 ] ;
public static int [ ] [ ] vis= new int [ 100 ] [ 100 ] ;
public static boolean flag= false ;
public static String [ ] Map= new String [ 100 ] ;
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++ )
{
Map[ i] = scanner. next ( ) ;
}
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< m; j++ )
{
if ( Map[ i] . charAt ( j) == 'S' )
{
p= i;
q= j;
break ;
}
}
}
dfs ( p, q) ;
if ( flag== true )
System. out. println ( "yes" ) ;
else
{
System. out. println ( "no" ) ;
}
}
public static void dfs ( int x, int y)
{
int xn, yn, i;
if ( Map[ x] . charAt ( y) == 'T' )
{
flag= true ;
return ;
}
for ( i= 0 ; i< 4 ; i++ )
{
xn= dis[ i] [ 0 ] + x;
yn= dis[ i] [ 1 ] + y;
if ( xn>= 0 && xn< n&& yn>= 0 && yn< m&& vis[ xn] [ yn] == 0 && Map[ xn] . charAt ( yn) != '*' )
{
vis[ xn] [ yn] = 1 ;
dfs ( xn, yn) ;
vis[ xn] [ yn] = 0 ;
}
}
}
}
计蒜客求解迷宫的最短步数BFS,点击链接
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BFS {
public static Queue<Node> Q=new LinkedList<Node>();
public static String [] map=new String[100];
public static int [] []vis=new int [100][100];
public static int n,m,min=999999,p,q,flag=0;
public static int [][]dis=new int[][] {{0,1},{1,0},{0,-1},{-1,0}};
@SuppressWarnings("resource")
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++)
map[i]=scanner.next();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if (map[i].charAt(j)=='S')
{
p=i;
q=j;
break;
}
}
}
Node node=new Node();
node.x=p;
node.y=q;
node.step=0;
Q.offer(node);
while(!Q.isEmpty())
{
Node s=Q.peek();
if(map[s.x].charAt(s.y)=='T')
{
flag=1;
min=Math.min(min, s.step);
}
else
{
for(int i=0;i<4;i++)
{
Node temp=new Node();
temp.x=s.x+dis[i][0];
temp.y=s.y+dis[i][1];
if (temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m&&vis[temp.x][temp.y]==0&&
map[temp.x].charAt(temp.y)!='*')
{
vis[temp.x][temp.y]=1;
temp.step=s.step+1;
Q.offer(temp);
}
}
}
Q.poll();
}
if (flag==1)
System.out.println(min);
else
System.out.println("-1");
}
}
class Node{
int x;
int y;
int step;
}