Beispiele:

Input

Output

Input

Output

11

move 10 over 1

move 4 over 7

move 7 onto 1

move 6 over 4

move 1 over 4

pile 4 over 8

move 6 onto 8

pile 3 onto 6

quit

0:  0

1:  1

2:  2

3:

4:

5:  5

6:

7:  7

8:  8 4 6 3

9:  9

10:  10

8

pile 4 over 1

pile 1 onto 5

move 2 over 3

move 4 onto 1

pile 1 over 3

move 7 onto 6

move 1 onto 6

quit

0:  0

1:

2:

3:  3 2

4:  4

5:  5

6:  6 1

7:  7



1. C++, Doina Logofatu, 2000

/**
* ACM programming Contest WS 08/09
* UVa Status: accepted
* Run Time: 0.020
* @author Doina Logofatu, logofatu@hm.edu
*/

#include <stdio.h>
#include <string.h>
int w[26][26];


int readData(char s1[4], int &n, char s2[4], int &m)
{
scanf("%4s",s1);
if(strcmp(s1,"quit")==0)return 0;
scanf("%d",&n);
scanf("%4s",s2);
scanf("%d",&m);
return 1;
}
// whre's "block" ?
int place(int n, int block)
{
int i,j;
for(i=0; i<n; i++)
for(j=1;j<=w[i][0];j++)
if (w[i][j]==block) return i;
return -1;
}
// move in the initial position all
// blocks
// above block a
int initialPos(int n, int i, int a)
{
int aux;
int j = 1;
while(w[i][j] != a) j++;
for(int k = j+1;k<=w[i][0]; k++)
{
aux=w[i][k];
w[aux][++w[aux][0]]=aux;
}
w[i][0] -= (w[i][0] - j);
return 1;
}
// move the whole pile that beginn with a
// on the blocks with b
int movePile(int n, int b1, int a, int b2, int b)
{
int j=1;
while(w[b1][j]!=a)j++;
for(int k=j; k<=w[b1][0]; k++)
w[b2][++w[b2][0]] = w[b1][k];
w[b1][0] -= (w[b1][0] - j +1);
return 1;
}

int moveOnto(int n, int a, int b)
{
int b1 = place(n,a);
int b2 = place(n,b);
if( a != b && b1 != b2)
{
initialPos(n,b1,a);
initialPos(n,b2,b);
w[b1][0]--;
w[b2][++w[b2][0]]=a;
}
return 1;
}

int moveOver(int n, int a, int b)
{
int b1 = place(n, a);
int b2 = place(n, b);
if( a != b && b1 != b2)
{
initialPos(n, b1, a);
w[b1][0]--;
w[b2][++w[b2][0]]=a;
}
return 1;
}

int pileOnto(int n, int a, int b)
{
int b1 = place(n, a);
int b2 = place(n, b);
if(a!=b && b1!=b2)
{
initialPos(n, b2, b);
movePile(n,b1,a,b2,b);
}
return 1;
}

int pileOver(int n, int a, int b)
{
int b1 = place(n, a);
int b2 = place(n, b);
if(a!=b && b1!=b2)
movePile(n,b1,a,b2,b);
return 1;
}

int write(int n)
{
int i, j;
for(i=0; i<n; i++)
{
printf("%d:", i);
for(j=1; j<=w[i][0]; j++)
printf(" %d", w[i][j]);
printf("\n");
}
return 1;
}

int main()
{
int n;
char s1[30], s2[30];
int a,b;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
w[i][0]=1;
w[i][1]=i;
}


while(readData(s1, a, s2, b))
{
if(strcmp(s1,"move")==0)
if(strcmp(s2,"onto")==0) moveOnto(n,a,b);
else moveOver(n,a,b);
else
if(strcmp(s2,"onto")==0) pileOnto(n,a,b);
else pileOver(n,a,b);
}
write(n);
return 0;
}

2. JAVA, Till Fischer



/*

 ============================================================================

 Name            : Main.java

 Author            : Till Fischer

 Description    : 101 - The Blocks Problem

 Accepted        : Timelimit Exceeded

 Time            : 3.000

 ============================================================================

 */



import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;



public class Main {



    public static void main(String[] args) throws IOException {



            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

            int numBlocks = Integer.parseInt(reader.readLine());

            int[][] blox; /* blockId,{current row, before, after} */

            int tmp, after, a, b;



            blox = new int[numBlocks][3];

            for(int i = 0; i < numBlocks; i++) {

                blox[i] = new int[] { i, -1, -1 };

            }

           

           

            while (true) {

                String line = reader.readLine();

                if (line.equals("quit"))

                    break;



                String[] params = line.split(" ");

                a = Integer.parseInt(params[1]);

                b = Integer.parseInt(params[3]);

               

                if(a == b || blox[a][0] == blox[b][0]){

                    continue;

                }



                if (params[0].equals("move")) {

                    if (params[2].equals("over")) {

                        //return all above a

                        after = blox[a][2];

                        blox[a][2] = -1;   

                        while(after != -1) {

                            tmp = blox[after][2];

                            blox[after][0] = after;

                            blox[after][1] = -1;

                            blox[after][2] = -1;

                            after = tmp;

                        }

                       

                        //get topmost

                        while(blox[b][2] != -1) {

                            b = blox[b][2];

                        }

                       

                        //disconnext a

                        if(blox[a][1] != -1) {

                            blox[blox[a][1]][2] = -1;

                        }

                       

                        blox[a][0] = blox[b][0]; //set as row to bs

                        blox[a][1] = b; //set b before a

                        blox[b][2] = a; //set a after b

                    } else if (params[2].equals("onto")) {

                        //return all above a

                        after = blox[a][2];

                        blox[a][2] = -1;   

                        while(after != -1) {

                            tmp = blox[after][2];

                            blox[after][0] = after;

                            blox[after][1] = -1;

                            blox[after][2] = -1;

                            after = tmp;

                        }

                       

                        //return all above b

                        after = blox[b][2];

                        blox[b][2] = -1;

                        while(after != -1) {

                            tmp = blox[after][2];

                            blox[after][0] = after;

                            blox[after][1] = -1;

                            blox[after][2] = -1;

                            after = tmp;

                        }

                       

                        blox[a][0] = blox[b][0]; //set as row to bs

                        blox[a][1] = b;

                        blox[b][2] = a;

                    }

                } else if (params[0].equals("pile")) {

                    if (params[2].equals("over")) {



                        //get topmost

                        while(blox[b][2] != -1) {

                            b = blox[b][2];

                        }



                        //disconnext a

                        if(blox[a][1] != -1) {

                            blox[blox[a][1]][2] = -1;

                        }

                       

                        blox[a][1] = b; //set b before a

                        blox[b][2] = a; //set a after b

                       

                        //change currentRow for all after a and a

                        blox[a][0] = blox[b][0];

                        tmp = blox[a][2];

                        while(tmp != -1) {

                            blox[tmp][0] = blox[a][0];

                            tmp = blox[tmp][2];

                        }

                    } else if (params[2].equals("onto")) {



                        //return all above b

                        after = blox[b][2];

                        blox[b][2] = -1;

                        while(after != -1) {

                            tmp = blox[after][2];

                            blox[after][0] = after;

                            blox[after][1] = -1;

                            blox[after][2] = -1;

                            after = tmp;

                        }



                        //disconnext a

                        if(blox[a][1] != -1) {

                            blox[blox[a][1]][2] = -1;

                        }

                       

                        blox[a][1] = b; //set b before a

                        blox[b][2] = a; //set a after b

                       

                        //change currentRow for all after a and a

                        blox[a][0] = blox[b][0];

                        tmp = blox[a][2];

                        while(tmp != -1) {

                            blox[tmp][0] = blox[a][0];

                            tmp = blox[tmp][2];

                        }

                    }

                }

            }

           

            //sysout is heavy but only once so nvm

            StringBuilder sb = new StringBuilder();

            for(int i = 0; i < numBlocks; i++) {

                sb.append(i); sb.append(":");

                //now get block that normally is here

                if(blox[i][0] != i) { //he's not here ;)
                    sb.append("\n");

                } else { //yep, there he is

                    //now we gotta print him, and all that are after him

                    sb.append(" "); sb.append(i);

                    tmp = blox[i][2];

                    while(tmp != -1) {

                        sb.append(" "); sb.append(tmp);

                        tmp = blox[tmp][2];

                    }

                    sb.append("\n");

                }

            }

            sb.deleteCharAt(sb.length() -1);

            System.out.print(sb.toString());

    }

}