zoukankan      html  css  js  c++  java
  • (补题 CF 1234C)Pipes

    Description

    You are given a system of pipes. It consists of two rows, each row consists of (n) pipes. The top left pipe has the coordinates ((1,1)) and the bottom right —$ (2,n)$.

    There are six types of pipes: two types of straight pipes and four types of curved pipes. Here are the examples of all six types:

    imgTypes of pipes

    You can turn each of the given pipes (90) degrees clockwise or counterclockwise arbitrary (possibly, zero) number of times (so the types (1) and (2) can become each other and types (3,4,5,6) can become each other).

    You want to turn some pipes in a way that the water flow can start at ((1,0)) (to the left of the top left pipe), move to the pipe at ((1,1)), flow somehow by connected pipes to the pipe at ((2,n)) and flow right to ((2,n+1)).

    Pipes are connected if they are adjacent in the system and their ends are connected. Here are examples of connected pipes:

    imgExamples of connected pipes

    Let's describe the problem using some example:

    imgThe first example input

    And its solution is below:

    imgThe first example answer

    As you can see, the water flow is the poorly drawn blue line. To obtain the answer, we need to turn the pipe at ((1,2)) (90)degrees clockwise, the pipe at ((2,3)) (90) degrees, the pipe at ((1,6)) (90) degrees, the pipe at ((1,7)) (180) degrees and the pipe at $(2,7) $ (180) degrees. Then the flow of water can reach ((2,n+1)) from ((1,0)).

    You have to answer (q) independent queries.

    Input

    The first line of the input contains one integer (q) ((1≤q≤104)) — the number of queries. Then (q) queries follow.

    Each query consists of exactly three lines. The first line of the query contains one integer (n) ((1≤n≤2⋅10^5)) — the number of pipes in each row. The next two lines contain a description of the first and the second rows correspondingly. Each row description consists of (n) digits from (1) to (6) without any whitespaces between them, each digit corresponds to the type of pipe in the corresponding cell. See the problem statement to understand which digits correspond to which types of pipes.

    It is guaranteed that the sum of (n) over all queries does not exceed (2⋅10^5).

    Output

    For the (i)-th query print the answer for it — "YES" (without quotes) if it is possible to turn some pipes in a way that the water flow can reach ((2,n+1)) from ((1,0)), and "NO" otherwise.

    Example

    Input

    6
    7
    2323216
    1615124
    1
    3
    4
    2
    13
    24
    2
    12
    34
    3
    536
    345
    2
    46
    54
    

    Output

    YES
    YES
    YES
    NO
    YES
    NO
    

    Note

    The first query from the example is described in the problem statement.

    题目大意&思路

    接水管游戏,数字代表相应的管子。给一个2*n的一个图,问能否通过旋转管子使左上角的水流能够流到右下角。

    虽然有六种管子,但是可以大概分为两类:1、2为一类;3、4、5、6为另外一列。利用dfs进行搜索。记录每个管子的位置和流向,看能否在右下角横着流出。

    代码样例

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 100000 + 10;
    bool flag; 
    int n;
    char pi[2][2 * maxn];
    
    /**
     * 自定义一下方向
     * 1、2、3、4分别是左,下,右,上
     */
    
    void dfs(int x, int y, int to)
    {
        if (y >= n)
            return;
        if (x == 1 && y == n - 1 && to == 3)
        {
            flag = true;
            return;
        }
        if (x == 0)
        {
            if (to == 2)
            {
                if (pi[x + 1][y] != '1' && pi[x + 1][y] != '2')
                    dfs(x + 1, y, 3);
            }
            else if (to == 3)
            {
                if (pi[x][y + 1] == '1' || pi[x][y + 1] == '2')
                    dfs(x, y + 1, 3);
                else
                    dfs(x, y + 1, 2);
            }
        }
        else if (x == 1)
        {
            if (to == 4)
            {
                if (pi[x - 1][y] != '1' && pi[x - 1][y] != '2')
                    dfs(x - 1, y, 3);
            }
            else if (to == 3)
            {
                if (pi[x][y + 1] == '1' || pi[x][y + 1] == '2')
                    dfs(x, y + 1, 3);
                else
                    dfs(x, y + 1, 4);
            }
        }
    }
    
    int main()
    {
        int t;
        cin >> t;
        while (t--)
        {
            cin >> n;
            cin >> pi[0] >> pi[1];
            flag = false;
            if (pi[0][0] == '1' || pi[0][0] == '2')
                dfs(0, 0, 3);
            else
                dfs(0, 0, 2);
            if (flag)
                cout << "YES" << endl;
            else
                cout << "NO" << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    topcoder srm 708 div1
    FBX SDK在vs 2010下面的配置
    Google Protocol Buffer在vs2010下配置
    topcoder srm 709 div1
    topcoder srm 707 div1
    CNN Mnist
    SVM学习笔记5-SMO
    SVM学习笔记4-核函数和离群点的处理
    SVM学习笔记3-问题转化
    如何使用SSL pinning来使你的iOS APP更加安全
  • 原文地址:https://www.cnblogs.com/cafu-chino/p/11759466.html
Copyright © 2011-2022 走看看