Given a map, the map is divided into n × m (n, m <= 100) squares, any block is not a flat mountain. The ground can be, mountains can not. Now you are in the map (x1, y1) piece of ground, and asked: Do you at least need to turn a few bends to reach the destination (x2, y2)? You can only travel along the horizontal and vertical directions of the flat, equal to the number of turn changes the direction of travel times (from horizontal to vertical or from vertical to horizontal). For example: As shown in FIG, 5 is the minimum number of bends.
Input Format
Line 1: nm
row n + 1 to 2: Description entire topographic map (0: space; 1: Alpine),
As (FIG) line is described as 2 Terrain: 1000010
Terrain line 3 is described as: 0010100
N + 2 th row: x1 y1 x2 y2 (starting, respectively, the end point coordinates)
Output Format
s (i.e., the minimum number of bends)
Sample input
5 7
1 0 0 0 0 1 0
0 0 1 0 1 0 0
0 0 0 0 1 0 1
0 1 1 0 0 0 0
0 0 0 0 1 1 0
1 3 1 7
Sample input
5
Import of java.util.ArrayList; Import java.util.Scanner; public class Test { // Wang Yaohui static Scanner SC = new new Scanner (the System.in); // n-m long if a b a width point map information is recorded l go through recording the coordinates of a point obtained have to find the minimum value min in a direction fang array static int n-= sc.nextInt (); static int m = sc.nextInt (); static int [] [] = a new new int [n-] [m]; static Boolean [] [] B = new new Boolean [n-] [m]; static the ArrayList <Integer> = Lnew new the ArrayList <Integer> (); static int [] = {Fang - 1,0,1,0,0, -1,0,1 }; static int min = Integer.MAX_VALUE; public static void main (String [] args) { // stored map information for ( int I = 0; I <n-; I ++ ) { for ( int J = 0; J <m; J ++ ) { A [I] [J] = sc.nextInt () ; } } // X1, Y1 starting coordinates x2, y2 end point coordinate int X1 = sc.nextInt () -. 1 ; int Y1 = sc.nextInt () -. 1; int x2=sc.nextInt()-1; int y2=sc.nextInt()-1; l.add(x1); l.add(y1); b[x1][y1]=true; f(x1,y1,x2,y2); System.out.println(min); } private static void f(int x1, int y1, int x2, int y2) { // TODO Auto-generated method stub if (x1==x2&&y1==y2) { //存我现在走得方向 int []fang1=new int[2]; // record times I turned int JI = 0 ; // cycle set my route for ( int I = 2; I <l.size () - 2; = I + 2 ) { // Analyzing this step I go direction is the same and take the next step of the iF (l.get (I) + fang1 [0] == l.get (I + 2) && l.get (I +. 1) + fang1 [. 1] == l.get ( . 3 + I )) { Continue ; } the else { // change direction fang1 [0] = l.get (I + 2) - l.get (I); fang1 [ . 1] = l.get (I +. 3) - l.get (I +. 1 ); JI ++ ; } } //Minimum is found IF (JI < min) { min = JI; } } the else { for ( int I = 0; I <fang.length; + I = 2 ) { // Next go to coordinate int X = X1 + Fang [ I]; int Y = Y1 + Fang [I +. 1 ]; // determines next whether bounds and next whether through the next go whether plain iF ! (X> = 0 && X <n-&& Y> = 0 && Y <m && B [X] [Y] && A [X] [Y] == 0 ) { X1 + = [I] Fang; Y1 + = Fang [I +. 1 ]; B [X1] [Y1] =true; l.add(x1); l.add(y1); f(x1,y1,x2,y2); b[x1][y1]=false; x1-=fang[i]; y1-=fang[i+1]; l.remove(l.size()-1); l.remove(l.size()-1); } } } } }