• uva 101 木块问题


    题目大意:

    输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为0~n-1的位置。现对这些木块进行操作,操作分为四种。

    1、move a onto b:把木块a、b上的木块放回各自的原位,再把a放到b上;

    2、move a over b:把a上的木块放回各自的原位,再把a发到含b的堆上;

    3、pile a onto b:把b上的木块放回各自的原位,再把a连同a上的木块移到b上;

    4、pile a over b:把a连同a上木块移到含b的堆上。

    当输入quit时,结束操作并输出0~n-1的位置上的木块情况

    Sample Input 
    10
    move 9 onto 1
    move 8 over 1
    move 7 over 1
    move 6 over 1
    pile 8 over 6
    pile 8 over 5
    move 2 over 1
    move 4 over 9
    quit
    Sample Output 
     0: 0
     1: 1 9 2 4
     2:
     3: 3
     4:
     5: 5 8 7 6
     6:
     7:
     8:
     9:

     思路:一个堆一个一维数组,整体二维数组(不定长数组使用STL中的vector来完成)

    审题可发现move会将a上方的木块归位,onto会将b上方的木块归位

    因此只需要判断move和onto将相应的归位操作完成后,即可直接将(a及a以上的木块)移到(b及b以上的木块)之上

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<algorithm>
    using namespace std;
    const int maxn = 25;
    void start_block(int n);
    void show_block();
    void found_block(int a, int& p, int& h);
    void clear(int p, int h);
    void move(int a, int ha, int b);
    int n;
    vector<int> pile[maxn];
    int main()
    {
        string s1, s2;
        int a, b;
        cin >> n;
        start_block(n);
        show_block();
        while (cin >> s1&&s1 != "quit"&&cin >> a >> s2 >> b)
        {
            int pa, pb, ha, hb;
            found_block(a, pa, ha);
            found_block(b, pb, hb);
            //if (s1 == "quit") break;
            if (pa == pb) continue;
            if (s1 == "move")clear(pa, ha);
            if (s1 == "onto")clear(pb, hb);
            move(pa, ha, pb);
        }
        show_block();
    return 0; }
    void start_block(int n)//初始化木块 { for (int i = 0; i < n; i++) { pile[i].push_back(i); } } void found_block(int a, int& p, int& h)//查找a木块的位置,以引用的形式返回调用者 { for (p = 0; p < n; p++) { for (h = 0; h < pile[p].size(); h++) if (pile[p][h] == a)return; } } void show_block() { for (int i = 0; i < n; i++) { cout << i << ":"; for (int j = 0; j < pile[i].size(); j++) { cout << " " << pile[i][j]; } cout << endl; } } void clear(int p, int h) //将第p堆h高度以上的木块归位 { for (int i = h + 1; i < pile[p].size(); i++) { int x = pile[p][i]; pile[x].push_back(x); } pile[p].resize(h + 1); } void move(int a, int ha, int b) //(a及a以上的木块(b及b以上的木块)之上 { for (int i = ha; i < pile[a].size(); i++) { pile[b].push_back(pile[a][i]); } pile[a].resize(ha); }

     问题:

    这里第一次使用了cin当作while条件,以前一直用的是while(scanf)来判断的

    scanf作为返回值是返回的是正确接收的个数,而cin并不是如此

    cin当作while循环条件的详细测试将写在下一篇博客里

    不定长数组:vector

    头文件
    #include<vector>
    vector<int> vec;
    
    
    迭代器:
    vector<int>::iteratorite ite = vec.begin();
    
    vec.push_back(1);尾部添加
    vec.pop_back(1);尾部删除 (vs里增加已存在空间的一半) vec.reserve(
    10);容量变大 vec.resize(3);重新设置有效原数个数 vec.empty(); vec.at(3);元素的引用 vec.back();最后一个元素 #include<algorithm> vec.insert(vec.begin()+2,12); 添加元素 vec.clear(); vec.assign((vec.begin(),vec.end()); vec.erase(vec.begin(),vec.end()); vec.assign(2,5); 迭代器失效
    算法:
    #include<algorithm>
    遍历:
    for_each(vec.begin(),vec.end(),函数);
    排序:
    sort(vec.begin(),vec.end());
    #include<functional>
    sort(str.begin(),str.end(),greater<int>());从大到小
    乱序:
    random_shuffle(vec.begin(),vec.end());
  • 相关阅读:
    基于antlr的表达式解析器
    ANTLR语法层的选项及动作
    Understanding ANTLR Grammar Files
    写给Git初学者的7个建议
    Top 8 Diagrams for Understanding Java
    技术面不深入
    一个初级程序员学习新技术的策略
    SoftReference,WeakReference&WeakHashMap
    探索Antlr(Antlr 3.0更新版)
    Five minute introduction to ANTLR 3
  • 原文地址:https://www.cnblogs.com/seamusopen/p/8447013.html
Copyright © 2020-2023  润新知