uva 101

原题

一道比较繁琐的模拟题, 非常能练习STL的

一开始WA了好多次, 原因是看漏了题目描述最后一句话, 对于不合法的命令要自己进行忽略处理.....

以后看题得耐心, 看完才下手

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring> 
#include <vector>
#include <set>
#include <queue>
#include <map>
using namespace std;
const int MAXN = 30;
typedef long long LL;

/*
uva 101
 
*/
char cmd[MAXN],str1[MAXN],str2[MAXN];
vector<int> blks[MAXN];
int pos[MAXN],n,from,to;

int main(){
//	freopen("input2.txt","r",stdin);
	scanf("%d",&n);
	for(int i=0; i<n; i++){
		pos[i] = i;
		blks[i].push_back(i);
	}
	while( scanf("%s",cmd) && strcmp(cmd,"quit")!=0 ){
		strcpy(str1,cmd);
		scanf("%d %s %d",&from,str2,&to);
		if( from==to || pos[from]==pos[to] ) continue;
		if( strcmp(str1,"move")==0 ){
			int t = pos[from];			// from所在的木块堆编号 
//			cout << blks[t].back() << endl;
			while( blks[t].back()!=from ){
				int top = blks[t].back();
				pos[top] = top;
				blks[top].push_back(top);
				blks[t].pop_back();
			}
			if( strcmp(str2,"onto")==0 ){
				t = pos[to];		 
				while( blks[t].back()!=to ){
					int top = blks[t].back();
					pos[top] = top;
					blks[top].push_back(top);
					blks[t].pop_back();
				}
			}
//			if( strcmp(str2,"over")==0 ){
				blks[pos[from]].pop_back();
				pos[from] = pos[to];
				blks[pos[to]].push_back(from);
//			}
		}else{
			if( strcmp(str2,"onto")==0 ){
				int t = pos[to];		 
				while( blks[t].back()!=to ){
					int top = blks[t].back();
					pos[top] = top;
					blks[top].push_back(top);
					blks[t].pop_back();
				}
			}
			int t = pos[from];
			vector<int> tmp;
			while( blks[t].back()!=from ){
				tmp.push_back(blks[t].back());
				blks[t].pop_back();
			}
			tmp.push_back(blks[t].back());
			blks[t].pop_back();
			t = pos[to];
			while( !tmp.empty() ){
				pos[tmp.back()] = t;
				blks[t].push_back(tmp.back());
				tmp.pop_back();
			}
		}
	}
	for(int i=0; i<n; i++){
		printf("%d:",i);
		for(int j=0; j<blks[i].size(); j++){
			printf(" %d",blks[i][j]);
		}
		cout << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值