Question 1335: Implement and recursively implement the BFS in the maze.

Source: Internet
Author: User
Http://ac.jobdu.com/problem.php? Id = 1335

There is another question, similar
Http://ac.jobdu.com/problem.php? Id = 1365
We recommend that you use BFS for ease of implementation.

Description:

Sun's school holds a computer festival every year. This year's Computer Festival has a new interesting competition project called Chuang maze.
Sun's roommate designed the maze for the Computer Festival, So Sun asked Sun to help calculate the minimum number of steps out of the maze.
By knowing the minimum number of steps, you can help control the difficulty of the game and remove some maps without reaching the end point.
The competition rule is: From the origin point () to the end point (n-1, n-1), you can only go in the left and right directions, only in the given matrix.

Input:

Multiple groups of data are input.
Input N (0 <n <= 100) for each group of data, and then input the 01 matrix of N * n. 0 indicates that there is no obstacle in the grid, and 1 indicates that there is obstacle.
Note: If the origin and endpoint in the input are 1, the maze is inaccessible.

Output:

Output the shortest steps of the maze for each group of inputs. If the input cannot reach, the output is-1.

Sample input:
20 10 050 0 0 0 01 0 1 0 10 0 0 0 00 1 1 1 01 0 1 0 0
Sample output:
28
 
  
Depressing: I think I will identify the subject of ACM. There is always one or two cases. I don't know what boundary data is missing.
The test case is correct.
Once ACM arrives, it will be suspended. Only use the first case.
Which of the following experts can give you some advice ?? Grateful!
 
  
After preliminary analysis: such simple maze problems are unsuitable for recursion. Java stack overflow and big data errors. No problem found yet. Based on the print results, recursion only refers to a majority of recursion maps, not all.
If BFS is used, all nodes in the figure are traversed. At first, I thought it would be faster to use recursion. Later I found that it would be better to use BFs.
The following code is for reference only and is incorrect.
Import java. Io. bufferedinputstream;
Import java. util. arrays;
Import java. util. collections;


Public class Chuang maze {
Static int arr [] [];
Static long OPT [] [];

Static int N;
Static final long max = 99999999;
Static int Ori [] [] = {}, {0,-1 }};
Public static void main (string [] ARGs ){
S = new second (New bufferedinputstream (system. In ));
While (S. hasnextint ()){
N = S. nextint ();
Arr = new int [N] [N];
Opt = new long [N] [N];
For (long [] A: OPT)
Arrays. Fill (A, max );
For (INT I = 0; I <n; I ++ ){
For (Int J = 0; j <n; j ++)
Arr [I] [J] = S. nextint ();
}
If (ARR [0] [0] = 1 | arr [n-1] [n-1] = 1)
System. Out. println (-1 );
Else {
Long T = f (n-1, n-1 );
If (T <max)
System. Out. println (t );
Else
System. Out. println (-1 );
}
// For (long [] ar: OPT ){
// For (long I: Ar)
// System. Out. printf ("% 6D", I );
// System. Out. println ();
//}

}
}
Static Boolean isok (int x, int y ){
Return x> = 0 & x <n & Y> = 0 & Y <n;
}
// Idea of Dynamic Planning
Static long F (int I, Int J ){
Arr [I] [J] = 1; // an important step. Prevents infinite recursion. I don't know if it is appropriate to find a solution. Please advise me.
If (OPT [I] [J] <max)
Return OPT [I] [J];
If (I = 0 & J = 0)
Return 0;
For (int K = 0; k <4; k ++ ){
Int x = I + Ori [k] [0];
Int y = J + Ori [k] [1];
Long temp = max;
If (isok (x, y) & (ARR [x] [Y] = 0 | OPT [x] [Y] <max )) {// OPT [x] [Y] <max (points that have been computed and can be reached) is also important. Because the preceding arr [I] [J] = 1 will overwrite the computed vertex.
Temp = f (x, y );
If (OPT [I] [J]> temp + 1)
OPT [I] [J] = temp + 1;
}
}
Return OPT [I] [J];
}
}

/*
Test:
6
0 0 0 0 0 1
1 0 1 0 1 1
0 0 0 0 0 1
1 1 1 1 0 1
1 0 0 0 0 1
0 0 0 0 0 0
 
  

10
99999999 1 99999999 99999999 99999999 99999999
99999999 2 99999999 99999999 99999999 99999999
99999999 3 4 5 6 99999999
99999999 99999999 99999999 99999999 99999999
99999999 11 10 9 8 99999999
99999999 12 11 10 9 10


*/


 
  
The correct code should be shown below with BFS:
import java.io.BufferedInputStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;


public class Main {
static int n;
static int map[][];
static int visit[][];
static Queue<B> q;
static B start, end;
static int ori[][] = {{0,1},{0,-1},{1,0},{-1,0}};
public static void main(String[] args) {
Scanner s = new Scanner(new BufferedInputStream(System.in));
while(s.hasNextInt()){
n = s.nextInt();
map = new int[n][n];
visit = new int[n][n];
q = new LinkedList<B>();
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
map[i][j] = s.nextInt();
if(map[0][0] == 1 || map[n-1][n-1]==1){
System.out.println(-1);
continue;
}
System.out.println(bfs());
}
}
static int bfs(){
B pre = new B(0,0,0);
B next = null;
q.add(pre);
while(!q.isEmpty()){
pre = q.poll();
visit[pre.x][pre.y] = 1;
for(int i=0; i<4; i++){
int x = pre.x + ori[i][0];
int y = pre.y + ori[i][1];
if(x == n-1 && y ==n-1)
return pre.time +1;
if(x>=0 && x<n && y>=0 && y<n && map[x][y]==0 && visit[x][y]==0){
next = new B(x,y,pre.time+1);
q.add(next);
}
}
}
return -1;
}
}

class B{
int x,y,time;
public B(int x, int y, int time) {
super();
this.x = x;
this.y = y;
this.time = time;
}
}
 
  
 
 

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.