1. 程式人生 > >UVA 122 -- Trees on the level (二叉樹 BFS)

UVA 122 -- Trees on the level (二叉樹 BFS)

返回 錯誤 符號 pri false font width else print

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 }

技術分享圖片

UVA 122 -- Trees on the level (二叉樹 BFS)