package fastrobot;
import java.util.Scanner;
public class Solution {
static int a[][]=new int[201][201];
static int col,row,X,Y,Xd,Yd;
static String s;
static int dx[]={0,0,1,-1};
static int dy[]={1,-1,0,0};
static int[] xque=new int[10000000];
static int[] yque=new int[10000000];
static int dir[][]=new int [201][201];
static int change[][]=new int[201][201];
static int left,right;
private static Scanner sc;
static void push(int x,int y)
{
xque[right]=x;
yque[right++]=y;
}
static void bfs(int xstart, int ystart)
{
left=0; right=0;
push(xstart,ystart);
int x, y, nx, ny;
while(left!=right)
{
//pop
x=xque[left];
y=yque[left++];
for(int i=0;i<4;i++)
{
nx=x+dx[i];
ny=y+dy[i];
if(nx<=0 || ny<=0 || nx>col || ny>row) continue;
while(a[nx][ny]!=-1){
if(a[x][y]+1<a[nx][ny])
{
a[nx][ny]=a[x][y]+1;
}
nx=x+dx[i];
ny=y+dy[i];
push(nx,ny);
}
}
}
}
public static void main(String[] args) {
sc = new Scanner(System.in);
int T=sc.nextInt();
for(int t=1;t<=T;t++)
{
col=sc.nextInt();
row=sc.nextInt();
Y=sc.nextInt();
X=sc.nextInt();
Yd=sc.nextInt();
Xd=sc.nextInt();
for(int i=0;i<row;i++){
s=sc.next();
char[] c=s.toCharArray();
for(int j=0;j<col;j++)
{
if(c[j]=='1') a[i+1][j+1]=-1;
else a[i+1][j+1]=1000;
}
}
//==============================================
//a[Y][X]=1;
dir=new int [201][201];
change=new int[201][201];
change[X][Y]=-1;
bfs(X,Y);
if(change[Xd][Yd]==-1) System.out.println(-1);
else System.out.println(change[Xd][Yd]);
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.