zoukankan      html  css  js  c++  java
  • UVA101 【The Blocks Problem】

    一个大模拟!!!

    总的来说就是碰到move就要把a上面的全部放回原处。

    如果碰到onto就要把b上面的全部放到原处。
    因为move是只移动a一个,所以a上面的要归位,而pile是移一堆,所以不用。

    onto是要和b贴在一起,所以b上面的要归位,而over是上方,不需要直接接触,所以不用。。

    思路就是用栈来模拟,一开始就是n个栈。每个栈里都是一个元素,然后按照指令移,在这个栈里pop()掉它,在另一个栈里push()进去。。
    分四种情况来做移动,每种情况处理方式不一样。要注意如果是一堆移过去,因为还是要按照这个顺序,多以要先把这一堆放到另一个数组,再按顺序push进去。
    模拟完输出即可。。

    CODE:

    #include<iostream>  
    #include<cstdio>  
    #include<string>  
    #include<stack>  
    
    using namespace std;  
    
    stack <int> sta[100];  
    int t,num[100];  
    int res[100];  
    string m1,m2;  
    int p1,p2;  
      
    int main () {  
        cin >> t;  
        getchar();  
        for( int i = 0; i < t ;i++) {  
            sta[i].push(i);  
            num[i] = i;  
      
        }  
        while(1) {  
            cin >> m1;  
            if(m1 == "quit")  
                break;  
            cin >>p1 >>m2 >>p2;  
            if (num[p1] == num[p2])  
                continue;  
            if (m1 == "move" && m2 == "over") {  
                for (;sta[num[p1]].top() != p1; ) {  
                    sta[ sta[num[p1]].top() ].push(sta[num[p1]].top());   
                    num[sta[num[p1]].top()] = sta[num[p1]].top();  
                    sta[num[p1]].pop();  
                }  
                sta[num[p2]].push(p1);  
                sta[num[p1]].pop();  
                num[p1] = num[p2];  
            }  
            if (m1 == "pile" && m2 == "over") {  
                int k = 0;  
                int temp[200];  
                for (;sta[num[p1]].top() != p1; ) {  
                    temp[k++] = sta[num[p1]].top();  
                    num[sta[num[p1]].top()] = num[p2];  
                    sta[num[p1]].pop();  
                }  
                sta[num[p1]].pop();  
                temp[k] = p1;  
                num[p1] = num[p2];  
                for(int w = k ;w >= 0; w--)  
                    sta[num[p2]].push(temp[w]);  
            }  
            if (m1 == "move" && m2 == "onto") {  
                for (;sta[num[p1]].top() != p1; ) {  
                    sta[ sta[num[p1]].top() ].push(sta[num[p1]].top());   
                    num[sta[num[p1]].top()] = sta[num[p1]].top();  
                    sta[num[p1]].pop();  
                }  
                for (;sta[num[p2]].top() != p2; ) {  
                    sta[ sta[num[p2]].top() ].push(sta[num[p2]].top());   
                    num[sta[num[p2]].top()] = sta[num[p2]].top();  
                    sta[num[p2]].pop();  
                }  
                sta[num[p2]].push(sta[num[p1]].top());  
                sta[num[p1]].pop();  
                num[p1] = num[p2];  
            }  
            if (m1 == "pile" && m2 == "onto") {  
                int k = 0;  
                int temp[200];  
                for (;sta[num[p1]].top() != p1; ) {  
                    temp[k++] = sta[num[p1]].top();  
                    num[sta[num[p1]].top()] = num[p2];  
                    sta[num[p1]].pop();  
                }  
                sta[num[p1]].pop();  
                temp[k] = p1;  
                num[p1] =num[p2];  
                for (;sta[num[p2]].top() != p2; ) {  
                    sta[ sta[num[p2]].top() ].push(sta[num[p2]].top());   
                    num[sta[num[p2]].top()] = sta[num[p2]].top();  
                    sta[num[p2]].pop();  
                }  
                for(int w = k ;w >= 0; w--)  
                    sta[num[p2]].push(temp[w]);  
              
            }  
        }  
        int j;  
        for(int i = 0;i < t;i++) {  
            cout << i <<":";  
            for( j = 0 ;!sta[i].empty();j++) {  
                res[j] =  sta[i].top();  
                sta[i].pop();  
            }  
            for (j = j -1; j >= 0;j--)  
                cout<<" "<<res[j];  
            cout << endl;  
        }  
        return 0;  
    }  
    
    
    
    
  • 相关阅读:
    一种分布式框架设计(四)
    读书笔记-《拆掉思维里的墙》
    [JS前端开发] js/jquery控制页面动态载入数据 滑动滚动栏自己主动载入事件
    Qt 5.3更新无数,更改C++控制台输出最为赞
    Guava ---- Ordering排序工具
    codeforces 558D Guess Your Way Out! II 规律
    Linux shell之打印输出
    细说Oracle中NULL值
    责任成本汇总表
    NSOperationQueue小结
  • 原文地址:https://www.cnblogs.com/Repulser/p/9813291.html
Copyright © 2011-2022 走看看