UVaOJ 101——The Blocks Problem

题目的意思是要对一些方块进行操作(就这样认为吧)

有n个方块,需要搬运这些东西

输入move a onto b就是要把在a跟b上面的方块放回最初始的位置然后把a放到b上面

输入move a over b就是要把在a上面的方块放回最初始的位置然后把a放到包含b的那一摞上面

输入pile a onto b就是要把b上面的方块放回最初的位置然后把a跟它上面的方块一起搬到b上

输入pile a over b就是要把a跟它上面的方块一起搬到包含b的那一摞上面

题目思想很简单,一点算法都没有,就是特别麻烦。

#include<iostream>
using namespace std;

int blocksworld[26][26];
int stack[26];//stack为1表示这里只有1个元素并且就是本身,为0表示没有元素,大于1表示元素的具体个数 
int n;

int findelem(int a){
	if(blocksworld[a][0]==a)return a;
	for(int i=0;i<n;++i){
		for(int t=0;t<stack[i];++t){
			if(blocksworld[i][t]==a){
		 		return i;}
		}
	}
}

int moveonto(int a,int b){//把a跟b上所有的块移回原位后把a移到b上去 
	int posa,posb;
	posa=findelem(a);
	posb=findelem(b);
	if(posa==posb)return 1;
	int t;
	int i;
	//把a上面的东西都移回去 
	for(i=0;i<stack[posa];++i){
		if(blocksworld[posa][i]==a)break;
	}//找到a具体的位置 
	int ia=i;//ia保存a的具体位置 
	if(i+1<stack[posa]){
		for(t=i+1;t<stack[posa];++t){
			int tmp=blocksworld[posa][t];
			blocksworld[posa][t]=-1;
			blocksworld[tmp][0]=tmp;
			stack[tmp]=1;
		}
		stack[posa]=i+1;
	}//移完了 
	//把b上面的东西都移回去
 	for(i=0;i<stack[posb];++i){
		if(blocksworld[posb][i]==b)break;
	}//找到b具体的位置 
	if(i+1<stack[posb]){
		for(t=i+1;t<stack[posb];++t){
			int tmp=blocksworld[posb][t];
			blocksworld[posb][t]=-1;
			blocksworld[tmp][0]=tmp;
			stack[tmp]=1;
		}
		stack[posb]=i+1;
	}//移完了
	stack[posb]++;
	blocksworld[posa][ia]=-1;
	blocksworld[posb][stack[posb]-1]=a; 
	stack[posa]--;
	return 1;
}

int moveover(int a,int b){//把a上面的东西移回原位后把a移到包含b的块上去 
	int posa,posb;
	posa=findelem(a);
	posb=findelem(b);
	if(posa==posb)return 1;
	int i,t;
	//把a上面的东西都移回去 
	for(i=0;i<stack[posa];++i){
		if(blocksworld[posa][i]==a)break;
	}//找到a具体的位置 
	if(i+1<stack[posa]){
		for(t=i+1;t<stack[posa];++t){
			int tmp=blocksworld[posa][t];
			blocksworld[posa][t]=-1;
			blocksworld[tmp][0]=tmp;
			stack[tmp]=1;
		}
		stack[posa]=i+1;
	}//移完了
	stack[posb]++;
	blocksworld[posa][i]=-1;
	blocksworld[posb][stack[posb]-1]=a;
	stack[posa]--;
	return 1;
}

int pileonto(int a,int b){//把a以及a上面的东西搬到b上面去,b上面的块回到初始位置,a上面的块保持顺序 
	int posa,posb;
	posa=findelem(a);
	posb=findelem(b);
	if(posa==posb)return 1;
	int i,t;
	//把b上面的东西都移回去
 	for(i=0;i<stack[posb];++i){
		if(blocksworld[posb][i]==b)break;
	}//找到b具体的位置 
	if(i+1<stack[posb]){
		for(t=i+1;t<stack[posb];++t){
			int tmp=blocksworld[posb][t];
			blocksworld[posb][t]=-1;
			blocksworld[tmp][0]=tmp;
			stack[tmp]=1;
		}
		stack[posb]=i+1;
	}//移完了
	int bnum=stack[posb];
	for(i=0;i<stack[posa];++i){
		if(blocksworld[posa][i]==a)break;
	}//找到a具体的位置 
	for(t=i;t<stack[posa];++t){
		blocksworld[posb][bnum]=blocksworld[posa][t];
		blocksworld[posa][t]=-1;
		bnum++;
	}
	stack[posb]=bnum;
	stack[posa]=i;
	return 1;
}

int pileover(int a,int b){//把a以及a上面的块版移到包含b的东西上面 
	int posa,posb;
	posa=findelem(a);
	posb=findelem(b);
	if(posa==posb){
		return 1;}
	int i,t;
	int bnum=stack[posb];
	for(i=0;i<stack[posa];++i){
		if(blocksworld[posa][i]==a)break;
	}//找到a具体的位置 
	for(t=i;t<stack[posa];++t){
		blocksworld[posb][bnum]=blocksworld[posa][t];
		blocksworld[posa][t]=-1;
		bnum++;
	}
	stack[posb]=bnum;
	stack[posa]=i;
	return 1;
}



int main(){
	cin>>n;
	for(int i=0;i<=n;++i){stack[i]=1;blocksworld[i][0]=i;}
	while(1){
		int sum=0;
		string instruction;
		cin>>instruction;
		if(instruction=="move")sum+=0;
		else if(instruction=="pile")sum+=2;
		else break;
		int a,b;
		cin>>a;
		string instruction2;
		cin>>instruction2;
		if(instruction2=="onto")sum+=1;
		else sum+=2;
		cin>>b;
		if(sum==1)moveonto(a,b);
		else if(sum==2)moveover(a,b);
		else if(sum==3)pileonto(a,b);
		else pileover(a,b);
       
	}
	for(int i=0;i<n;++i){
		cout<<i<<':';
		for(int t=0;t<stack[i];++t){
			cout<<' '<<blocksworld[i][t];
		}
		cout<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值