Created
April 26, 2016 08:09
-
-
Save PreSoichiSumi/b3185c4b4defff986d79042b1b15c84a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayDeque; | |
import java.util.Arrays; | |
import java.util.Deque; | |
import java.util.Scanner; | |
public class Main { | |
public static int[][] maze =new int [100][]; | |
public static int[][] dist =new int [100][]; | |
public static int[][] dir ={{-1,0},{1,0},{0,-1},{0,1}}; | |
public static void main(String[] args) { | |
for(int i=0;i<100;i++){ | |
maze[i]=new int[100]; | |
} | |
for(int i=0;i<100;i++){ | |
dist[i]=new int[100]; | |
Arrays.fill(dist[i], -1); | |
} | |
Deque<int[]> queue=new ArrayDeque<>(); | |
Scanner sc=new Scanner(System.in); | |
int R=sc.nextInt(); | |
int C=sc.nextInt(); | |
int sy=sc.nextInt(); | |
int sx=sc.nextInt(); | |
int gy=sc.nextInt(); | |
int gx=sc.nextInt(); | |
for(int i=1;i<=R;i++){ | |
String tmp=sc.next(); | |
for(int j=1;j<=C;j++){ | |
maze[i][j]=tmp.codePointAt(j-1); | |
} | |
} | |
int[] S={sy,sx}; | |
queue.add(S); | |
maze[sy][sx]=1;//visited | |
dist[sy][sx]=0;//start | |
int[] now; | |
int res=0; | |
while(!queue.isEmpty()){ | |
now=queue.poll(); | |
// System.out.println(now[0]+","+now[1]+","+dist[now[0]][now[1]]); | |
if((now[0]==gy) && (now[1]==gx)){ | |
res=dist[now[0]][now[1]]; | |
break; | |
} | |
maze[now[0]][now[1]]=1; | |
for(int i=0;i<4;i++){ | |
int newY=now[0]+dir[i][0]; | |
int newX=now[1]+dir[i][1]; | |
if(maze[newY][newX]=='.'){ //まだなら,そこに訪れる | |
queue.add(new int[]{newY,newX}); | |
maze[newY][newX]=1; | |
dist[newY][newX]=dist[now[0]][now[1]]+1; | |
} | |
} | |
} | |
System.out.println(res); | |
sc.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment