#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int stk[30][30], len[30], n;
int main()
{
while (cin >> n)
{
for(int i = 0; i < n; i++)
{
stk[i][0] = i;
len[i] = 1;
}
char s1[10], s2[10];
int num1, num2;
while(scanf("%s", s1) && strcmp(s1, "quit")!=0)
{
scanf("%d%s%d", &num1, s2, &num2);
if(num1 == num2)
continue;
int num1_i, num1_j, num2_i, num2_j;
for(int i = 0; i < n; i++)
for(int j = 0; j < len[i]; j++)
{
if(num1 == stk[i][j])
{
num1_i=i;
num1_j=j;
}
else if(num2 ==stk[i][j])
{
num2_i=i;
num2_j=j;
}
}
if(num1_i == num2_i)
continue;
if(strcmp(s1,"move") == 0)
{
if(strcmp(s2,"onto") == 0)
{
for(int i = num1_j + 1; i < len[num1_i]; i++)
len[stk[num1_i][i]] = 1;
for(int i = num2_j + 1; i< len[num2_i]; i++)
len[stk[num2_i][i]] = 1;
len[num1_i] = num1_j;
stk[num2_i][num2_j + 1] = stk[num1_i][num1_j];
len[num2_i] = num2_j + 2;
}
else
{
for(int i = num1_j + 1; i < len[num1_i]; i++)
len[stk[num1_i][i]] = 1;
stk[num2_i][len[num2_i]++] = stk[num1_i][num1_j];
len[num1_i] = num1_j;
}
}
else
{
if(strcmp(s2,"onto") == 0)
{
for(int i = num2_j + 1; i < len[num2_i]; i++)
len[stk[num2_i][i]] = 1;
len[num2_i] = num2_j + 1;
for(int i = num1_j; i < len[num1_i]; i++)
stk[num2_i][len[num2_i]++] = stk[num1_i][i];
len[num1_i] = num1_j;
}
else
{
for(int i = num1_j; i < len[num1_i]; i++)
stk[num2_i][len[num2_i]++] = stk[num1_i][i];
len[num1_i] = num1_j;
}
}
}
for(int i = 0; i < n; i++)
{
printf("%d:", i);
for(int j = 0; j < len[i]; j++)
printf(" %d", stk[i][j]);
cout << endl;
}
}
return 0;
}
uva 101 - The Blocks Problem
最新推荐文章于 2018-02-09 20:23:00 发布