zoukankan      html  css  js  c++  java
  • Codeforces Round #590 (Div. 3) C. Pipes

    链接:

    https://codeforces.com/contest/1234/problem/C

    题意:

    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:

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

    Examples of connected pipes
    Let's describe the problem using some example:

    The first example input
    And its solution is below:

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

    思路:

    记录当前位置和上一位置, 枚举情况即可.

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int MAXN = 2e5+10;
    
    char Map[2][MAXN];
    int n;
    
    int main()
    {
        int t;
        scanf("%d", &t);
        while (t--)
        {
            scanf("%d
    ", &n);
            scanf("%s%s", Map[0], Map[1]);
            int nowx = 0, nowy = 0, lasx = -1, lasy = -1;
            bool flag = true;
            while (nowy < n)
            {
                if (lasx == -1)
                {
                    lasx = nowx, lasy = nowy;
                    if (Map[nowx][nowy]-'0' <= 2)
                        nowy++;
                    else
                        nowx ^= 1;
                    continue;
                }
                if (nowx == lasx)
                {
                    if (Map[lasx][lasy]-'0' <= 2)
                    {
                        lasx = nowx, lasy = nowy;
                        if (Map[nowx][nowy]-'0' <= 2)
                            nowy++;
                        else
                            nowx ^= 1;
                    }
                    else
                    {
                        lasx = nowx, lasy = nowy;
                        if (Map[nowx][nowy]-'0' <= 2)
                            nowy++;
                        else
                            nowx ^= 1;
                    }
                }
                else
                {
                    if (Map[lasx][lasy]-'0' <= 2)
                    {
                        flag = false;
                        break;
                    }
                    else
                    {
                        if (Map[nowx][nowy]-'0' <= 2)
                        {
                            flag = false;
                            break;
                        }
                        else
                        {
                            lasx = nowx, lasy = nowy;
                            nowy++;
                        }
                    }
                }
            }
    //        cout << line << endl;
            if (!flag || nowx != 1)
                puts("NO");
            else
                puts("YES");
        }
    
        return 0;
    }
    
  • 相关阅读:
    【BZOJ】1076: [SCOI2008]奖励关(状压dp+数学期望)
    【COGS & USACO】896. 圈奶牛(凸包)
    【wikioi】1553 互斥的数(hash+set)
    【wikioi】1229 数字游戏(dfs+水题)
    【COGS】714. USACO 1.3.2混合牛奶(贪心+水题)
    【wikioi】1403 新三国争霸(dp+kruskal)
    【wikioi】1108 方块游戏(模拟)
    [LeetCode] 270. Closest Binary Search Tree Value 最近的二叉搜索树的值
    [LeetCode] 261. Graph Valid Tree 图是否是树
    [LeetCode] 486. Predict the Winner 预测赢家
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11618852.html
Copyright © 2011-2022 走看看