Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594

POJ 1208 The Blocks Problem---模拟

Posted on 2010-08-12 12:16 Uriel 阅读(491) 评论(0)  编辑 收藏 引用 所属分类: POJ模拟
        集训结束了,最后一天想切道水题休息休息,结果被虐一晚上。。。
        看了Discuss才知道题目都理解错了。。晚上回宿舍改改,早上终于AC了。。

//Problem: 1208  User: Uriel 
//Memory: 364K  Time: 0MS 
//Language: G++  Result: Accepted 
//Simulation
//2010.08.12
#include<stdio.h>
#include
<stdlib.h>
#include
<string.h>

int n,pile[30][30],np[30];

int main(){
    
int i,j,k,n1,n2,fi,fj;
    
char s1[6],s2[6];
    scanf(
"%d",&n);
    
for(i=0;i<n;i++){
        pile[i][
0]=i;
        np[i]
=1;
    }

    
while(1){
        scanf(
"%s",s1);
        
if(strcmp(s1,"quit")==0)break;
        scanf(
"%d %s %d",&n1,s2,&n2);
        
if(strcmp(s1,"move")==0){//-----------------------------move
            if(strcmp(s2,"onto")==0){
                
for(i=0;i<n;i++){
                    
for(j=0;j<np[i];j++){
                        
if(pile[i][j]==n1){
                            fi
=i;fj=j;goto f1;
                        }

                    }

                }

f1:                
for(i=0;i<n;i++){
                    
for(j=0;j<np[i];j++){
                        
if(pile[i][j]==n2)goto sov1;
                    }

                }

sov1:            
if(fi==i)continue;
                
for(k=j+1;k<np[i];k++){
                    pile[pile[i][k]][
0]=pile[i][k];
                    np[pile[i][k]]
=1;
                }

                
for(k=fj+1;k<np[fi];k++){
                    pile[pile[fi][k]][
0]=pile[fi][k];
                    np[pile[fi][k]]
=1;
                }

                np[i]
=j+2;
                np[fi]
=fj;
                pile[i][j
+1]=n1;
            }

            
else{
                
for(i=0;i<n;i++){
                    
for(j=0;j<np[i];j++){
                        
if(pile[i][j]==n1){
                            fi
=i;fj=j;goto f2;
                        }

                    }

                }

f2:                
for(i=0;i<n;i++){
                    
for(j=0;j<np[i];j++){
                        
if(pile[i][j]==n2)goto sov2;
                    }

                }

sov2:            
if(fi==i)continue;
                
for(k=fj+1;k<np[fi];k++){
                    pile[pile[fi][k]][
0]=pile[fi][k];
                    np[pile[fi][k]]
=1;
                }

                np[fi]
=fj;
                pile[i][np[i]]
=n1;
                np[i]
++;
            }

        }

        
else{//-------------------------------------------------pile
            if(strcmp(s2,"onto")==0){
                
for(i=0;i<n;i++){
                    
for(j=0;j<np[i];j++){
                        
if(pile[i][j]==n1){
                            fi
=i;fj=j;goto f3;
                        }

                    }

                }

f3:                
for(i=0;i<n;i++){
                    
for(j=0;j<np[i];j++){
                        
if(pile[i][j]==n2)goto sov3;
                    }

                }

sov3:            
if(fi==i)continue;
                
for(k=j+1;k<np[i];k++){
                    pile[pile[i][k]][
0]=pile[i][k];
                    np[pile[i][k]]
=1;
                }

                
for(k=1;k<=np[fi]-fj;k++)
                    pile[i][k
+j]=pile[fi][fj+k-1];
                np[i]
=j+1;
                np[i]
+=np[fi]-fj;
                np[fi]
=fj;
            }

            
else{
                
for(i=0;i<n;i++){
                    
for(j=0;j<np[i];j++){
                        
if(pile[i][j]==n1){
                            fi
=i;fj=j;goto f4;
                        }

                    }

                }

f4:                
for(i=0;i<n;i++){
                    
for(j=0;j<np[i];j++){
                        
if(pile[i][j]==n2)goto sov4;
                    }

                }

sov4:            
if(fi==i)continue;
                
for(k=0;k<np[fi]-fj;k++)
                    pile[i][np[i]
+k]=pile[fi][fj+k];
                np[i]
+=np[fi]-fj;
                np[fi]
-=np[fi]-fj;
            }

        }

    }

    
for(i=0;i<n;i++){
        printf(
"%d:",i);
        
for(j=0;j<np[i];j++)printf(" %d",pile[i][j]);
        puts(
"");
    }

    
return 0;
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理