1. 程式人生 > 實用技巧 >POJ3984 迷宮問題

POJ3984 迷宮問題

題目連結:https://vjudge.net/problem/POJ-3984
Description
定義一個二維陣列:
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表示可以走的路,只能橫著走或豎著走,不能斜著走,要求程式設計序找出從左上角到右下角的最短路線。

Input
一個5 × 5的二維陣列,表示一個迷宮。資料保證有唯一解。
Output
左上角到右下角的最短路徑,格式如樣例所示。
Sample 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

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

解題思路:題目要求最短路,BFS即可,把一個節點的座標跟它的前一個點的座標封裝成一個類(或者結構體),這樣跑完BFS後,便可以匯出起點到終點的路徑

Cpp Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
int const maxn = 10;
bool vis[maxn][maxn];
int table[maxn][maxn];

struct node{
    int x, y, prex, prey;
}index[maxn][maxn];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
void bfs(){
    queue<node> q;
    node cur, nex;
    index[0][0].prex = index[0][0].prey = -1;
    vis[0][0] = 1;
    q.push(index[0][0]);
    while ((!q.empty()))
    {
        cur = q.front();
        q.pop();
        if(cur.x==4&&cur.y==4){
            return;
        }
        for (int i = 0; i < 4;i++){
            int nx = cur.x + dx[i];
            int ny = cur.y + dy[i];
            if(nx>=0&&nx<5&&ny>=0&&ny<5&&!vis[nx][ny]&&table[nx][ny]==0){
                vis[nx][ny] = 1;
                index[nx][ny].prex = cur.x;
                index[nx][ny].prey = cur.y;
                index[nx][ny].x = nx;
                index[nx][ny].y = ny;
                q.push(index[nx][ny]);
            }
        }
    }
    
}


int main(){
    for (int i = 0; i<5;i++){
        for (int j = 0; j < 5;j++){
            cin >> table[i][j];
        }
    }
    bfs();
    stack<node> s;
    node now, nex;
    now.x = now.y = 4;
    now.prex=index[4][4].prex;
    now.prey=index[4][4].prey;
    s.push(now);
    while (1)
    {
        if(now.prex==-1&&now.prey==-1){
            break;
        }
        nex.x=now.prex;
        nex.y=now.prey;
        nex.prex = index[nex.x][nex.y].prex;
        nex.prey = index[nex.x][nex.y].prey;
        s.push(nex);
        now = nex;
         /* code */
    }
    while (!s.empty())
    {
        node x = s.top();
        s.pop();
        cout << "(" <<x.x << ", " <<x.y << ")" << endl;
        /* code */
    }
    return 0;
}

Java Code

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class Main {
	static int maxn=10;
	static boolean vis[][]=new boolean[maxn][maxn];
	static int table[][]=new int[maxn][maxn];
	static int dx[]= {0, 0, -1, 1};
	static int dy[]= {-1, 1, 0, 0};
	
	static class node{
		int x,y,prex,prey;
		node(){}
		node(int a,int b,int c,int d){
			x=a;
			y=b;
			prex=c;
			prey=d;
		}
		void out() {
			System.out.println(x+" "+y+" "+prex+" "+prey);
		}
	}
	static node index[][]=new node[maxn][maxn];
	
	static void bfs() {
		Queue<node> q=new LinkedList<node>();
		node cur,nex;
		index[0][0]=new node(0,0,-1,-1);
		vis[0][0]=true;
		q.offer(index[0][0]);
		while(!q.isEmpty()) {
			cur=q.poll();
			if(cur.x==4&&cur.y==4) {
				return;
			}
			for(int i=0;i<4;i++) {
				int nx=cur.x+dx[i];
				int ny=cur.y+dy[i];
				if(nx>=0&&nx<5&&ny>=0&&ny<5&&!vis[nx][ny]&&table[nx][ny]==0) {
					vis[nx][ny]=true;
					index[nx][ny]=new node(nx,ny,cur.x,cur.y);
					q.offer(index[nx][ny]);
				}
			}
		}
		
	}
	public static void main(String[] args) {
		for(int i=0;i<maxn;i++) {
			for(int j=0;j<maxn;j++) {
				index[i][j]=new node();
			}
		}
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		for(int i=0;i<5;i++) {
			for(int j=0;j<5;j++) {
				table[i][j]=sc.nextInt();
			}
		}
		bfs();
		Stack<node> s=new Stack<node>();
		node now,nex;
		now=new node(4,4,index[4][4].prex,index[4][4].prey);
		s.push(now);
		while(true) {
			
			if(now.prex==-1&&now.prey==-1) {
				break;
			}
			nex=new node(now.prex,now.prey,index[now.prex][now.prey].prex,index[now.prex][now.prey].prey);
			s.push(nex);
			now=nex;
		}
		while(!s.isEmpty()) {
			node x=s.pop();
			System.out.println("("+x.x+", "+x.y+")");
		}
	}

}