Blocks Problem (Uva 101)

Excerpts from the original question

Input n, get the blocks numbered 0~n-1, and place them in the position of 0~n-1 in order. These blocks are now operated in four ways.

1. move a onto b: Put the blocks on block a and B back to their original positions, and then put a on block B.

2. move a over b: Put the blocks on a back to their original positions, and then send a to the pile containing B.

3. pile a onto b: put the blocks on B back to their original positions, and then move the blocks on a and a to B.

4. pile a over b: move a together with the blocks on a to the pile containing B.

When quit is input, the operation is terminated and the block condition at 0~n-1 position is output.

For input and output, see the end of the article.

Title Analysis

The height of each block pile is uncertain, so it's appropriate to use vector to save it; and the number of each block pile is not more than n, so an array can be used to store it.

Vector < int > pile [maxn]; // an array of int vectors

Analysis of the four operations in the original topic:

  Homing Moving top
move a homing All -
onto b homing Both are -
move and onto A and B are all homing. a to b-
pile/over nothing discharge

Complete code (detailed comments)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <assert.h>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>

using namespace std;
const int maxn = 30;
int n;
vector<int> pile[maxn];
 
//Find the pile and height of block a and return it to the caller as a reference
void find_block(int a, int& p, int& h)//When there is an element, the height is 0, that is h=0.
{
	for (p = 0; p < n;p++)
	for (h = 0; h < pile[p].size(); h++)
	{
		if (pile[p][h] == a)return;
	}
 
}
//Remove all the blocks above the block with the height of pile p h to their original position
void clear_above(int p, int h)
{
	for (int i = h + 1; i < pile[p].size(); i++)
	{
		int m = pile[p][i];
		pile[m].push_back(m);
	}
	pile[p].resize(h + 1);
 
}
//Move the whole block of wood above the height of pile p to the top of pile P 2.
void pile_onto(int p, int h,int p2)
{
	for (int i = h; i < pile[p].size(); i++)
		pile[p2].push_back(pile[p][i]);
	pile[p].resize(h);
 
}
 
void print()
{
	for (int i = 0; i < n; i++)
	{
		printf("%d:", i);
		for (int j = 0; j < pile[i].size(); j++)
			printf(" %d", pile[i][j]);
		printf("\n");
 
	}
}
 
 
int main()
{
	int a, b;
	cin >> n;
	string s1, s2;
	for (int i = 0; i < n; i++)pile[i].push_back(i);
	while (cin >> s1 >> a >> s2 >> b)//cin >> s1&&s1 != "quit"&&cin >> a >> s2 >> b
	{
		int pa, pb, ha, hb;
		find_block(a, pa, ha);//Find the location of a: pa refers to the number of vector-1,ha refers to the length-1 of the vector.
		find_block(b, pb, hb);
		if (pa == pb) continue;//Illegal instruction
		if (s2 == "onto") clear_above(pb, hb);//b homing
		if (s1 == "move") clear_above(pa, ha);//a homing
		pile_onto(pa, ha, pb);
 
	}
	print();
	
	return 0;
}

Attached if the location is not clear, you can just use find_block() function output to see:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <assert.h>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 30;
int n;
vector<int> pile[maxn]; 
//Find the pile and height of block a and return it to the caller as a reference
void find_block(int a, int& p, int& h)
{
	for (p = 0; p < n;p++)
	for (h = 0; h < pile[p].size(); h++)
	{
		if (pile[p][h] == a)return;
	}
 
} 
int main()
{
	int a, b;
	cin >> n;
	for (int i = 0; i < n; i++)pile[i].push_back(i);
	cin >> a >>  b;
	int pa, pb, ha, hb;
	find_block(a, pa, ha);
	find_block(b, pb, hb);	
	cout<<"pa:"<<pa<<" "<<"ha:"<<ha<<endl;
	cout<<"pb:"<<pb<<" "<<"hb:"<<hb<<endl;
	return 0;
}
//https://www.cnblogs.com/QG-whz/

 

Posted by mosi on Tue, 08 Oct 2019 02:31:35 -0700