TOJ-1338 Snap

Snap is a 2-player card game. The deck of cards contains several of each type of card. Initially each player has one half of the deck, in some random sequence, face down in a pile, and plays them in sequence from top to bottom, placing each card face-up in another pile. When the face-down pile is exhausted, the face-up pile is turned over to become the face-down pile and play continues.

The two players play their cards concurrently and synchronously. That is, each places a card face up at exactly the same time. If two cards turned up at the same time are the same type, each player calls "Snap!" and the player who calls first takes the other's face-up pile and places it on top of his or her own.

Play proceeds until one player has all the cards. This player is the winner.

Your job is to simulate a game of snap to determine whether it will end within 1000 turns and, if so, to determine the winner.

Each type of card is denoted by a single letter or digit. The first line of input Jane's initial pile of cards, from top to bottom. The second line of input is John's initial pile. Jane and John start with the same number of cards; not more than 50 each.

During play, whenever it comes time to call "Snap!" the builtin function "random" is used to determine who calls first: if the expression

   rand()/141%2             {in C or C++}

   random div 141 mod 2     {in Pascal; see note below}
yields 0, Jane calls first; otherwise John calls first. Whenever Jane calls first, print "Snap! for Jane: " followed by Jane's face-up pile, from top to bottom. Whenever John calls first, print "Snap! for John: " followed by John's face-up pile. If the game ends, print "John wins." or "Jane wins.", whichever is appropriate. If the game does not end when each player has turned over 1000 cards, print "Keeps going and going ..."

Sample Input

ABCDA
CBADC

Output for Sample Input

Snap! for Jane: BCBA
Snap! for Jane: DADCBCBA
Snap! for John: CBAC
Snap! for John: ADADCBAC
John wins.



Source: Waterloo Local Contest Jan. 29, 2000

两人开始各有一堆牌,牌面朝下,两人每次都将各自牌堆顶的牌亮出并各自放在一个牌面朝上的牌堆顶,

若两张牌相同,两人喊snap,先喊的人将对方牌面朝上的牌堆放在自己的对应牌堆上。若他们牌面向下

的牌堆没牌了,将自己的牌面向上的牌堆倒置做牌面向下的牌堆。若一人没牌了,另一人胜。若两人共

倒置超过1000次,则输出:这俩人没完了。。。

用vector模拟,纯粹的模拟。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<char> jane_down,jane_up,john_down,john_up;//四个牌堆
int main(){
    int i,j,k;
    string s1,s2;
    vector<char>::iterator iter;
    cin>>s1;cin>>s2;
    for(i=0;i<s1.length();i++)    jane_down.push_back(s1[i]);
    for(i=0;i<s2.length();i++)    john_down.push_back(s2[i]);
    for(i=0;i<1000;i++){
        if(jane_down.empty()){
            cout<<"John wins."<<endl;    return 0;
        }
        else if(john_down.empty()){
            cout<<"Jane wins."<<endl;    return 0;
        }
        k = min(jane_down.size(),john_down.size());
        while(k--){
            jane_up.push_back(jane_down.front()); //将一张牌翻至面朝上 
            john_up.push_back(john_down.front());
            if(john_down.front()==jane_down.front()) {
                if((rand()/141)%2==0){
                    jane_up.insert(jane_up.end(),john_up.begin(),john_up.end());
                    john_up.clear();
                    cout<<"Snap! for Jane: ";
                    j=0;
                    for(iter=jane_up.begin();iter!=jane_up.end();iter++){
                        s[j++]=*iter;
                    }
                    for(iter=jane_up.end()-1;iter>=jane_up.begin();iter--){
                        cout<<*iter;
                    }
                    cout<<endl;    
                }
                else{
                    john_up.insert(john_up.end(),jane_up.begin(),jane_up.end());
                    jane_up.clear();
                    cout<<"Snap! for John: ";
                    for(iter=john_up.end()-1;iter>=john_up.begin();iter--){
                        cout<<*iter;
                    }
                    cout<<endl;
                }
            }
            jane_down.erase(jane_down.begin());
            john_down.erase(john_down.begin());
        }
        if(jane_down.empty()){
            jane_down = jane_up;
            jane_up.clear();
        }
        if(john_down.empty()){
            john_down = john_up;
            john_up.clear();
        }
    }
    cout<<"Keeps going and going ..."<<endl;
    return 0;
}

 

posted @ 2017-02-07 17:14  DGSX  阅读(304)  评论(0编辑  收藏  举报