Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
3.4 kB
0
Indexable
Never
import java.util.Scanner;
class queue {
	static int[] Data = new int[40000];
	static int front, rear;

	public queue() {
		this.front = this.rear = -1;
	}

	void reset() {
		front = rear = -1;
	}

	public void enQueue(int value) {
		Data[++rear] = value;
	}

	int deQueue() {
		return Data[++front];
	}
	int get(){
		int x = front + 1;
		return Data[x];
	}
	boolean isEmpty() {
		if (this.front == this.rear) {
			return true;
		}
		return false;
	}
}

public class Fastrobot {
	static int inX, inY, outX, outY;
	static final int MAX = 10;
	static int[][] Map = new int[MAX][MAX];
	static int[][] Map2 = new int[MAX][MAX];
	static boolean[][] visited = new boolean[MAX][MAX];
	static int n, m, cnt_exit;
	static char[][] arr = new char[20][20];
	static int[] spinR = { 0, 0, 1, -1 };
	static int[] spinC = { 1, -1, 0, 0 };
	static int cr, cc, nr, nc, r, c, min,x,y;
	static boolean ans = false;
	static String row;
	
	
	public static void main(String agr[]){
		Scanner sc = new Scanner(System.in);
		queue rqueue = new queue();
		queue cqueue = new queue();
		int t = sc.nextInt();
		for(int test_case = 1; test_case <= t; test_case++){
			rqueue.reset();
			cqueue.reset();
			n = sc.nextInt();
			m = sc.nextInt();
			inY = sc.nextInt();
			inX = sc.nextInt();
			outY = sc.nextInt();
			outX = sc.nextInt();
			sc.nextLine();
			for (int i = 0; i < n; i++){
				for(int j = 0; j < m; j++){
					visited[i][j] = false;
				}
			}
			for(int i = 0; i< n; i++){
				row = sc.nextLine();
				for(int j = 0; j< row.length(); j++){
					Map[i][j] = Integer.valueOf(String.valueOf(row.charAt(j)));
					Map2[i][j] = Integer.valueOf(String.valueOf(row.charAt(j)));
				}
			}
			x = inX - 1;
			y = inY - 1;
			Map2[x][y] = -1;
			while(x != outX - 1 || y != outY - 1){
				if(x - 1 > 0 && Map[x - 1][y] == 0){
					for(int i = x - 1; i > 0; i--){ // tren
						if(Map[i][y] == 0 && visited[i][y] == false){
							rqueue.enQueue(i);
							cqueue.enQueue(y);
							Map2[i][y] = Map2[x][y] + 1;
							visited[i][y] = true;
						}else{
							break;
						}
						
					}
				}
				if(x + 1 <= n && Map[x + 1][y] == 0){ // duoi
					for(int i = x + 1; i < n; i++){
						if(Map[i][y] == 0 && visited[i][y] == false){
							rqueue.enQueue(i);
							cqueue.enQueue(y);
							Map2[i][y] = Map2[x][y] + 1;
							visited[i][y] = true;
						}else{
							break;
						}
						
					}
				}
				if(y + 1 <= m && Map[x][y + 1] == 0){ // phai
					for(int i = y + 1; i < m; i++){
						if(Map[x][i] == 0 && visited[x][i] == false){
							rqueue.enQueue(x);
							cqueue.enQueue(i);
							Map2[x][i] = Map2[x][y] + 1;
							visited[x][i] = true;
						}else{
							break;
						}
					}
				}
				if(y - 1 > 0 && Map[x][y - 1] == 0){ // trai
					for(int i = y; i > 0; i--){
						if(Map[i][y] == 0 && visited[i][y] == false){
							rqueue.enQueue(x);
							cqueue.enQueue(i);
							Map2[x][i] = Map2[x][y] + 1;
							visited[i][y] = true;
						}else{
							break;
						}
					}
				}
				x = rqueue.deQueue();
				y = cqueue.deQueue();
			}
			System.out.println(Map2[outX-1][outY - 1]);
		}
	}
}



2
7 7
1 2 7 5
1111111
0000011
1011001
1011100
1011110
1000000
1111111
7 7
1 2 7 6
1111111
0000001
1011000
1011100
1011110
1000000
1111111