zoukankan      html  css  js  c++  java
  • UVA 122 -- Trees on the level (二叉树 BFS)

     Trees on the level UVA - 122 

    解题思路:

      首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束。因此可以专门编写一个readin()函数,类型设置为bool型,遇到第一种情况时返回true,遇到第二种情况返回false,主程序中只要发现readin返回false时就break,结束整个大循环。

      接下来要建立二叉树,首先为二叉树编写一个结构体,然后根据字符串的输入情况来建树,如果是‘L’就往左走,走不动时建一颗新树,同样的方法处理右子树,最后读入结点值。由于输入可能有误,因此用一个全局变量failed来记录是否有输入错误的情况出现,如果在建树过程中发现该结点已经被赋过值,那么全局变量failed变为true。

      最后开始BFS找结点值,此时可能出现有结点没有结点值的情况,因此要把bfs定义为bool型,只要出现这种非法情况,返回false。最后便不难根据情况进行输出了。

     sscanf() - 从一个字符串中读进与指定格式相符的数据.

      函数原型:
      Int sscanf( string str, string fmt, mixed var1, mixed var2 ... );
      int scanf( const char *format [,argument]... );

      说明:
      sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。
      其中的format可以是一个或多个 {%[*] [width] [{h | l | I64 | L}]type | ' ' | '/t' | '/n' | 非%符号}
      注:
      1、 * 亦可用于格式中, (即 %*d 和 %*s) 加了星号 (*) 表示跳过此数据不读入. (也就是不把此数据读入参数中)
      2、{a|b|c}表示a,b,c中选一,[d],表示可以有d也可以没有d。
      3、width表示读取宽度。
      4、{h | l | I64 | L}:参数的size,通常h表示单字节size,I表示2字节 size,L表示4字节size(double例外),l64表示8字节size。
      5、type :这就很多了,就是%s,%d之类。
      6、特别的:%*[width] [{h | l | I64 | L}]type 表示满足该条件的被过滤掉,不会向目标参数中写入值
      支持集合操作:
      %[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)
      %[aB'] 匹配a、B、'中一员,贪婪性
      %[^a] 匹配非a的任意字符,贪婪性
     

      1 #include<iostream>
      2 #include<cstring>
      3 #include<string>
      4 #include<cstdio>
      5 #include<vector>
      6 #include<queue>
      7 using namespace std;
      8 
      9 const int maxn = 270;
     10 vector<int> ans;
     11 bool failed;
     12 class Node{
     13 public:
     14     bool create;//表示该结点是否已经建立
     15     int v;//结点值
     16     Node *left,*right;
     17     Node():create(false),left(NULL),right(NULL){}//初始化
     18 }*root;//根节点
     19 char a[maxn];//保存读入的节点
     20 
     21 void create_tree(int v,char *tr)
     22 {
     23     int len = strlen(tr);
     24     Node *temp = root;
     25     if(len == 1)
     26     {
     27         temp->v = v;
     28         temp->create = true;
     29         return;
     30     }
     31     for(int i=0;i<len-1;i++)///注意最后一个是')'
     32     {
     33         if(tr[i] == 'L')///向左子树
     34         {
     35             if(temp->left == NULL)
     36             {
     37                 temp->left = new Node;
     38             }
     39             temp = temp->left;
     40         }
     41         else{
     42             if(temp->right == NULL)
     43                 temp->right = new Node;
     44             temp = temp->right;
     45         }
     46     }
     47     if(temp->create == true) failed = true;///已经赋值,说明输入有误
     48     temp->v = v;
     49     temp->create = true;
     50 }
     51 
     52 bool read_input()
     53 {
     54     failed = false;
     55     root = new Node;///创建根节点
     56     for(;;)
     57     {
     58         if(scanf("%s",a) != 1) return false;///整个输入结束
     59         if(!strcmp(a,"()")) break;///读到结束标志,退出循环
     60         int v;
     61         sscanf(&a[1],"%d",&v);
     62         create_tree(v,strchr(a,',')+1);
     63     }
     64     return true;
     65 }
     66 int print_tree()
     67 {
     68     queue<Node*> q;
     69     ans.clear();///清空 vector<int> ans;
     70     q.push(root);///初始时只有一个根节点
     71     while(!q.empty())
     72     {
     73         Node *temp = q.front();q.pop();
     74         if(temp->create == false)///没有被建立的结点
     75             return 0;
     76         ans.push_back(temp->v);
     77         if(temp->left != NULL) q.push(temp->left);
     78         if(temp->right != NULL) q.push(temp->right);
     79     }
     80     return 1;
     81 }
     82 
     83 
     84 int main()
     85 {
     86     while(read_input())//有输入则建树
     87     {
     88         ///开始遍历树,层次输出
     89         int temp = print_tree();
     90         if(temp == 0 || failed)
     91             cout<<"not complete"<<endl;
     92         else{
     93             int len = ans.size();
     94             for(int i=0;i<len;i++)
     95             {
     96                 cout<<ans[i];
     97                 if(i == len-1) cout<<endl;
     98                 else cout<<" ";
     99             }
    100         }
    101         }
    102     return 0;
    103 }

  • 相关阅读:
    [导入]Zanzarah: The Hidden Portal Cheats
    [导入]Zanzarah Walkthrough
    Each的实现
    关于SQL命令中不等号(!=,<>)
    获取当前运行函数及调用函数
    MVC3.0学习2Razor视图引擎基础语法
    MVC 3.0 学习1
    java.io.IOException: Too many open files错误解决方案
    linux下执行命令输出乱码解决方案
    通用代码引擎生成工具 XDoclet 介绍
  • 原文地址:https://www.cnblogs.com/yxh-amysear/p/8449045.html
Copyright © 2011-2022 走看看