Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.6 kB
27
Indexable
Never
Fast robot


Mr. Noh is responsible for enhancing the movement of a robot faster. 
Now, Mr. Noh is thinking like this: The speed of the robot decreases when it changes its direction. 
Therefore, it requires a study of the acceleration in direction changes. 
However, the better method than that is to use the route with the minimum number of direction changes when it moves from point A to point B.

Because of that, he studies a maze. 
When the maze information is given, he tries to create a program to move it from the starting point to the arriving point based on the minimized direction changes.

Let’s find out the minimum direction changes possible from the starting point to the arriving point when the maze information is given.

Time limit : 1 sec (Java : 2 sec)

[Input]
There can be more than one test case in the input file. The first line has T, the number of test cases. 
Then the totally T test cases are provided in the following lines (T ≤ 10 )
In each test case, The width and height of the maze are given as N & M separately at the first row. (1 ≤ N, M ≤ 200)
The horizontal coordinate and vertical coordinate of the starting point, and the horizontal coordinate and vertical coordinate of the arriving point are given separately in order at the second row. 
Information of the maze is given from the third row the number (N). At this time, the path indicates 0; the wall indicates 1. There is no blank each other.

[Output]
In case of being reachable from the starting point to the arriving point, generate the minimum direction change frequency between two points. 
If not, generate "-1"

[I/O Example]
Input
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

Output
3
2



Code:
#include<iostream>
#define max 1000000
using namespace std;
char a[305][305];
int visit[305][305];
int n,m;
int cotS, hangS, cotE, hangE;
int front= -1;
int	rear=-1;
int queuex[1000000];
int queuey[1000000];
int luu_vt[1000][1000];
int re[1000][1000];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ans,dem;
void push(int x, int y){
	if(rear == max-1) rear =-1;
	rear++;
	queuex[rear]=x;
	queuey[rear]=y;
}

void pop(){
	if(front == max-1) front =-1;
	front++;
}

bool IsEmpty(){
	return front == rear;
}

bool check(int i, int j){
	if(i>=1 && i <=m && j >=1 && j <=n){
		return true;
	}
	return false;
}

void Try(int hang, int cot){
	front =rear =-1;
	push(hang, cot);
	re[hang ][cot] = -1;
	luu_vt[hangS][cotS] = -1;
	while(!IsEmpty()){
		pop();
		int x1= queuex[front];
		int y1= queuey[front];

		for(int i=0; i<4; i++){
			int x2= x1+dx[i];
			int y2= y1 +dy[i];
			if( check(x2, y2) ==true && a[x2][y2] == '0' && re[x1][y1] < re[x2][y2] ){
				luu_vt[x2][y2] = i;
				if( luu_vt[x2][y2]==luu_vt[x1][y1]){
					re[x2][y2]=re[x1][y1];
				}
				else if( luu_vt[x2][y2] != luu_vt[x1][y1]){
					re[x2][y2] = re[x1][y1]+1;
				}
				push(x2,y2);
				
			}
		}
	}

}
int main(){
	freopen("Text.txt", "r", stdin);
	int test;
	cin >> test;
	
	for(int tc= 1; tc <= test; tc++){
		cin >> n >> m; // n la cot, m la hang

		cin >> cotS >> hangS >> cotE >> hangE;

		for(int i=1; i<=m; i++){
			for(int j =1; j<=n ; j++){
				cin >> a[i][j];
			}
		}
		for(int i=1; i<=m; i++){
			for(int j=1; j<=n; j++){
				re[i][j] = 1000000;
				luu_vt[i][j]=-1;
			}
		}
		
		Try(hangS, cotS);
		if(re[hangE][cotE] == 1000000) cout << -1 << endl;
		else cout << re[hangE][cotE] << endl;
	}
	return 0;
}