[uva 246][deque]10-20-30

 10-20-30 

A simple solitaire card game called 10-20-30 uses a standard deck of 52 playing cards in which suit is irrelevant. The value of a face card (king, queen, jack) is 10. The value of an ace is one. The value of each of the other cards is the face value of the card (2, 3, 4, etc.). Cards are dealt from the top of the deck. You begin by dealing out seven cards, left to right forming seven piles. After playing a card on the rightmost pile, the next pile upon which you play a card is the leftmost pile.

For each card placed on a pile, check that pile to see if one of the following three card combinations totals 10, 20, or 30.


		 1. 		 the first two and last one,

2. the first one and the last two, or

3. the last three cards.

If so, pick up the three cards and place them on the bottom of the deck. For this problem, always check the pile in the order just described. Collect the cards in the order they appear on the pile and put them at the bottom of the deck. Picking up three cards may expose three more cards that can be picked up. If so, pick them up. Continue until no more sets of three can be picked up from the pile.

For example, suppose a pile contains 5 9 7 3 where the 5 is at the first card of the pile, and then a 6 is played. The first two cards plus the last card (5 + 9 + 6) sum to 20. The new contents of the pile after picking up those three cards becomes 7 3. Also, the bottommost card in the deck is now the 6, the card above it is the 9, and the one above the 9 is the 5.

If a queen were played instead of the six, 5 + 9 + 10 = 24, and 5 + 3 + 10 = 18, but 7 + 3 + 10 = 20, so the last three cards would be picked up, leaving the pile as 5 9.

If a pile contains only three cards when the three sum to 10, 20, or 30, then the pile "disappears" when the cards are picked up. That is, subsequent play skips over the position that the now-empty pile occupied. You win if all the piles disappear. You lose if you are unable to deal a card. It is also possible to have a draw if neither of the previous two conditions ever occurs.

Write a program that will play games of 10-20-30 given initial card decks as input.

Input

Each input set consists of a sequence of 52 integers separated by spaces and/or ends of line. The integers represent card values of the initial deck for that game. The first integer is the top card of the deck. Input is terminated by a single zero (0) following the last deck.

Output

For each input set, print whether the result of the game is a win, loss, or a draw, and print the number of times a card is dealt before the game results can be determined. (A draw occurs as soon as the state of the game is repeated.) Use the format shown in the ``Sample Output" section.

Sample Input

2 6 5 10 10 4 10 10 10 4 5 10 4 5 10 9 7 6 1 7 6 9 5 3 10 10 4 10 9 2 1
10 1 10 10 10 3 10 9 8 10 8 7 1 2 8 6 7 3 3 8 2
4 3 2 10 8 10 6 8 9 5 8 10 5 3 5 4 6 9 9 1 7 6 3 5 10 10 8 10 9 10 10 7
2 6 10 10 4 10 1 3 10 1 1 10 2 2 10 4 10 7 7 10
10 5 4 3 5 7 10 8 2 3 9 10 8 4 5 1 7 6 7 2 6 9 10 2 3 10 3 4 4 9 10 1 1
10   5 10 10 1 8 10 7 8 10 6 10 10 10 9 6 2 10 10
0

Sample Output

Win : 66
Loss: 82
Draw: 73

写这题算是学了deque的用法,移动牌堆还是类似上题的写法,用pos数组建一个位置到堆的映射,要注意的是,去除一个堆以后下标要自减,以保证下次访问到当前位置的堆,这点我trick很久。学了resize和reserve的区别,这两个和效率有关还是值得注意的。记得resize完以后再赋值。
如果是自己写的class,想用set判重,要重载操作符<,可用memcpy实现。
#include<cstdio>
#include<queue>
#include<set>
#include<vector>
using namespace std;
const int maxn  = 53;
char *ans[3] = {"Draw:", "Loss:", "Win :"};

vector<deque<int> >s(8);
deque<int> &hand  = s[0];

int pos[7];
int plSz;
int step;

inline bool pick(deque<int> & x)
{
   int n = x.size();
   int sum;

   if(sum = x[0] + x[1] + x[n-1], sum == 10|| sum == 20 || sum == 30 ){//
      hand.push_back(x[0]); hand.push_back(x[1]);  hand.push_back(x[n-1]);
      x.pop_front(); x.pop_front();  x.pop_back();
      return true;
   }
   if(n>3){
      if(sum = x[0]+x[n-2]+x[n-1], sum == 10|| sum == 20 || sum == 30) {
         hand.push_back(x[0]); hand.push_back(x[n-2]);  hand.push_back(x[n-1]);
         x.pop_front();  x.pop_back(); x.pop_back();
         return true;
      }
      if(sum = x[n-3]+x[n-2]+x[n-1], sum == 10|| sum == 20 || sum == 30) {
         hand.push_back(x[n-3]); hand.push_back(x[n-2]);  hand.push_back(x[n-1]);
         x.pop_back();  x.pop_back();  x.pop_back();
         return true;
      }
   }

   return false;
}

int sovle()
{
   set<vector<deque<int> > > vis;
   vis.insert(s);
   int t,id;
   for(;;){
      for(int i = 0;i < plSz; i++) {
         if(hand.empty())  return 1;
         t = hand.front(); hand.pop_front();
         step++;
         id = pos[i];
         s[id].push_back(t);
         while(s[id].size()>=3 && pick(s[id])) {
            if(s[id].empty()) {
               plSz--;
               if(plSz == 0) return 2;
               for(int j = i;j < plSz;j++) {//移动堆
                  pos[j] = pos[j+1];
               }
               i--;//访问移动后这堆堆
            }
         }
         if(vis.count(s) == 0) vis.insert(s);
         else return 0;
      }
   }
   return 0;
}

bool read()
{
   int t;
   scanf("%d",&t);
   if(t == 0) return false;

   step = 0;
   for(int i = 1;i <8;i++)
      s[i].clear();
   hand.resize(52);

   for(int i = 0;i <7;i++)
      pos[i] = i+1;
   plSz = 7;

   deque<int>:: iterator it;
   hand[0] = t;
   for(it = hand.begin(), it++; it != hand.end() ; it++)
      scanf("%d",&t), *it = t;
   return true;
}

int main()
{
 //  freopen("in.txt","r",stdin);
   while(read()) {
      int flag = sovle();
      printf("%s %d\n",ans[flag],step);
   }
   return 0;
}
				
时间: 2024-10-20 01:25:34

[uva 246][deque]10-20-30的相关文章

个人回忆录 2014.10.20 至 2015.7.30

时间过的太快.以至于对我来说都记不起来每天做了些什么事情.工作节奏太快,下班.上班 然后再下班再上班. 每天下班后都晚上9点左右.真的看不见日出看不见日落. 从2014.10.20 到现在已经快10个月了.新的工作环境以及新的同事.上司都已熟悉了.回想刚刚开始进入这个研发团队的时候. 高原反应非常强烈,总是在疑问自己为何选择这个方向—C++ 客户端开发.为何不沿用最熟悉的.NET 平台开发.当从新学习一门新技术的时候 才发现自己太笨.有点像当年的高考,时间很紧.因为没有太多的时间用在学习上.MF

UVA 246 - 10-20-30 (模拟+STL)

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

UVA - 246 10-20-30 (模拟+STL)

Description  10-20-30  A simple solitaire card game called 10-20-30 uses a standard deck of 52 playing cards in which suit is irrelevant. The value of a face card (king, queen, jack) is 10. The value of an ace is one. The value of each of the other c

10.19-10.22 iptables规则备份和恢10.20 firewalld的9个zone

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone 10.21 firewalld关于zone的操作 10.22 firewalld关于service的操作 # 10.19 iptables 规则备份和恢复 - 保存和备份iptables 的规则 - service iptables save 会把规则保存到 /etc/sysconfig/iptables - 把iptables规则备份到my.ipt 文件中 - iptables-save > my.ipt

(转自http://www.blogjava.net/moxie/archive/2006/10/20/76375.html)WebWork深入浅出

(转自http://www.blogjava.net/moxie/archive/2006/10/20/76375.html) WebWork深入浅出 本文发表于<开源大本营> 作者:钱安川 前言 本篇文章并没有太多WebWork 的实战代码细节.本人非常希望能充当一名导游的角色,带领读者逐步游览WebWork的功能特性和原理.在第一章,我们将提出基于三层架构的Web层需要解决的10个问题,这是本文的纵轴.围绕着纵轴,我们按照横轴的顺序逐步描述讲解:WebWork简介.WebWork入门.We

从“假如有以下几种价格10,20,50,请你代码实现将他们排序输出”看着设计模式中的策略模式

今天重温了一下策略模式,将自己的一些感悟与大家分享...本人只是技术渣渣,所理解的东西的难免会有很大的局限性甚至是错误,还请各位带着批判的眼光去看待....不喜请勿吐槽 定义:策略模式属于设计模式中的对象行为型模式,它将用到的算法单独抽象成一个单独的类.通常,我们在多个类完成同一件事情,仅仅完成的方式不同时,我们可以考虑使用这种设计模式. 举例:相信,很多人在看到"假如有以下几种价格10,20,50,请你代码实现将他们排序输出"这种题目的时候,很快就写出了以下代码,写之前还不忘了问一下

给你六种面额1 5 10 20 50 100元的纸币假设每种币值的数量足够多

编写程序求组成N元的不同组合的个数.输入一个数字N输出一个也是数字,为组成N的组合个数.如输入2,输出1,输入5,输出2 背包问题: 1 function fn (all) { 2 const arr = [1, 5, 10, 20, 50, 100], 3 len = arr.length, 4 res = []; 5 for (let i = 0; i <= len; i++) { 6 res[i] = []; 7 res[i][0] = 1; 8 } 9 for (let j = 1; j

[OpenStack 存储] 说说RAID0 1 2 3 4 5 6 10 01 30 50, 软RAID, 硬RAID

最近在思考一种廉价方便的cinder集成LVM driver的方式,那就是cinder+LVM+多块盘组成的RAID硬盘.这样的情况下就要根据读写需求和可用资源考虑采用什么样的RAID,以及怎样选择实现raid的方式,有两种分别为硬件RAID和软件RAID,为了温习一下,就顺便画些图(图中使用的硬盘数都是该RAID下需要的最少硬盘数)总结下各个RAID技术. 软RAID与硬RAID 硬RAID可以理解为需要RAID卡,通过RAID卡实现对多块盘的管理, 把多块盘组成RAID冗余阵列,如何组合成R

10.19 iptables规则备份和恢复 10.20 firewalld的9个zone 10.21

七周五次课 10.19 iptables规则备份和恢复 10.20 firewalld的9个zone 10.21 firewalld关于zone的操作 10.22 firewalld关于service的操作 10.19 iptables规则备份和恢复 10.20 firewalld的9个zone 启动firewalld防火墙,关闭iptables 查看所有的zone和默认的zone 10.21 firewalld关于zone的操作 设置默认zone 设置网卡的zone 10.22 firewal