The Blocks Problem UVA 101 木块问题
- #include
- #include
- #include
- #include
- using namespace std;
- const int maxn=30;
- int n;
- vector
p[maxn]; void query(int &a1,int &h1,int a){ //用于找到输入值a,b所在的位置,并通过引用返回
- for(a1=0;a1<n;a1++){
- for(h1=0;h1<p[a1].size();h1++){
- if(a==p[a1][h1])return ;
- }
- }
- }
void clear1(int where,int height){ //清除 指定堆高度为height的木块上方全部移回原位
- for(int i=height+1;i<p[where].size();i++){
- int temp=p[where][i];
- p[temp].push_back(temp);
- }
- p[where].resize(height+1); //此处注意,vector容器与数组相似,最大下标为长度减1;(这里只应保留下标为0~n的元素
- }
void move(int w_a,int ha,int w_b){ //用于移动第a堆到第b堆上
- for(int i=ha;i<p[w_a].size();i++){
- int temp=p[w_a][i];
- p[w_b].push_back(temp);
- }
- p[w_a].resize(ha); //最后第a堆元素因该为n- 1个;
- }
void print1(){
- for(int i=0;i<n;i++){
- printf(“%d:”,i);
- for(int j=0;j<p[i].size();j++){
- printf(“ %d”,p[i][j]);
- }
- printf(“\n”);
- }
- }
- int main(){
- scanf(“%d”,&n);
- for(int i=0;i<=n-1;i++)p[i].push_back(i);
- int a,b;
- string s1,s2;
- while(cin>>s1>>a>>s2>>b){
- if(s1==”quit”)break;
- int wa,wb,ha,hb;
- query(wa,ha,a);
- query(wb,hb,b);
- if(wa==wb)continue;
- if(s1==”move”)clear1(wa,ha);
- if(s2==”onto”)clear1(wb,hb);
- move(wa,ha,wb);
- }
- print1();
- return 0;
- }
还没有评论,来说两句吧...