zoukankan      html  css  js  c++  java
  • 75 金字塔转换矩阵(756)

    作者: Turbo时间限制: 1S章节: 深度优先搜索

    晚于: 2020-08-26 12:00:00后提交分数乘系数50%

    截止日期: 2020-09-02 12:00:00

    问题描述 :

    现在,我们用一些方块来堆砌一个金字塔。 每个方块用仅包含一个字母的字符串表示。

    使用三元组表示金字塔的堆砌规则如下:

    对于三元组(A, B, C) ,“C”为顶层方块,方块“A”、“B”分别作为方块“C”下一层的的左、右子块。当且仅当(A, B, C)是被允许的三元组,我们才可以将其堆砌上。

    初始时,给定金字塔的基层 bottom,用一个字符串表示。一个允许的三元组列表 allowed,每个三元组用一个长度为 3 的字符串表示。

    如果可以由基层一直堆到塔尖就返回 true ,否则返回 false 。

    示例 1:

    输入:bottom = "BCD", allowed = ["BCG", "CDE", "GEA", "FFF"]

    输出:true

    解析:

    可以堆砌成这样的金字塔:

        A

       /

      G   E

     / /

    B   C   D

    因为符合('B', 'C', 'G'), ('C', 'D', 'E') 和 ('G', 'E', 'A') 三种规则。

    示例 2:

    输入:bottom = "AABA", allowed = ["AAA", "AAB", "ABA", "ABB", "BAC"]

    输出:false

    解析:

    无法一直堆到塔尖。

    注意, 允许存在像 (A, B, C) 和 (A, B, D) 这样的三元组,其中 C != D。

    提示:

    bottom 的长度范围在 [2, 8]。

    allowed 的长度范围在[0, 200]。

    方块的标记字母范围为{'A', 'B', 'C', 'D', 'E', 'F', 'G'}。

    可使用以下main函数:

    int main()

    {

        string bottom,rule;

        vector<string> allowed;

        cin>>bottom;

        int n;

        cin>>n;

        for(int i=0; i<n; i++)

        {

            cin>>rule;

            allowed.push_back(rule);

        }

        bool res=Solution().pyramidTransition(bottom, allowed);

        cout<<(res?"true":"false")<<endl;

        return 0;

    }

    输入说明 :

    首先输入bottom,

    然后输入allowed包含的三元组数目n,

    最后输入n个字符串,每个字符串表示一个三元组

    bottom 的长度范围在 [2, 8]。

    allowed 的长度n的范围在[0, 200]。

    输出说明 :

    输出true或false

    输入范例 :

    输出范例 :

    #include <iostream>
    #include <vector>
    #include <unordered_set>
    using namespace std;
    
    
    class Solution {
    public:
        bool pyramidTransition(string bottom, vector<string>& allowed) 
        {
            unordered_set<string> S(allowed.begin(),allowed.end());
            return dfs(bottom,"",S);
        }
        bool dfs(string &bottom,string next,unordered_set<string> &S)
        {
            int m=bottom.size(),n=next.size();
            if(m==1)
                return true;
            if(m-n>1)
            {
                string temp=bottom.substr(n,2);//取出下一个需要构造的三元组前两个元素
                for(char ch='A';ch<'H';ch++)
                {
                    auto it=S.find(temp+ch);
                    //如果能在集合里面找到,即合法,进行下一次查找;
                    if(it!=S.end()&&dfs(bottom,next+(*it)[2],S)) 
                        return true;     
                }
                return false;
            }
            else//此时next这一层已经构造完毕,更新bottom为next,继续使用next构造下一层
            {
                return dfs(next,"",S);
            }
            
        }
    };
    
    int main()
    {
        string bottom,rule;
        vector<string> allowed;
    
        cin>>bottom;
        int n;
        cin>>n;
    
        for(int i=0; i<n; i++)
        {
            cin>>rule;
            allowed.push_back(rule);
        }
        bool res=Solution().pyramidTransition(bottom, allowed);
        cout<<(res?"true":"false")<<endl;
        return 0;
    }
  • 相关阅读:
    Java设计模式——单例模式
    Java设计模式——工厂模式
    多线程
    Collection集合
    内部类
    多态
    接口
    面向对象(2)
    数组
    面向对象(1)
  • 原文地址:https://www.cnblogs.com/zmmm/p/13648638.html
Copyright © 2011-2022 走看看