UVa Problem 101 - The Blocks Problem

  1. // UVa Problem 101 - The Blocks Problem   
  2. // Verdict: Accepted   
  3. // Submission Date: 2011-10-16   
  4. // UVa Run Time: 0.020s   
  5. //   
  6. // 版权所有(C)2011,邱秋。metaphysis # yeah dot net   
  7. //   
  8. // [解题方法]   
  9. // 模拟题,注意题意的操作中将方块返回原位置是指将方块 i 放到第 i 组方块最上方。   
  10.   
  11. #include <iostream>   
  12. #include <cstring>   
  13. #include <set>   
  14.   
  15. using namespace std;  
  16.   
  17. #define MAXN 25   
  18.   
  19. int blocks[MAXN];  
  20. int state[MAXN][MAXN];  
  21. int nBlocks;  
  22.   
  23. void returning(int x, pair < intint > xIndex)  
  24. {  
  25.     for (int i = xIndex.second + 1; i < blocks[xIndex.first]; i++)  
  26.     {  
  27.         int tmp = state[xIndex.first][i];  
  28.         state[tmp][blocks[tmp]++] = tmp;  
  29.     }  
  30.   
  31.     blocks[xIndex.first] = xIndex.second + 1;  
  32. }  
  33.   
  34. void moveOver(int a, int b, pair < intint > aIndex, pair < intint > bIndex)  
  35. {  
  36.     returning(a, aIndex);  
  37.   
  38.     state[bIndex.first][blocks[bIndex.first]++] = a;  
  39.     blocks[aIndex.first] = aIndex.second;  
  40. }  
  41.   
  42. void moveOnto(int a, int b, pair < intint > aIndex, pair < intint > bIndex)  
  43. {  
  44.     returning(b, bIndex);  
  45.     moveOver(a, b, aIndex, bIndex);  
  46. }  
  47.   
  48. void pileOver(int a, int b, pair < intint > aIndex, pair < intint > bIndex)  
  49. {  
  50.     int current = aIndex.second;  
  51.     while (current < blocks[aIndex.first])  
  52.         state[bIndex.first][blocks[bIndex.first]++] =  
  53.             state[aIndex.first][current++];  
  54.   
  55.     blocks[aIndex.first] = aIndex.second;  
  56. }  
  57.   
  58. void pileOnto(int a, int b, pair < intint > aIndex, pair < intint > bIndex)  
  59. {  
  60.     returning(b, bIndex);  
  61.     pileOver(a, b, aIndex, bIndex);  
  62. }  
  63.   
  64. pair < intint > findIndex(int x)  
  65. {  
  66.     for (int i = 0; i < nBlocks; i++)  
  67.         for (int j = 0; j < blocks[i]; j++)  
  68.             if (state[i][j] == x)  
  69.                 return make_pair(i, j);  
  70. }  
  71.   
  72. void print(void)  
  73. {  
  74.     for (int i = 0; i < nBlocks; i++)  
  75.     {  
  76.         cout << i << ":";  
  77.         for (int j = 0; j < blocks[i]; j++)  
  78.             cout << " " << state[i][j];  
  79.         cout << endl;  
  80.     }  
  81. }  
  82.   
  83. int main(int ac, char *av[])  
  84. {  
  85.     int a, b, aIndex, bIndex;  
  86.     string command, action;  
  87.   
  88.     cin >> nBlocks;  
  89.     for (int i = 0; i < nBlocks; i++)  
  90.     {  
  91.         blocks[i] = 0;  
  92.         state[i][blocks[i]++] = i;  
  93.     }  
  94.   
  95.     while (cin >> command, strcmp(command.data(), "quit"))  
  96.     {  
  97.         cin >> a >> action >> b;  
  98.   
  99.         if (a == b)  
  100.             continue;  
  101.   
  102.         pair < intint > aIndex = findIndex(a);  
  103.         pair < intint > bIndex = findIndex(b);  
  104.         if (aIndex.first == bIndex.first)  
  105.             continue;  
  106.   
  107.         if (strcmp(command.data(), "move") == 0)  
  108.         {  
  109.             if (strcmp(action.data(), "onto") == 0)  
  110.                 moveOnto(a, b, aIndex, bIndex);  
  111.             else  
  112.                 moveOver(a, b, aIndex, bIndex);  
  113.   
  114.         }  
  115.         else if (strcmp(action.data(), "onto") == 0)  
  116.             pileOnto(a, b, aIndex, bIndex);  
  117.         else  
  118.             pileOver(a, b, aIndex, bIndex);  
  119.     }  
  120.   
  121.     print();  
  122.   
  123.     return 0;  
  124. }  
// 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值