zoukankan      html  css  js  c++  java
  • [图论][BFS]Traffic Light

    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
    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: .

    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 .

    思路:bfs裸题,需要注意的是题目给出的数据范围是1=<n*m<=3*10^5,开二维数组的时候可以直接开Map[n+1][m+1]或

    vector<vector<int> > Map(n+1,vector<int>(m+1));

    AC代码:

    #include <iostream>
    #include<cstdio>
    #include<queue>
    using namespace std;
    
    struct node{
      int x,y,step;
    };
    int dire[2]={-1,1};
    
    int bfs(){
      int n,m;
      scanf("%d%d",&n,&m);
      int Map[n+1][m+1];
      int vis[n+1][m+1];
      for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&Map[i][j]);
            vis[i][j]=0;
        }
      }
      int x,y,xx,yy;
      scanf("%d%d%d%d",&x,&y,&xx,&yy);
      queue<node> q;
      while(!q.empty()) q.pop();
      node head;
      head.x=x; head.y=y; head.step=0;
      vis[head.x][head.y]=1;
      q.push(head);
      while(!q.empty()){
        node head=q.front();
        q.pop();
        if(head.x==xx&&head.y==yy) return head.step;
        int op=(Map[head.x][head.y]+head.step)%2;
        if(op==1){
             for(int i=0;i<2;i++){
                node next;
                next.x=head.x; next.y=head.y+dire[i]; next.step=head.step+1;
                if(vis[next.x][next.y]) continue;
                if(next.y<1||next.y>m) continue;
                vis[next.x][next.y]=1;
                q.push(next);
             }
        }
        if(op==0){
            for(int i=0;i<2;i++){
                node next;
                next.x=head.x+dire[i]; next.y=head.y; next.step=head.step+1;
                if(vis[next.x][next.y]) continue;
                if(next.x<1||next.x>n) continue;
                vis[next.x][next.y]=1;
                q.push(next);
            }
        }
      }
      return -1;
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--){
         int ans;
         ans=bfs();
         printf("%d
    ",ans);
        }
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    linux下查看jdk路径
    mysql内部级联删除
    Mybatis Mapper.java和Mapper.xml能否分离问题
    The request sent by the client was syntactically incorrect问题解决
    centos下-MariaDB的安装
    对于mariadb安装后可以默认使用无密码登录的问题解决方案
    C#复习笔记(2)--C#1所搭建的核心基础
    C#精粹--协变和逆变
    linux和sqlserver 2017的安装
    C#精粹--闭包陷阱
  • 原文地址:https://www.cnblogs.com/lllxq/p/8737409.html
Copyright © 2011-2022 走看看