UVa 101 The Blocks Problem(vector)

19781 ワード

個人ブログvecorは不定長の配列です.「パッケージ」はvector内で多くの一般的な操作を行います.例えば、a.size()読み出しサイズ、a.resize()変更サイズ、a.push_back()は末尾に要素を追加し、a.pop_back()最後の要素を削除し、a.clear()を空にし、a.empty()を空にするかどうかをテストします.
vectorはテンプレートクラスであり、vectoraまたはvectorbのような宣言方式でvectorを宣言する必要がある.vectorはint a[]に類似した整数配列であり、vectorはstring a[]に類似した文字列配列である.
vector詳細命令:http://hongxuelin.com/code/acm/zisu/83.html
個人ブログテーマ:
move a onto b
where a and b are block numbers, puts block a onto block b after returning any blocks that are stacked on top of blocks a and b to their initial positions.
aとbは積み木の番号で、まずaとbの上のすべての積み木を元の場所に戻して、それからaをbの上に置きます.

move a over b
where a and b are block numbers, puts block a onto the top of the stack containing block b, after returning any blocks that are stacked on top of block a to their initial positions.
aとbはすべて積み木の番号で、まずaの上のすべての積み木を元の場所に戻して、それからaをbの上に置きます.(b上に既存の積み木が動かない)
pile a onto b
where a and b are block numbers, moves the pile of blocks consisting of block a, and any blocks that are stacked above block a, onto block b. All blocks on top of block b are moved to their initial positions prior to the pile taking place. The blocks stacked above block a retain their order when moved.
aとbはいずれも積み木の番号であり,aとその上のすべての積極的な構成の積み重ね全体をbに移動する.移動する前にbの上のすべての積極性を元の場所に戻します.移動する積み木は元の順序を維持しなければならない.

pile a over b
where a and b are block numbers, puts the pile of blocks consisting of block a, and any blocks that are stacked above block a, onto the top of the stack containing block b. The blocks stacked above block a retain their original order when moved.
aとbはいずれも積み木の番号であり、aとその上のすべての積極的な構成の積み木全体をbがある積み木の一番上の積み木に移動する.移動する積み木は元の順序を維持しなければならない.

quit
terminates manipulations in the block world.
積み木の世界の操縦を終わらせる.

個人ブログコードはojでSTL:vectorを学ぶだけではありません.
文章は洪学林の個人ブログから転載した.http://www.hongxuelin.com/
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64

           
           
           
           
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 30;
int n;
vector<int>pile[maxn];
void find_block(int a,int &p,int &h)
{
for(p=0;p<n;p++)
{
for(h=0;h<pile[p].size();h++)
{
if(a==pile[p][h]) return ;//
}
}
}

void clear_above(int p,int h)// p h
{
for(int i=h+1;i<pile[p].size();i++)
{
int b=pile[p][i];
pile[b].push_back(b);//
}
pile[p].resize(h+1);// , 0-h;
}

void pile_onto(int p,int h,int p2)// p h p2
{
for(int i=h;i<pile[p].size();i++)
{
pile[p2].push_back(pile[p][i]);
}
pile[p].resize(h);
}
int main()
{
int a,b;
while(cin>>n)
{
string s1,s2;
for(int i=0;i<n;i++) pile[i].push_back(i);//
while(cin>>s1>>a>>s2>>b)
{
int pa,pb,ha,hb;
find_block(a,pa,ha);
find_block(b,pb,hb);
if(pa==pb) continue;// ,
if(s2=="onto") clear_above(pb,hb);
if(s1=="move") clear_above(pa,ha);
pile_onto(pa,ha,pb);
}
//output
for(int i=0;i<n;i++)
{
printf("%d:",i);
for(int j=0;j<pile[i].size();j++) printf(" %d",pile[i][j]);
cout<<endl;
}
}
return 0;
}