Poj 3984 Maze problem (breadth-first BFS)

Source: Internet
Author: User

Basic breadth-first search.

Import Java. util. using list; import Java. util. queue; import Java. util. extends; public class main {public static void main (string... ARGs) {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 vertex list <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 (); // traverse the subnode for (INT I = 0; I <4; I ++) {// upper if (I = 0) {If (parentnode. X = 0) // No continue above; else if (maze [parentnode. x-1] [parentnode. y] = 1) // The above is the wall 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 (PAR Entnode. X + 1, parentnode. y) ;}}// left 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) ;}/// right 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 & Amp; nextopen. Y = 4) {// print (nextopen);} If (! Visit [nextopen. x] [nextopen. y]) {visit [nextopen. x] [nextopen. y] = true; open. add (nextopen) ;}}} private void print (node) {If (node. pre! = NULL) print (node. PRE); system. out. println ("(" + node. X + "," + node. Y + ")");} private partition CIN = new partition (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 ;}

The General extended search process adds the close table and checks whether repeated search is performed using the hash table:

Package COM. xujin; import Java. util. hashset; import Java. util. using list; import Java. util. queue; import Java. util. extends; public class main {public static void main (string... ARGs) {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; o Pen. Add (start); node parentnode; node nextopen; label: While (! Open. isempty () {parentnode = open. peek (); close. add (parentnode); open. remove (); // traverse the subnode for (INT I = 0; I <4; I ++) {// upper if (I = 0) {If (parentnode. X = 0) // No continue above; else if (maze [parentnode. x-1] [parentnode. y] = 1) // The above is the wall 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 {n Extopen = new node (parentnode. X + 1, parentnode. y) ;}}// left 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) ;}/// right 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 = parentn Ode; If (nextopen. x = 4 & nextopen. Y = 4) {// print (nextopen); break label;} If (! Close. Contains (nextopen) {open. Add (nextopen) ;}}} private void print (node) {If (node. Pre! = NULL) print (node. PRE); system. out. println ("(" + node. X + "," + node. Y + ")");} private partition CIN = new partition (system. in); Private int [] [] maze = new int [5] [5]; private queue <node> open = new queue list <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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.