当前位置: 代码迷 >> 综合 >> Traffic Light(动态数组+广度优先搜索)
  详细解决方案

Traffic Light(动态数组+广度优先搜索)

热度:26   发布时间:2023-11-22 13:51:34.0

题目链接
DreamGrid City is a city with n x m intersections arranged into a grid of n rows and m columns. The intersection on the i-th row and the j-th column can be described as (i,j), and two intersections (i1,j1) and (i2,j2) are connected by a road if |i1 - i2| + |j1 - j2| = 1.

At each intersection stands a traffic light. A traffic light can only be in one of the two states: 0 and 1. If the traffic light at the intersection (i,j) is in state 0, one can only move from (i,j) to (i + 1,j) or (i - 1,j); If the traffic light is in state 1, one can only move from (i,j) to (i,j + 1) or (i,j - 1) (of course, the destination must be another intersection in the city).

BaoBao lives at the intersection (si,sj), and he wants to visit his best friend DreamGrid living at the intersection (fi,fj). After his departure, in each minute the following things will happen in order:

BaoBao moves from his current intersection to another neighboring intersection along a road. As a law-abiding citizen, BaoBao has to obey the traffic light rules when moving.
Every traffic light changes its state. If a traffic light is in state 0, it will switch to state 1; If a traffic light is in state 1, it will switch to state 0.
As an energetic young man, BaoBao doesn’t want to wait for the traffic lights, and he must move in each minute until he arrives at DreamGrid’s house. Please tell BaoBao the shortest possible time he can move from (si,sj) to (fi,fj) to meet his friend, or tell him that this is impossible.

Input

There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers n and m (1 <= n x m <= 105), indicating the size of the city.

For the following n lines, the i-th line contains m integers ti1,ti2,...tim (0 <= tij <= 1), where tij indicates the initial state of the traffic light at intersection (i,j).

The next line contains four integers si,sj,fi and fj (1 <= si,fi <= n,1 <= sj,fj <= m ), indicating the starting intersection and the destination intersection.

It’s guaranteed that the sum of n x m over all test cases will not exceed 3 x 105.

Output

For each test case output one line containing one integer, indicating the shortest possible time (in minute) BaoBao can move from (si,sj) to (fi,fj) without stopping. If it is impossible for BaoBao to arrive at DreamGrid’s house, print “-1” (without quotes) instead.

Sample Input

4
2 3
1 1 0
0 1 0
1 3 2 1
2 3
1 0 0
1 1 0
1 3 1 2
2 2
1 0
1 0
1 1 2 2
1 2
0 1
1 1 1 1
Sample Output

3
5
-1
0
Hint

For the first sample test case, BaoBao can follow this path: .

(1,3) -> (2,3) -> (2,2) -> (2,1).

For the second sample test case, due to the traffic light rules, BaoBao can’t go from (1,3) to (1,2) directly. Instead, he should follow this path: .

(1,3) -> (2,3) ->(2,2) -> (2,1) -> (1,1) -> (1,2).

For the third sample test case, it’s easy to discover that BaoBao can only go back and forth between (1,1) and (1,2).

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int t,n,m,st,se,ft,fe,tmp,ans,flag;
vector <int> vec[N],vis[N];
struct Node{
    int x,y,step;Node(int xx = 0,int yy = 0,int xstep = 0) : x(xx),y(yy),step(xstep){
    }
}p;
void bfs(int stt,int see,int ftt,int fee){
    queue <Node> Q;vis[stt][see] = true;Q.push(Node(stt,see,0));while(!Q.empty()){
    p = Q.front();Q.pop();if(p.x == ftt && p.y == fee){
    flag = true;ans = p.step;return;}if(p.step & 1) vec[p.x][p.y] = !vec[p.x][p.y];if(vec[p.x][p.y] == 1){
    for(int i = 0;i < 2;i++){
    int tx,ty;if(!i){
    tx = p.x;ty = p.y + 1;}else{
    tx = p.x;ty = p.y - 1;}if(tx >= 1 && tx <= n && ty >= 1 && ty <= m && !vis[tx][ty]){
    vis[tx][ty] = true;Q.push(Node(tx,ty,p.step + 1));}}}else{
    for(int i = 0;i < 2;i++){
    int tx,ty;if(!i){
    tx = p.x + 1;ty = p.y;}else{
    tx = p.x - 1;ty = p.y;}if(tx >= 1 && tx <= n && ty >= 1 && ty <= m && !vis[tx][ty]){
    vis[tx][ty] = true;Q.push(Node(tx,ty,p.step + 1));}}}}return;
}
int main(){
    ios :: sync_with_stdio(false);cin.tie(0);#ifndef ONLINE_JUDGEfreopen("in.txt","r",stdin);#endifwhile(cin >> t){
    while(t--){
    cin >> n >> m;for(int i = 0;i <= n;i++){
    vec[i].clear();//必须要先清空,然后再申请,第一次用不熟悉!!!vis[i].clear();vec[i].resize(m + 5);vis[i].resize(m + 5);}for(int i = 1;i <= n;i++){
    for(int j = 1;j <= m;j++){
    cin >> vec[i][j];}}cin >> st >> se >> ft >> fe;if(st == ft && se == fe) {
    cout << 0 << endl;continue;}flag = ans = false;bfs(st,se,ft,fe);if(!flag) cout << -1 << endl;else cout << ans << endl;}}return 0;
}
  相关解决方案