import java.io.*;
import java.util.*;
public class Main{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static int[][] map;
static int n,x1,y1,x2,y2,result;
static boolean[][] judge;
static int[] X = new int[]{-1,1,0,0};
static int[] Y = new int[]{0,0,-1,1};
public static void main(String[] args) throws IOException {
n = Integer.parseInt(br.readLine());
map = new int[n][n];
judge = new boolean[n][n];
result = Integer.MAX_VALUE;
for(int i = 0 ; i < n ; i++){
String[] STR = br.readLine().split("");
for(int j = 0 ; j < STR.length ; j++){
map[i][j] = Integer.parseInt(STR[j]);
if(Integer.parseInt(STR[j]) == 1){
judge[i][j] = true;
}
}
}
String[] STR = br.readLine().split(" ");
x1 = Integer.parseInt(STR[0]) - 1;
y1 = Integer.parseInt(STR[1]) - 1;
x2 = Integer.parseInt(STR[2]) - 1;
y2 = Integer.parseInt(STR[3]) - 1;
BFS();
out.write(result + "");
out.flush();
out.close();
br.close();
}
public static void BFS(){
Point first = new Point();
first.x = x1;
first.y = y1;
first.step = 0;
Deque<Point> deque = new LinkedList<>();
deque.offer(first);
judge[x1][y1] = true;
while(!deque.isEmpty()){
Point root = deque.poll();
for(int i = 0 ; i < 4 ; i++){
int a = root.x + X[i];
int b = root.y + Y[i];
int c = root.step + 1;
if(a >= 0 && a < n && b >= 0 && b < n && !judge[a][b]){
if(a == x2 && b == y2){
result = Math.min(c , result);
}
else{
Point node = new Point();
node.x = a;
node.y = b;
node.step = c;
deque.offer(node);
judge[a][b] = true;
}
}
}
}
}
}
class Point{
int x;
int y;
int step;
}
洛谷 P1746 离开中山路 Java
最新推荐文章于 2024-05-31 10:46:10 发布