POJ-3984 迷宫问题 广度优先搜索

问题链接:http://poj.org/problem?id=3984

思路参考:http://blog.csdn.net/raphealguo/article/details/7523411 


定义一个二维数组: 
int maze[5][5] = {

	0, 1, 0, 0, 0,

	0, 1, 0, 1, 0,

	0, 0, 0, 0, 0,

	0, 1, 1, 1, 0,

	0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
左上角到右下角的最短路径,格式如样例所示。

Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	
	class Point{
		public int x;
		public int y;
		public int before_x;  //记录前一个点x
		public int before_y;  //记录前一个点y
		public Point(int x, int y) {
			this.x = x;
			this.y = y;
		}
		
	}
	
	static int input[][] = new int[5][5];
	static int visitflag[][] = new int[5][5];
    public static void main(String[] args) {
    	Main m = new Main();
    	Scanner in = new Scanner(System.in);
    	for(int i=0;i<5;i++)
    		for(int j=0;j<5;j++){
    			int x = in.nextInt();
    			input[i][j] = x;
    		}
    	Main.Point start = m.new Point(0, 0);
    	Main.Point end = m.new Point(4,4);
    	List<Point> result = BFS(start,end);
    	List<Point> path = new ArrayList<Point>(); 
    	if(result != null){
    		if(result.get(result.size()-1).x==4&&result.get(result.size()-1).y==4){
    			path.add(m.new Point(4,4));
    			int before_path_x = result.get(result.size()-1).before_x;  //找到第一个前置x坐标值
    			int before_path_y = result.get(result.size()-1).before_y;
    			while(before_path_x!=0||before_path_y!=0){
    				for(int i=result.size()-1;i>0;i--){
    					if(result.get(i).x==before_path_x&&result.get(i).y==before_path_y){
    						before_path_x = result.get(i).before_x;
    						before_path_y = result.get(i).before_y;
    						path.add(result.get(i));
    					}
    						
    				}
    			}
    			path.add(m.new Point(0,0));
    			for(int i=path.size()-1;i>=0;i--)
    				System.out.println("("+path.get(i).x+","+path.get(i).y+")");
    		}
    		else
    			System.out.println("not to end!");
    	}
    	else
    		System.out.println("no way!");
    	
    }
    
    private static List<Point> BFS(Point start, Point end) {
    	Main m = new Main();
    	int destination[][] = new int[][]{{-1,0},{1,0},{0,-1},{0,1}};
    	List<Point> result = new ArrayList<Point>();
    	List<Point> mid = new ArrayList<Point>();
    	result.add(start);
    	mid.add(start); 
    	visitflag[start.x][start.y]=1;  //起始点已经被访问过了
    	
    	
    	while(mid.size()!=0){
    		Point p = mid.get(0);             //获取头元素
    		mid.remove(0);					//移除掉头元素		
    		for(int i=0;i<4;i++){
    			int new_x = p.x + destination[i][0];
    			int new_y = p.y + destination[i][1];
    			Main.Point new_point = m.new Point(new_x,new_y);
    			if(isOK(new_point)&&visitflag[new_x][new_y]==0){
    				new_point.before_x = p.x;
    				new_point.before_y = p.y;
    				mid.add(new_point);
    				result.add(new_point);
    				if(isEnd(new_point,end)){
    					return result;
    				}
    			}
    		}
    		visitflag[p.x][p.y]=1;  //起始点已经被访问过了
    	}
		return null;
	}

	private static boolean isEnd(Point new_point, Point end) {
		if(new_point.x==end.x&&new_point.y==end.y)
			return true;
		else
			return false;
	}

	private static boolean isOK(Point new_point) {
		if(new_point.x>=0&&new_point.x<=4&&new_point.y>=0&&new_point.y<=4)
			if(input[new_point.x][new_point.y]==0)
				return true;
			else 
				return false;
		else 
			return false;
	}
	
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值