zoukankan      html  css  js  c++  java
  • uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟

    挺水的模拟题,刚开始题目看错了,poj竟然过了。。。无奈。uva果断wa了

    搞清题目意思后改了一下,过了uva。

    题目要求模拟木块移动:

    有n(0<n<25)快block,有5种操作:

    move a onto b  在将a搬到b上之前,先把a和b上的积木放回原來的位置

    move a over b在将a搬到b所在的那堆积木上前,先把a上的积木放回原來的位罝

    pile a onto b 将包括a本身和上方的积木一起放到b上,在放之前b上方的积木放回原来的位置

    pile a over b 将包括a本身和上放的积木一起搬到到b所在的那堆上 

    quit结束命令,前四个动作中若ab在同一堆中,则不做改变。


    我用的vector模拟,其实用线性表也不错的,纯当练习stl了。


    代码:

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    typedef vector<int>::iterator VI;
    
    vector <int> v[30];
    int rec[30];
    int n;
    
    VI Find(vector <int> &v, int num) {
    	for (VI i = v.begin(); i != v.end(); i++)
    		if (*i == num) {
    			return i;
    		}
    	return v.begin() - 1;
    }//find num in vector v
    
    void check() {
    /*	for (int i = 0; i < n; i++)
    		cout << rec[i] << ' ';
    	cout << endl;
    	*/
    	for (int i = 0; i < n; i++) {
    		cout << i << ':';
    		if (v[i].empty()) {
    			cout << endl;
    			continue;
    		}
    		cout << ' ' << v[i][0];
    		for (int j = 1; j < v[i].size(); j++)
    			cout << ' ' << v[i][j];
    		cout << endl;
    	}
    }
    
    int main() {
    	cin >> n;
    	for (int i = 0; i < n; i++) {
    		v[i].clear();
    		v[i].push_back(i);
    		rec[i] = i;
    	}//for init
    	string ts;
    	int tn1, tn2;
    	while (cin >> ts && ts != "quit") {
    		if (ts == "move") {
    			cin >> tn1 >> ts >> tn2;
    			if (rec[tn1] == rec[tn2]) continue;
    			while (v[rec[tn1]].back() != tn1) {
    				rec[v[rec[tn1]].back()] = v[rec[tn1]].back();
    				v[v[rec[tn1]].back()].push_back(v[rec[tn1]].back());
    				v[rec[tn1]].pop_back();
    			}
    			v[rec[tn1]].pop_back();
    			if (ts == "onto") {
    				VI i;
    				while (v[rec[tn2]].back() != tn2) {
    					rec[v[rec[tn2]].back()] = v[rec[tn2]].back();
    					v[v[rec[tn2]].back()].push_back(v[rec[tn2]].back());
    					v[rec[tn2]].pop_back();
    				}
    				v[rec[tn2]].push_back(tn1);
    				rec[tn1] = rec[tn2];
    			}//move onto
    			else {
    				v[rec[tn2]].push_back(tn1);
    				rec[tn1] = rec[tn2];
    		   	}//move over
    		}
    		else {
    			cin >> tn1 >> ts >> tn2;
    			if (rec[tn1] == rec[tn2]) continue;
    			if (ts == "onto") {
    				while (v[rec[tn2]].back() != tn2) {
    					rec[v[rec[tn2]].back()] = v[rec[tn2]].back();
    					v[v[rec[tn2]].back()].push_back(v[rec[tn2]].back());
    					v[rec[tn2]].pop_back();
    				}
    				VI pos1 = Find(v[rec[tn1]], tn1), pos2 = Find(v[rec[tn2]], tn2);
    				int tmp[25], cnt = 0;
    				for (VI i = v[rec[tn1]].end() - 1; i >= pos1; i--) {
    	//				cout << "*i=" << *i << endl;
    					tmp[cnt++] = *i;
    					v[rec[tn1]].erase(i);
    					rec[*i] = rec[tn2];
    				}
    				for (int i = 0; i < cnt / 2; i++) {
    					int t = tmp[i];
    					tmp[i] = tmp[cnt - i - 1];
    					tmp[cnt - i - 1] = t;
    				}
    					v[rec[tn2]].insert(pos2 + 1, tmp, tmp + cnt);
    				
    			}//pile onto
    			else {
    				VI pos1 = Find(v[rec[tn1]], tn1);
    				int tmp[25], cnt = 0;
    				for (VI i = v[rec[tn1]].end() - 1; i >= pos1; i--) {
    	//				cout << "*i=" << *i << endl;
    					tmp[cnt++] = *i;
    					v[rec[tn1]].erase(i);
    					rec[*i] = rec[tn2];
    				}
    				for (int i = 0; i < cnt / 2; i++) {
    					int t = tmp[i];
    					tmp[i] = tmp[cnt - i - 1];
    					tmp[cnt - i - 1] = t;
    				}
    					v[rec[tn2]].insert(v[rec[tn2]].end(), tmp, tmp + cnt);
    			}//pile over
    		}
    	}//while
    	check();
    	return 0;
    }


  • 相关阅读:
    论文(卷积数据流)-Communication Lower Bound in Convolution Accelerators
    CPU架构相关
    Verilog-数据包检测器
    多比特乘法器的分解
    Verilog-数字时钟无毛刺切换
    Booth乘法器
    C++:地平线2019相关题
    C++:char数组和string类
    C++:strcpy函数
    半导体 semiconductor 相关知识
  • 原文地址:https://www.cnblogs.com/java20130723/p/3212119.html
Copyright © 2011-2022 走看看