python 迷宫问题_迷宫问题

//深度优先+回溯法 与 广度优先+前驱节点

import java.util.*;

public class Main{

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

while(sc.hasNext()){

int n = sc.nextInt();

int m = sc.nextInt();

int[][] maz = new int[n][m];

for(int i=0; i

for(int j=0; j

maz[i][j] = sc.nextInt();

}

}

int[][] flag = new int[n][m];

//bfs(maz,n,m,flag); 广度优先

ArrayList> list = new ArrayList>();

Stack stack = new Stack();

flag[0][0] =2;

stack.push(new Node(0,0));

dfs(0,0,maz,stack,list,flag); //深度优先

dfsOut(list);

}

}

public static void dfs(int i,int j,int[][] maz,Stack stack,ArrayList> list,int[][] flag){

if(i == maz.length-1 && j == maz[0].length-1){

Stack stack1 = new Stack();

ArrayList alist = new ArrayList();

while(!stack.isEmpty()){

Node node1 = stack.pop();

stack1.push(node1); //复制栈

alist.add(node1);

}

for(int k=alist.size()-1;k>0;k--){ //不要最后一个

stack.push(alist.get(k));

}

list.add(stack1);

flag[i][j] = 0; //最后一个清0

return;

}

if(i+1

stack.push(new Node(i+1,j));

flag[i+1][j] = 2;

dfs(i+1,j,maz,stack,list,flag);

}

if(i-1>=0 && maz[i-1][j]==0 && flag[i-1][j]==0){

stack.push(new Node(i-1,j));

flag[i-1][j] = 2;

dfs(i-1,j,maz,stack,list,flag);

}

if(j+1

stack.push(new Node(i,j+1));

flag[i][j+1] = 2;

dfs(i,j+1,maz,stack,list,flag);

}

if(j-1>=0 && maz[i][j-1]==0 && flag[i][j-1]==0){

stack.push(new Node(i,j-1));

flag[i][j-1] = 2;

dfs(i,j-1,maz,stack,list,flag);

}

Node top = stack.pop();

flag[top.x][top.y] = 0;

}

public static void dfsOut(ArrayList> list){

int minSize = 10000, minIndex=-1;

for(int i=0;i

if(list.get(i).size() < minSi敏感词Index = i;

minSize = list.get(i).size();

}

}

Stack s = list.get(minIndex);

while(!s.isEmpty()){

Node node = s.pop();

System.out.println("(" + node.x + "," + node.y + ")");

}

}

public static void bfs(int[][] maz, int n, int m, int[][] flag){

Node node0 = new Node(0,0);

Queue queue = new LinkedList();

ArrayList list = new ArrayList();

queue.offer(node0);

flag[0][0] =1;

while(!queue.isEmpty()){

Node now = queue.poll();

list.add(now);

if(now.x == n-1 && now.y == m-1){

break;

}else{

if(now.x+1

Node right = new Node(now.x+1, now.y);

right.prex = now.x;

right.prey = now.y;

flag[now.x+1][now.y]=1;

queue.offer(right);

}

if(now.y+1

Node right = new Node(now.x, now.y+1);

right.prex = now.x;

right.prey = now.y;

flag[now.x][now.y+1] =1;

queue.offer(right);

}

if(now.x-1>=0 && maz[now.x-1][now.y]==0 && flag[now.x-1][now.y]==0){//上

Node right = new Node(now.x-1, now.y);

right.prex = now.x;

right.prey = now.y;

flag[now.x-1][now.y]=1;

queue.offer(right);

}

if(now.y-1>=0 && maz[now.x][now.y-1]==0 && flag[now.x][now.y-1]==0){//左

Node right = new Node(now.x, now.y-1);

right.prex = now.x;

right.prey = now.y;

flag[now.x][now.y-1]=1;

queue.offer(right);

}

}

}

Stack s = new Stack();

for(int i=list.size()-1; i>=0; i--){

Node node = list.get(i);

if(node.x == n-1 && node.y == m-1){

s.push(node);

n = node.prex + 1;

m = node.prey + 1;

}

}

while(!s.isEmpty()){

Node node = s.pop();

System.out.println("(" + node.x + "," + node.y + ")");

}

}

static class Node{

int x;

int y;

int prex;

int prey;

public Node(int x, int y){

this.x = x;

this.y = y;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值