当前位置: 代码迷 >> 综合 >> 移动盒子(Boxes in a Line,UVa 12657)
  详细解决方案

移动盒子(Boxes in a Line,UVa 12657)

热度:86   发布时间:2024-01-29 04:29:42.0

想了半天没想明白哪里出错了.早起再改吧。

#include<CStdio>
const int maxn = 10001;
int Right[maxn],Left[maxn];
int main(){int T=10,n,m,Case=1;bool j=false;while(scanf("%d%d",&n,&m)==2 && T--){for(int i=1;i<=n;i++){//将1-n个盒子两端连接 Left[i]=i-1;Right[i]=i+1;}Right[0]=1;Left[n+1]=n;for(int i=0;i<m;i++){int opernum,x,y;scanf("%d",&opernum);if(opernum!=4){scanf("%d%d",&x,&y);if(opernum==1){Right[Left[x]]=Right[x];Left[Right[x]]=Left[x];Left[x]=Left[y];Right[x]=y;//设置X的左右 Right[Left[y]]=x;//设置y-1 Left[y]=x;//设置Y的左 }if(opernum==2){Right[Left[x]]=Right[x];Left[Right[x]]=Left[x];Left[x]=y;Right[x]=Right[y];//设置X的左右 Left[Right[y]]=x;Right[y]=x;//设置Y的左右 }if(opernum==3){int templ,tempr;templ=Left[y];tempr=Right[y];Right[Left[x]]=y;Left[y]=Left[x];Right[y]=Right[x];Left[Right[x]]=y;                                   Right[templ]=x;Left[x]=templ;Right[x]=tempr;Left[tempr]=x;}else j=!j;}}//for(int k=Right[0];k!=n+1;k=Right[k]) printf("%d ",k);int i,sig=0,sum=0;if(!j){i=Right[0];while(Right[i]!=n+1){if((++sig)%2)sum+=i;i=Right[i];}}else{i=Left[n+1];while(Left[i]!=0){if((++sig)%2)sum+=i;i=Left[i];}}printf("Case %d: %d\n",Case++,sum);}
}
  相关解决方案