# UVa Problem 101 - The Blocks Problem

// UVa Problem 101 - The Blocks Problem
// Verdict: Accepted
// Submission Date: 2011-10-16
// UVa Run Time: 0.020s
//
// 版权所有（C）2011，邱秋。metaphysis # yeah dot net
//
// [解题方法]
// 模拟题，注意题意的操作中将方块返回原位置是指将方块 i 放到第 i 组方块最上方。

#include <iostream>
#include <cstring>
#include <set>

using namespace std;

#define MAXN 25

int blocks[MAXN];
int state[MAXN][MAXN];
int nBlocks;

void returning(int x, pair < int, int > xIndex)
{
for (int i = xIndex.second + 1; i < blocks[xIndex.first]; i++)
{
int tmp = state[xIndex.first][i];
state[tmp][blocks[tmp]++] = tmp;
}

blocks[xIndex.first] = xIndex.second + 1;
}

void moveOver(int a, int b, pair < int, int > aIndex, pair < int, int > bIndex)
{
returning(a, aIndex);

state[bIndex.first][blocks[bIndex.first]++] = a;
blocks[aIndex.first] = aIndex.second;
}

void moveOnto(int a, int b, pair < int, int > aIndex, pair < int, int > bIndex)
{
returning(b, bIndex);
moveOver(a, b, aIndex, bIndex);
}

void pileOver(int a, int b, pair < int, int > aIndex, pair < int, int > bIndex)
{
int current = aIndex.second;
while (current < blocks[aIndex.first])
state[bIndex.first][blocks[bIndex.first]++] =
state[aIndex.first][current++];

blocks[aIndex.first] = aIndex.second;
}

void pileOnto(int a, int b, pair < int, int > aIndex, pair < int, int > bIndex)
{
returning(b, bIndex);
pileOver(a, b, aIndex, bIndex);
}

pair < int, int > findIndex(int x)
{
for (int i = 0; i < nBlocks; i++)
for (int j = 0; j < blocks[i]; j++)
if (state[i][j] == x)
return make_pair(i, j);
}

void print(void)
{
for (int i = 0; i < nBlocks; i++)
{
cout << i << ":";
for (int j = 0; j < blocks[i]; j++)
cout << " " << state[i][j];
cout << endl;
}
}

int main(int ac, char *av[])
{
int a, b, aIndex, bIndex;
string command, action;

cin >> nBlocks;
for (int i = 0; i < nBlocks; i++)
{
blocks[i] = 0;
state[i][blocks[i]++] = i;
}

while (cin >> command, strcmp(command.data(), "quit"))
{
cin >> a >> action >> b;

if (a == b)
continue;

pair < int, int > aIndex = findIndex(a);
pair < int, int > bIndex = findIndex(b);
if (aIndex.first == bIndex.first)
continue;

if (strcmp(command.data(), "move") == 0)
{
if (strcmp(action.data(), "onto") == 0)
moveOnto(a, b, aIndex, bIndex);
else
moveOver(a, b, aIndex, bIndex);

}
else if (strcmp(action.data(), "onto") == 0)
pileOnto(a, b, aIndex, bIndex);
else
pileOver(a, b, aIndex, bIndex);
}

print();

return 0;
}

#### UVA101The Blocks Problem(小模拟＋STL）

2016-07-20 20:49:39

#### UVA101 The Blocks Problem

2014-07-21 14:42:10

#### 《算法竞赛入门经典2ndEdition 》例题5-2 木块问题(The Blocks Problem, Uva101)

2015-12-25 21:51:55

#### 例题5-2 UVA 101 The Blocks Problems 木块问题

2015-12-15 15:51:39

#### uva 101 木块问题 The Blocks Problem

2016-10-17 21:27:56

#### 例题5-2木块问题（The Blocks Problem,UVa 101）

2018-05-16 13:01:15

#### Uva 101 the block problem 木块问题（算法竞赛经典入门）STL vector

2015-05-06 17:04:49

#### uva 101 The Blocks Problem

2013-10-01 15:35:19

#### Uva 101 The Blocks Problem

2017-07-05 11:05:57

#### The Blocks Problem ——Uva 101

2018-03-08 11:59:56