zoukankan      html  css  js  c++  java
  • uva 101

    题目:给你n个方块,有四种操作:
                1.move a onto b,把a和b上面的方块都放回原来位置,然后把a放到b上面;

                2.move a over b,把a上面的放回原处,然后把a放在b所在的方块堆的上面;

                3.pile a onto b,把b上面的放回原来位置,然后把a所在的堆整体放到b上面;

                4.pile a over b,吧a所在堆整体放到b所在堆的上面。

    分析:模拟,数据结构。观察操作,如果是move就是先把a上面的还原,如果是onto就是先把b上面的还原。

                然后,就是移动一堆到另一堆的上面(单个也认为是一堆)。所以设置两个基础操作:

                1.将a上面的还原init_place(a);

                2.将a和上面的(可以没有上面的)放到b上面pile_a_to_b(a,b)。

                那么上述的四组操作就变成下面了:

                1.move a onto b,init_place(a);init_place(b);pile_a_to_b(a,b);

                2.move a over b,init_place(a);pile_a_to_b(a,b);

                3.pile a onto b,init_place(b);pile_a_to_b(a,b);

                4.pile a over b,pile_a_to_b(a,b)。

                利用两个操作轻松解决。具体实现时设置一个place数组记录每个编号的方块对应的堆。

    注意:如果a和b已经在一堆中就不要操作,此时认为不用移动,否则会WA。

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 using namespace std;
     5 
     6 int place[25];
     7 int stack[25][25];
     8 int top[25];
     9 
    10 //将a上面的放回原位
    11 void init_place( int a )
    12 {
    13     int block,id = place[a];
    14     while ( stack[id][top[id]] != a ) {
    15         block = stack[id][top[id] --];
    16         place[block] = block;
    17         stack[block][++ top[block]] = block;
    18     }
    19 }
    20 
    21 //将a和上面的全都移动到b上
    22 int  temp[25];
    23 void pile_a_to_b( int a, int b )
    24 {
    25     int topt = -1,id = place[a],ID = place[b];
    26 
    27     //先将a上面的逆序存入temp
    28     while ( stack[id][top[id]] != a )
    29         temp[++ topt] = stack[id][top[id] --];
    30 
    31     //再存入a
    32     place[a] = ID;
    33     stack[ID][++ top[ID]] = a;
    34     top[id] --;
    35 
    36     //最后将temp里面的逆序存入b
    37     while ( topt >= 0 ) {
    38         place[temp[topt]] = ID;
    39         stack[ID][++ top[ID]] = temp[topt --];
    40     }
    41 }
    42 
    43 int main()
    44 {
    45     int  n,a,b;
    46     char oper[5],type[5];
    47     while ( ~scanf("%d",&n) ) {
    48         for ( int i = 0 ; i < n ; ++ i ) {
    49             stack[i][0] = i;
    50             place[i] = i;
    51             top[i] = 0;
    52         }
    53         while ( scanf("%s",oper) && oper[0] != 'q' ) {
    54             scanf("%d%s%d",&a,type,&b);
    55 
    56             if ( place[a] == place[b] ) continue;   //如果ab在同一堆,不处理
    57 
    58             if ( oper[0] == 'm' ) init_place( a );  //如果是move先把a上面的还原
    59 
    60             if ( type[1] == 'n' ) init_place( b );  //如果是onto先把b上面的还原
    61 
    62             pile_a_to_b( a, b );    //把A堆放在B堆上
    63         }
    64 
    65         for ( int i = 0 ; i < n ; ++ i ) {
    66             printf("%d:",i);
    67             int now = 0;
    68             while ( now <= top[i] )
    69                 printf(" %d",stack[i][now ++]);
    70             printf("
    ");
    71         }
    72     }
    73     return 0;
    74 }

    被这题伤的不浅,==|

  • 相关阅读:
    价值理论的出发点和落脚点都是人--以人为本
    价值理论是人类决策和行为的标尺
    事实判断和价值判断
    什么是价值理论?---人们认识世界和改造世界的过程可分解为四个基本阶段
    大人只看利弊 小孩才分对错
    为人处世、事实判断和价值判断皆不可少--人类认识客观事物的标尺:对错与利弊
    知行之间--价值与真理--行动的标尺
    事实判断与价值判断之间的桥梁就是人的需要
    10分钟梳理MySQL核心知识点
    postman设置环境变量,实现一套接口根据选择的环境去请求不同的url
  • 原文地址:https://www.cnblogs.com/aze-003/p/5155491.html
Copyright © 2011-2022 走看看