zoukankan      html  css  js  c++  java
  • E

    - 题目大意

         先理解给出的四个移动方式:

    1. move a onto b:把木块a、b上的木块放回各自的原位,再把a放到b上;
    1. move a over b:把a上的木块放回各自的原位,再把a发到含b的堆上;

    2. pile a onto b:把b上的木块放回各自的原位,再把a连同a上的木块移到b上;

    3. pile a over b:把a连同a上木块移到含b的堆上。

     然后根据条件来移动即可。

    - 解题思路

        如果把每个情况都写出来肯定很麻烦,所以首先先找出他们的共同点然后来写出算法,这样就会简单很多,如

    1. 找到某木块
    2. 将某木块上的木块放回原位
    3. 将某木块及其上面木块移动到另一个木块上

    - 代码

        

    #include<string>
    #include<vector>
    #include<iostream>
    	using namespace std;
    	int n;
    	vector<int> num[24];
    	void find(int a, int & i, int &j )
    	{
    		for (i = 0; i < n; i++)
    			for (j = 0; j< num[i].size(); j++)
    				if (num[i][j] == a) return;
    	}
    	void over(int j, int h)
    	{
    		for (int i = h + 1; i < num[j].size(); i++)
    		{
    			int b = num[j][i];
    			num[b].push_back(b);
    		}
    		num[j].resize(h + 1);
    	}
    	void renew(int p, int h, int g )
    	{
    		for (int i = h; i < num[p].size(); i++)
    			num[g].push_back(num[p][i]);
    		num[p].resize(h);
    	}
    	int main()
    	{
    		int a, b;
    		cin >> n;
    		string s1, s2;
    		for (int i = 0; i < n; i++) 
    			num[i].push_back(i);
    		while (cin >> s1)
    		{
    			if (s1 == "quit")
    			{
    				for (int i = 0; i < n; i++)
    				{
    					cout << i<<":";
    					for (int j = 0; j < num[i].size(); j++)
    						cout <<" "<<num[i][j];
    					cout << endl; 
    				}
    				break;
    			}	
    			cin >> a >> s2 >> b;
    			int q, w, e, r;
    			find(a, q, e);
    			find(b, w, r);
    			if (q == w)
    				continue;
    			if (s2 == "onto")
    				over(w, r);
    			if (s1 == "move")
    				over(q, e);
    			renew(q, e, w);
    		}
    
    		system("pause");
    		return 0;
    	}
    

      

  • 相关阅读:
    apache的日志切割
    实现HTTPS--Apache+Openssl
    CentOS 6.x 编译安装LAMP
    apache的域名跳转
    模型生成过程中检测到一个或多个验证错误
    电商时代已经要过去了。接下来是零售
    电商时代已经要过去了。接下来是零售
    华为手机怎么安装Google
    华为手机怎么安装Google
    table不让td中文字溢出操作方法
  • 原文地址:https://www.cnblogs.com/alpacadh/p/8438509.html
Copyright © 2011-2022 走看看