zoukankan      html  css  js  c++  java
  • UVA 246 10-20-30

    题意:

      给52张的扑克堆,先从左往右发7张牌,之后连续不断从左往右发7张牌,如果有牌堆形成了以下3种情况(按顺序判断):1、头两张+尾一张和为10或20或30。2、头一张+尾两张和为10或20或30。3、尾三张和为10或20或30。就把这三张牌拿走,放到总牌堆底(这步要不断执行直到不再满足条件或牌堆没了)如果有一个牌堆因为这个操作被取完了,那么以后将不在这个位置发牌。如果最后7个牌堆都可以消掉,那么赢,总牌堆用完,那么输,否则平(即不断循环)。问最后的输赢平,并输出步数

    分析:

      模拟,用一个vector记录下7个牌堆和总牌堆,这样就可以用set去记录状态了,然后每个牌堆用一个双端队列deque表示,这样满足可以从头也可以从尾巴取,不断模拟即可

    代码:

      

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <deque>
    #include <vector>
    #include <map>
    #include <queue>
    using namespace std;
    const int N=7;
    int ans;
    struct node
    {
        int v[N*10];
        bool operator<(const node &a) const
        {
            return memcmp(v,a.v,sizeof(node))<0;
        }
    };
    queue<int> hand;
    deque<int> piles[N];
    map<node,int> st;
    void handle (deque<int>& pile)
    {
        while (pile.size() >= 3)
        {
            int n=pile.size();
    		if((pile[0]+pile[1]+pile[n - 1])%10==0)
    		{
    			hand.push(pile[0]);
    			hand.push(pile[1]);
    			hand.push(pile[n-1]);
    			pile.pop_front();
    			pile.pop_front();
    			pile.pop_back();
    		}
            else if((pile[0]+pile[n-1]+pile[n-2])%10==0)
            {
    			hand.push(pile[0]);
    			hand.push(pile[n-2]);
    			hand.push(pile[n-1]);
    			pile.pop_front();
    			pile.pop_back();
    			pile.pop_back();
    		}
    		else if((pile[n-1]+pile[n-2]+pile[n-3])%10==0)
            {
    			hand.push(pile[n - 3]);
    			hand.push(pile[n - 2]);
    			hand.push(pile[n - 1]);
    			pile.pop_back();
    			pile.pop_back();
    			pile.pop_back();
    		}
    		else
    			return;
    	}
    }
    int solve()
    {
        for(int i=0;i<2;i++)
        {
            for(int j=0;j<N;j++)
            {
                piles[j].push_back(hand.front());
                hand.pop();
            }
        }
        ans=14;
        node tmp;
        while(hand.size())
        {
            for(int i=0;i<N;i++)
            {
                if(hand.size()==52)
                    return 1;
                if(piles[i].size()==0)
                    continue;
                if(hand.size())
                {
                    piles[i].push_back(hand.front());
                    hand.pop();
                    ans++;
                    handle(piles[i]);
                    int cnt=0;
                    memset(tmp.v,0,sizeof(tmp.v));
                    for(int k=0;k<N;k++)
                    {
    					for (int j=0;j<piles[k].size();j++)
    						tmp.v[cnt++]=piles[k][j];
    					tmp.v[cnt++]=11;
    				}
    				queue<int>q=hand;
    				while (!q.empty())
                    {
    					tmp.v[cnt++]=q.front();
    					q.pop();
    				}
    				tmp.v[cnt]=11;
    				if(st[tmp])
                        return -1;
                    else
                        st[tmp]=1;
                }
                else
                    return  0;
            }
        }
        return 0;
    }
    int main()
    {
        int card;
        while(scanf("%d",&card)&&card)
        {
            while (!hand.empty())
            {
                hand.pop();
            }
            st.clear();
            for (int i = 0; i < N; i++)
                piles[i].clear();
            hand.push(card);
            for(int i=0;i<51;i++)
            {
                scanf("%d",&card);
                hand.push(card);
            }
            int tmp=solve();
            if (tmp == 0)
    			printf ("Loss: %d
    ", ans);
    		else if (tmp == 1)
    			printf ("Win : %d
    ", ans);
    		else
    			printf ("Draw: %d
    ", ans);
    
        }
    }
  • 相关阅读:
    模糊查询于聚合函数
    用SQL语句操作数据
    数据库增删改查
    错题本4
    数据查询基础
    用表组织数据
    初识数据库
    错题本3
    深入C#的String类
    定位
  • 原文地址:https://www.cnblogs.com/137033036-wjl/p/4907459.html
Copyright © 2011-2022 走看看