ZOJ 4020 Traffic Light

原创 2018年04月15日 14:03:14
Traffic Light

Time Limit: 1 Second      Memory Limit: 131072 KB

DreamGrid City is a city with  intersections arranged into a grid of  rows and  columns. The intersection on the -th row and the -th column can be described as , and two intersections  and  are connected by a road if .

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  is in state 0, one can only move from  to  or ; If the traffic light is in state 1, one can only move from  to  or  (of course, the destination must be another intersection in the city).

BaoBao lives at the intersection , and he wants to visit his best friend DreamGrid living at the intersection . 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  to  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 , indicating the number of test cases. For each test case:

The first line contains two integers  and  (), indicating the size of the city.

For the following  lines, the -th line contains  integers  (), where  indicates the initial state of the traffic light at intersection .

The next line contains four integers  and  (), indicating the starting intersection and the destination intersection.

It's guaranteed that the sum of  over all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the shortest possible time (in minute) BaoBao can move from  to  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
2 2
1 1 0
1 3 1 2
1 2
1 0
1 0
1 1 2 2
1 1 1 1
0 1

Sample Output

3
5
-1
0

Hint

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

For the second sample test case, due to the traffic light rules, BaoBao can't go from  to  directly. Instead, he should follow this path: .

For the third sample test case, it's easy to discover that BaoBao can only go back and forth between  and 

get:数组太大可以用向量来存,记得先push_back(0)使得矩阵列从1开始。以为数组太小段错误,wa了好几次

状态变化也很容易解决,步数为偶数次的时候不变,奇数次的时候相反

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define Max (int)1e5+10
struct node
{
	int x,y,d;
};
vector<int> a[Max],vis[Max];
typedef  pair<int ,int > p;
int sx, sy, fx, fy,n,m,mi;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool is(int x,int y)
{
	if(x>0&&x<=n&&y>0&&y<=m&&vis[x][y]==0)
	return 1;
	return 0;
}
int bfs()
{
	queue<node> que;
	vis[sx][sy] = 1;
	node start;
	start.d=0;
	start.x=sx;
	start.y=sy;
	que.push(start);
	while(que.size())
	{
		start=que.front();
		que.pop();
		int x=start.x,y=start.y; 
	    int state=a[x][y];
	    if(start.d%2)
	    {
	    	if(state)
	    	   state=0;
	    	else
	    	    state=1;
		}
		if (state == 0)
		{
			node t;
			for(int i=2;i<4;i++)
			{
				t.x=x+dx[i];
				t.y=y+dy[i];
				t.d=start.d+1;
				if(t.x== fx&& t.y==fy)
	               return t.d;
				if(is(t.x,t.y))
				{
					vis[t.x][t.y] = 1;
			        que.push(t);
				}
			}
		}
		else
		{
			node t;
			for(int i=0;i<2;i++)
			{
				t.x=x+dx[i];
				t.y=y+dy[i];
				t.d=start.d+1;
				if(t.x== fx&& t.y==fy)
	               return t.d;
				if(is(t.x,t.y))
				{
					vis[t.x][t.y] = 1;
			        que.push(t);
				}
			}
		}
    }
    return -1;
}
int main()
{
	int t,i,j;
	cin >> t;
	while (t--)
	{
		scanf("%d%d", &n, &m);
		for(int i=1;i<=n;i++)
		   {
		   	a[i].clear();
		   	vis[i].clear();
		   	a[i].push_back(0);
		   	vis[i].push_back(0);
		   }
		for (i = 1; i <= n; i++)
			for (j = 1; j <= m; j++)
			{
				int x;
				cin>>x;
				a[i].push_back(x);
				vis[i].push_back(0);	
			}

		scanf("%d%d%d%d", &sx, &sy, &fx, &fy);
		if(sx==fx&&sy==fy)
			printf("0\n");
		else	
		    printf("%d\n", bfs());
	}
	return 0;
}



[ZOJ] 4020 G- Traffic Light (BFS)

 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5748Traffic LightTime Limit: 1 Seco...
  • sizaif
  • sizaif
  • 2018年04月07日 22:08
  • 43

ZOJ-3203-Light Bulb【6th浙江省赛】【数学公式】【三分】

ZOJ-3203-Light Bulb
  • loy_184548
  • loy_184548
  • 2016年02月21日 16:23
  • 478

ZOJ3203 Light Bulb[三分/推公式]

Light Bulb Time Limit:1000MS     Memory Limit:32768KB Submit Status Description Compared ...
  • ControlBear
  • ControlBear
  • 2017年01月08日 22:38
  • 644

zoj3203 Light Bulb(三分)

Problem Description Compared to wildleopard's wealthiness, his brother mildleopard is rather poor. H...
  • u013776243
  • u013776243
  • 2014年03月15日 14:54
  • 524

交通信号灯识别公开数据集 Traffic Lights Recognition (TLR) public benchmarks

Traffic Lights Recognition (TLR) public benchmarks Urban scene 1 Dataset © Read t...
  • GarfieldEr007
  • GarfieldEr007
  • 2016年05月27日 20:40
  • 10985

POJ-1211Traffic Lights

Description One way of achieving a smooth and economical drive to work is to `catch' every traffic ...
  • u014583062
  • u014583062
  • 2014年11月25日 18:30
  • 601

poj 1158 TRAFFIC LIGHTS

// poj 1158 TRAFFIC LIGHTS (Dijkstra算法的应用) /* 其实最短路的裸题几乎不会出现,要学的是最优路径的性质的应用,本题虽然加了个时间限制, 但如果能看穿并证...
  • birdforever
  • birdforever
  • 2010年09月06日 00:21
  • 947

poj1158 - TRAFFIC LIGHTS

想看更多的解题报告:http://blog.csdn.net/wangjian8006/article/details/7870410                                ...
  • wangjian8006
  • wangjian8006
  • 2012年09月04日 19:19
  • 901

sgu 103 Traffic Lights

题目描述: 103. Traffic Lights Time limit per test: 0.5 second(s) Memory limit: 4096 kilobytes ...
  • utoppia
  • utoppia
  • 2012年10月23日 21:02
  • 634

ZOJ - Traffic Light - 搜索题

Traffic LightTime Limit: 1 Second      Memory Limit: 131072 KB题解:         给你一个矩阵,里面都是1/0的数字,再给你两个坐标,...
  • qq_40513946
  • qq_40513946
  • 2018年04月07日 20:00
  • 12
收藏助手
不良信息举报
您举报文章:ZOJ 4020 Traffic Light
举报原因:
原因补充:

(最多只允许输入30个字)