• hdu 1622 Trees on the level


    题意:按层次遍历二叉树

    思路:对于不会建树的同学,直接广搜即可

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <map>
    #include <string>
    #include <queue>
    using namespace std;
    #define maxn 300
    char s[maxn];
    int d[maxn],cnt,n;
    map<string,int>mp;
    bool bfs()
    {
        string str="";
        queue<string>q;
        q.push(str);
        if(mp[str]) d[cnt++]=mp[str];
        else return false;
        while(!q.empty())
        {
            str=q.front();
            q.pop();
            if(mp[str+'L'])
            {
                d[cnt++]=mp[str+'L'];
                q.push(str+'L');
            }
            if(mp[str+'R'])
            {
                d[cnt++]=mp[str+'R'];
                q.push(str+'R');
            }
        }
        if(cnt==n) return true;
        return false;
    }
    void init()
    {
        n=0;
        cnt=0;
        mp.clear();
    }
    int main()
    {
        int i,j,k,m,len;
        init();
        while(scanf(" %s",s)!=EOF)
        {
            if(strcmp(s,"()")==0)
            {
                if(!bfs()) printf("not complete
    ");
                else
                {
                    for(i=0;i<cnt;i++)
                    {
                        printf("%d",d[i]);
                        if(i==cnt-1) printf("
    ");
                        else printf(" ");
                    }
                }
                init();
                continue;
            }
            n++;
            j=len=strlen(s);
            m=0;
            for(i=1; i<len; i++)
            {
                if(s[i]==',')
                {
                    j=i+1;
                    break;
                }
                m=m*10+s[i]-'0';
            }
            string str="";
            for(;j<len-1;j++)
                str+=s[j];
            mp[str]=m;
        }
        return 0;
    }
  • 相关阅读:
    NTC3950-10K温度传感器
    Maven常用命令:
    Linux-IIC驱动(详解)
    sourceinsight4 用设置
    LTDC/DMA2D—液晶显示***
    STM32F429的LTDC和DMA2D ***
    python机器学习sklearn 岭回归(Ridge、RidgeCV)
    random_state 参数
    python3 文件及文件夹路径相关
    机器学习:简单线性回归
  • 原文地址:https://www.cnblogs.com/zuferj115/p/5432934.html
Copyright © 2020-2023  润新知