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;
}
}