using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace test
{
class test
{
static void Main(){
int stack_n = int.Parse(Console.ReadLine());
Stack<int>[] intStack = new Stack<int>[stack_n];
int[] Block = new int[stack_n];
for (int i = 0; i < stack_n; i++)
{
Block[i] = i;
intStack[i] = new Stack<int>();
intStack[i].Push(Block[i]);
}
while (true)
{
string rl = Console.ReadLine();
string[] parts = rl.Split(' ');
if (parts.Length == 4)
{
int a = int.Parse(parts[1]);
int b = int.Parse(parts[3]);
if (parts[0] == "move" && parts[2] == "onto")
{
while (intStack[Block[a]].Peek() != a)
{
int tmp = intStack[Block[a]].Pop();
intStack[tmp].Push(tmp);
Block[tmp] = tmp;
}
while (intStack[Block[b]].Peek() != b)
{
int tmp = intStack[Block[b]].Pop();
intStack[tmp].Push(tmp);
Block[tmp] = tmp;
}
int tmp2 = intStack[Block[a]].Pop();
intStack[Block[b]].Push(tmp2);
Block[tmp2] = Block[b];
}
else if (parts[0] == "move" && parts[2] == "over")
{
while (intStack[Block[a]].Peek() != a)
{
int tmp = intStack[Block[a]].Pop();
intStack[tmp].Push(tmp);
Block[tmp] = Block[b];
}
int tmp2 = intStack[Block[a]].Pop();
intStack[Block[b]].Push(tmp2);
Console.WriteLine("moveover " + tmp2+" "+a +" "+b);
Block[tmp2] = Block[b];
}
else if (parts[0] == "pile" && parts[2] == "onto")
{
Stack<int> tmpstack = new Stack<int>();
while (intStack[Block[a]].Peek() != a)
{
int tmp = intStack[Block[a]].Pop();
tmpstack.Push(tmp);
}
tmpstack.Push(intStack[Block[a]].Pop());
while (intStack[Block[b]].Peek() != b)
{
int tmp = intStack[Block[b]].Pop();
intStack[tmp].Push(tmp);
Block[tmp] = tmp;
}
while (tmpstack.Count > 0)
{
int tmp = tmpstack.Pop();
intStack[Block[b]].Push(tmp);
Block[tmp] = Block[b];
}
}
else if (parts[0] == "pile" && parts[2] == "over")
{
Stack<int> tmpstack = new Stack<int>();
while (intStack[Block[a]].Peek() != a)
{
int tmp = intStack[Block[a]].Pop();
tmpstack.Push(tmp);
}
tmpstack.Push(intStack[Block[a]].Pop());
while (tmpstack.Count > 0)
{
int tmp = tmpstack.Pop();
intStack[Block[b]].Push(tmp);
Block[tmp] = Block[b];
}
}
}
if(parts[0]=="quit")
{
break;
}
}
for (int i = 0; i < stack_n; i++)
{
Console.Write(i + " :");
foreach (int j in intStack[i])
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}
2009年6月1日 星期一
ACM Q101 - C#
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言