ZOJ - 4020 Traffic Light(BFS)

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.InputThere 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 .OutputFor 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 Input4
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 Output3
5
-1
0
HintFor 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 .

思路:本来想用int或者bool来存图,但是都爆了(太大了),后来才知道vector也可以存图(是我太菜了)。本来我想在每次搜图的时候都不断改变红绿灯的数值(0或1),但发现这样的复杂度太高。应该根据现走的步数为单数或者双数来进行处理(为单数的时候,所在位置或下一个位置的数字都相反,即0变1,1变0。为双数的时候,所有位置与刚开始的一样)。存了之后,用bfs根据不同情况来进行搜索。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 8;

int path1[2][2] = { 0,1, 0,-1 };//左右
int path2[2][2] = { 1,0, -1,0 };//上下

vector<int>status[maxn];

struct node
{
    int x, y, sum;
};

int ans;
int t, n, m, sx, sy, ex, ey;
int bfs(int x, int y)
{
    bool sign[n+1][m+1];
    memset(sign, 0, sizeof(sign));
    sign[x][y] = 1;
    queue<node>q;
    node p;
    p.x = x;
    p.y = y;
    p.sum = 0;
    q.push(p);
    while (!q.empty())
    {
        node r = q.front();
        q.pop();
        if (r.x == ex && r.y == ey)return r.sum;
        if (r.sum % 2 == 0)//本身
        {
            if (status[r.x][r.y])
            {
                for (int i = 0; i<2; i++)
                {
                    node w;
                    w.x = r.x + path1[i][0];
                    w.y = r.y + path1[i][1];
                    if (w.y >= 0 && w.y < m && w.x>=0 && w.x < n && !sign[w.x][w.y])
                    {
                        w.sum = r.sum + 1;
                        sign[w.x][w.y] = 1;
                        q.push(w);
                    }
                }
            }
            else
            {
                for (int i = 0; i<2; i++)
                {
                    node w;
                    w.x = r.x + path2[i][0];
                    w.y = r.y + path2[i][1];
                    if (w.y >= 0 && w.y < m && w.x >= 0 && w.x < n && !sign[w.x][w.y])
                    {
                        w.sum = r.sum + 1;
                        sign[w.x][w.y] = 1;
                        q.push(w);
                    }
                }
            }
        }
        else
        {
            if (!status[r.x][r.y])
            {
                for (int i = 0; i<2; i++)
                {
                    node w;
                    w.x = r.x + path1[i][0];
                    w.y = r.y + path1[i][1];
                    if (w.y >= 0 && w.y < m && w.x >= 0 && w.x < n && !sign[w.x][w.y])
                    {
                        w.sum = r.sum + 1;
                        sign[w.x][w.y] = 1;
                        q.push(w);
                    }
                }
            }
            else
            {
                for (int i = 0; i<2; i++)
                {
                    node w;
                    w.x = r.x + path2[i][0];
                    w.y = r.y + path2[i][1];
                    if (w.y >= 0 && w.y < m && w.x >= 0 && w.x < n && !sign[w.x][w.y])
                    {
                        w.sum = r.sum + 1;
                        sign[w.x][w.y] = 1;
                        q.push(w);
                    }
                }
            }
        }
    }
    return -1;
}

int main()
{
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d", &n, &m);
        int f;
        for (int i = 0; i<=n; i++)
            status[i].clear();
        for (int i = 0; i < n; i++)//存图的时候是从0开始存
            for (int j = 1; j <= m; j++)
            {
                scanf("%d", &f);
                status[i].push_back(f);
            }
        scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
        sx--; sy--; ex--; ey--;//因为存图是从0开始,但是题目给的是1~n,所以要让题目给的数据都-1
        ans = bfs(sx, sy);
        printf("%d\n", ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/RootVount/p/10729625.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值