poj 3984 迷宮問題 (廣度優先BFS)

來源:互聯網
上載者:User

基本的廣度優先搜尋。

import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;public class Main {public static void main(String...args){Main main = new Main();main.input();main.process();}private void input(){for(int i = 0; i < 5; i++)for(int j = 0; j < 5; j++){maze[i][j] = cin.nextInt();}}private void process(){open = new LinkedList<Node>();Node start = new Node(0, 0);start.pre = null;open.add(start);visit[0][0] = true;Node parentNode;Node nextOpen;while(!open.isEmpty()){parentNode = open.peek();open.remove();//遍曆子節點for(int i = 0; i < 4; i++){//上if(i == 0){if(parentNode.x == 0) //沒有上面continue;else if(maze[parentNode.x - 1][parentNode.y] == 1)//上面是牆continue;else{nextOpen = new Node(parentNode.x - 1, parentNode.y);}}//下else if(i == 1){if(parentNode.x == 4) continue;else if(maze[parentNode.x + 1][parentNode.y] == 1)continue;else{nextOpen = new Node(parentNode.x + 1, parentNode.y);}}//左else if(i == 2){if(parentNode.y == 0) continue;else if(maze[parentNode.x][parentNode.y - 1] == 1)continue;else{nextOpen = new Node(parentNode.x, parentNode.y - 1);}}//右else{if(parentNode.y == 4) continue;else if(maze[parentNode.x][parentNode.y + 1] == 1)continue;else{nextOpen = new Node(parentNode.x, parentNode.y + 1);}}nextOpen.pre = parentNode;if(nextOpen.x == 4 && nextOpen.y == 4) {//找到了print(nextOpen);}if(!visit[nextOpen.x][nextOpen.y]){visit[nextOpen.x][nextOpen.y] = true;open.add(nextOpen);}}}}private void print(Node node){if(node.pre != null) print(node.pre);System.out.println("(" + node.x + ", " + node.y + ")");}private Scanner cin = new Scanner(System.in);private int[][] maze = new int[5][5];private Queue<Node> open;private boolean visit[][] = new boolean[5][5];}class Node{public Node(int x, int y){this.x = x;this.y = y;}public Node(){}public int x;public int y;public Node pre;}

更一般化的廣度優先搜尋過程,下面這個把close表也加上了,利用hash表來檢查是否重複搜尋:

package com.xujin;import java.util.HashSet;import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;public class Main {public static void main(String...args){Main main = new Main();main.input();main.process();}private void input(){for(int i = 0; i < 5; i++)for(int j = 0; j < 5; j++){maze[i][j] = cin.nextInt();}}private void process(){Node start = new Node(0, 0);start.pre = null;open.add(start);Node parentNode;Node nextOpen;label:while(!open.isEmpty()){parentNode = open.peek();close.add(parentNode);open.remove();//遍曆子節點for(int i = 0; i < 4; i++){//上if(i == 0){if(parentNode.x == 0) //沒有上面continue;else if(maze[parentNode.x - 1][parentNode.y] == 1)//上面是牆continue;else{nextOpen = new Node(parentNode.x - 1, parentNode.y);}}//下else if(i == 1){if(parentNode.x == 4) continue;else if(maze[parentNode.x + 1][parentNode.y] == 1)continue;else{nextOpen = new Node(parentNode.x + 1, parentNode.y);}}//左else if(i == 2){if(parentNode.y == 0) continue;else if(maze[parentNode.x][parentNode.y - 1] == 1)continue;else{nextOpen = new Node(parentNode.x, parentNode.y - 1);}}//右else{if(parentNode.y == 4) continue;else if(maze[parentNode.x][parentNode.y + 1] == 1)continue;else{nextOpen = new Node(parentNode.x, parentNode.y + 1);}}nextOpen.pre = parentNode;if(nextOpen.x == 4 && nextOpen.y == 4) {//找到了print(nextOpen);break label;}if(!close.contains(nextOpen)){open.add(nextOpen);}}}}private void print(Node node){if(node.pre != null) print(node.pre);System.out.println("(" + node.x + ", " + node.y + ")");}private Scanner cin = new Scanner(System.in);private int[][] maze = new int[5][5];private Queue<Node> open = new LinkedList<Node>();;private HashSet<Node> close = new HashSet<Node>();}class Node{public Node(int x, int y){this.x = x;this.y = y;}public Node(){}public int hashCode(){return x * 10 + y;}public boolean equals(Object node){if(this.hashCode() == node.hashCode()) return true;return false;}public int x;public int y;public Node pre;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.