当前位置:网站首页>[exercise-8] (UVA 246) 10-20-30== simulation

[exercise-8] (UVA 246) 10-20-30== simulation

2022-07-06 15:56:00 Flame car

The question :
A game . common 52 card (1~10 Number in ) The initial state , Put it in the order of input In the total pile . Then start from the beginning , Take it next to each other 7 Zhang , Swing from left to right , As 7 Pile up . Then go back to the first pile , In this cycle, stack one sheet at a time . After each card is played , Consider the following :
1. The sum of the first two and the last one in this pile is equal to 10 or 20 or 30
2. The sum of the first and last two sheets of this pile is equal to 10 or 20 or 30
3. The sum of the last three sheets of this pile is equal to 10 or 20 or 30
If one of these conditions is satisfied , Then take these three cards out of the pile , Put it at the end of the total heap ( Be careful not to change the order of the three cards ). When multiple situations are satisfied , Give priority to the front . When a pile of cards is emptied just once , Then this pile will be forgotten forever , No more upward licensing . ask : The final state . if 7 The pile is empty , be Win. If the total stack is empty , be Loss. If you fall into an infinite loop , Then for Draw Then output the number of steps .

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const ll mod = 1e9+7;
const int maxn = 1e3+5;
int situation1(deque<int>tmp)// situation 1: Two heads and one tail  
{
    
    int sum=0;
    sum+=tmp.front(); tmp.pop_front();
    sum+=tmp.front(); tmp.pop_front();
    sum+=tmp.back(); tmp.pop_back();
    return sum%10==0; // Whether it is 10 Multiple ( The range of possible numbers is 3-30 So it's no problem to write like this ) 
}
int situation2(deque<int>tmp)// situation 2: One head and two tails  
{
    
    int sum=0;
    sum+=tmp.front(); tmp.pop_front();
    sum+=tmp.back(); tmp.pop_back();
    sum+=tmp.back(); tmp.pop_back();
    return sum%10==0;
}
int situation3(deque<int>tmp)// situation 3: Three tails  
{
    
    int sum=0;
    sum+=tmp.back(); tmp.pop_back();
    sum+=tmp.back(); tmp.pop_back();
    sum+=tmp.back(); tmp.pop_back();
    return sum%10==0;
}
// Because it's a function that passes parameters , Therefore, its operation will not affect the double ended queue itself .
// This is the judgment of the feasibility of this operation . 
int solve(deque<int>&tmp, deque<int>&all) 
{
    	// Simulate the operation process , Here is a quote , The operation will affect the double ended queue itself . 
    if(tmp.size()<3)return 0;// Less than three cards cannot be operated . 
    if(situation1(tmp))
    {
    	// If the feasibility judgment of a certain situation passes , It is necessary to perform this operation .
		// And put the cards in the total deck in order . 
        int s1=tmp.front(); tmp.pop_front();
        int s2=tmp.front(); tmp.pop_front();
        int s3=tmp.back(); tmp.pop_back();
        all.push_back(s1);
        all.push_back(s2);
        all.push_back(s3);
        return 1;
    }
    else if(situation2(tmp))
    {
    
        int s1=tmp.front(); tmp.pop_front();
        int s2=tmp.back(); tmp.pop_back();
        int s3=tmp.back(); tmp.pop_back();
        all.push_back(s1);
        all.push_back(s3);
        all.push_back(s2);
        return 1;
    }
    else if(situation3(tmp))
    {
    
        int s1=tmp.back(); tmp.pop_back();
        int s2=tmp.back(); tmp.pop_back();
        int s3=tmp.back(); tmp.pop_back();
        all.push_back(s3);
        all.push_back(s2);
        all.push_back(s1);
        return 1;
    }
    return 0;
}
int main()
{
    
	int a[52];
	// Used to initialize the order of cards . 
    vector<deque<int> > deq;
    // use vector In fact, it is about equal to opening a deque Array of . 
    set< vector<deque<int> > > repeat; 
    // This is to judge whether there is a circular section ( Repeat the same situation ) 
    for(int i=0;i<8;i++) deq.push_back(deque<int>()); 
    // initialization “ Double ended queue array ” namely VECTOR deque  
    deque<int> &all = deq[7];
	// Why should we directly let all And deq[7] Hook up rather than directly define all Well ?
	// The reason is that the order of the current deck should also be considered when judging whether it is repeated (all) 
    while(cin>>a[0]&&a[0])
    {
    
        all.clear();repeat.clear();
        for(int i=1;i<52;i++) cin>>a[i];
        for(int i=0;i<52;i++) all.push_back(a[i]);
        for(int i=0;i<7;i++)
        {
    
        	deq[i].clear();
            int num=all.front();all.pop_front();    
            deq[i].push_back(num);
        }
        int ans=7;
		// Record the steps ( In the above step, stack one card for each card , So there are seven steps ) 
        int point=0;
		// Record the current deck  
        while(1)
        {
    
            ans++;
            int num=all.front(); all.pop_front();
            deq[point].push_back(num); 
            while(solve(deq[point],all));
            if(repeat.count(deq))
			// Here is to see if it has appeared deq Permutation , If it happens , Will repeat indefinitely .
			// The repetition here deq The arrangement of means  8 Two double ended queue arrays are all the same . 
            {
    
                printf("Draw: %d\n",ans);
                break;
            }
            repeat.insert(deq);
            if(all.size()==0)
            {
    
                printf("Loss: %d\n",ans);
                break;
            }
            if(all.size()==52)
            {
    
                printf("Win : %d\n",ans);
                break;
            }
            do{
    
                point=(point+1)%7;
            }while(deq[point].size()==0);
            // If there are no cards in the stack, skip . 
        }
    }
    return 0;
}



That's interesting .

原网站

版权声明
本文为[Flame car]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919552241.html