UVA-127 "Accordian" Patience (模拟)
题目大意:一种纸牌游戏,将52张扑克牌排成一列,每次操作可将一张扑克牌移到它的前一张或前面第三张上当牌的点数或花色匹配时。每次都移动最靠左的扑克牌,并且能移动三格就移动三格。求最终扑克牌状态。
题目分析:利用栈这种数据结构模拟,以为会超时,没想到AC了。
代码如下:
# include<iostream>
# include<cstdio>
# include<map>
# include<stack>
# include<cstring>
# include<algorithm>
using namespace std; struct Poke
{
char v,f;
Poke(char _v,char _f):v(_v),f(_f){}
};
char str[2];
stack<Poke>s[52]; int get(int pos,int k)
{
for(int i=pos-1;i>=0;--i){
if(!s[i].empty()){
--k;
if(k==0) return i;
}
}
return -1;
} bool match(const Poke &a,const Poke &b)
{
return a.v==b.v||a.f==b.f;
} bool toDo(int i,int k)
{
Poke a=s[i].top();
Poke b=s[k].top();
if(match(a,b)){
s[k].push(a);
s[i].pop();
return true;
}
return false;
} void solve()
{
int i;
while(1)
{
for(i=1;i<52;++i){
if(s[i].empty())
continue;
int k=get(i,3);
if(k!=-1&&toDo(i,k))
break;
k=get(i,1);
if(k!=-1&&toDo(i,k))
break;
}
if(i>=52)
break;
}
} int main()
{
while(scanf("%s",str))
{
if(str[0]=='#')
break;
for(int i=0;i<52;++i)
while(!s[i].empty())
s[i].pop();
s[0].push(Poke(str[0],str[1]));
for(int i=1;i<52;++i){
scanf("%s",str);
s[i].push(Poke(str[0],str[1]));
Poke u=s[i].top();
}
solve();
int k=0;
for(int i=0;i<52;++i)
if(!s[i].empty())
++k;
if(k==1)
printf("1 pile remaining: 52\n");
else{
printf("%d piles remaining:",k);
for(int i=0;i<52;++i)
if(!s[i].empty())
printf(" %d",s[i].size());
printf("\n");
}
}
return 0;
}
UVA-127 "Accordian" Patience (模拟)的更多相关文章
- ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)
Description ``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patie ...
- UVa 127 - "Accordian" Patience
题目:52张扑克,从左到右在平面上排列,按着如下规则处理: 1.按照从左到右的顺序,如果一张牌和左边的第一张或者第三张匹配,就把它放到对应的牌上面. 2.如果可以移动到多个位置,移动到最左端的牌上面. ...
- Uva 127 poj 1214 `Accordian'' Patience 纸牌游戏 模拟
Input Input data to the program specifies the order in which cards are dealt from the pack. The inpu ...
- [刷题]算法竞赛入门经典(第2版) 6-9/UVa127 - "Accordian" Patience
题意:52张牌排一行,一旦出现任何一张牌与它左边的第一张或第三张"匹配",即花色或点数相同,则须立即将其移动到那张牌上面,将其覆盖.能执行以上移动的只有压在最上面的牌.直到最后没有 ...
- UVA127- "Accordian" Patience(模拟链表)
"Accordian" Patience You are to simulate the playing of games of ``Accordian'' patience, t ...
- ACM学习历程——UVA127 "Accordian" Patience(栈, 链表)
Description ``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patie ...
- UVa 127 - "Accordian" Patience POJ 1214 链表题解
UVa和POJ都有这道题. 不同的是UVa要求区分单复数,而POJ不要求. 使用STL做会比較简单,这里纯粹使用指针做了,很麻烦的指针操作,一不小心就错. 调试起来还是很费力的 本题理解起来也是挺费力 ...
- 【习题 6-9 UVA - 127】"Accordian" Patience
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 链表模拟即可. 1pile不能加s... [代码] #include <bits/stdc++.h> using nam ...
- UVA-127 "Accordian" Patience(模拟)
题目: 把52张牌从左到右排好,每张牌自成一个牌堆.当某张牌与它左边那张牌或者左边第三张牌匹配时(花色或者点数相同)时,就把这张牌移到那张牌上面. 移动之后还要查看是否可以进行其他移动.只有位于牌堆顶 ...
随机推荐
- Django框架【基础篇】
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- SQL调优学习之——sqlserver分页从低效到高效
背景 首先感谢网友@aixuexi 在评论中的提醒,原博文介绍的几种都不是最高效,现已修改加入另一种更高效的方法. 以前都是使用mysql和oracle,对sqlserver的使用不多.最近因项目原因 ...
- MySQL server has gone away 问题的解决方法(转)
mysql出现ERROR : (2006, 'MySQL server has gone away') 的问题意思就是指client和MySQL server之间的链接断开了. 造成这样的原因一般是s ...
- Selenium之firefox浏览器的启动
1.编写如下代码 import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; /** ...
- ng-深度学习-课程笔记-1: 介绍深度学习(Week1)
1 什么是神经网络( What is a neural network ) 深度学习一般是指非常非常大的神经网络,那什么是神经网络呢? 以房子价格预测为例,现在你有6个房子(样本数量),你知道房子的大 ...
- uwsgi手动安装时报错ValueError: invalid literal for int() with base 10: '32_1'
安装uwsgi,安装步骤如下 wget https://projects.unbit.it/downloads/uwsgi-latest.tar.gz tar zxvf uwsgi-latest.ta ...
- SQL 处理排序空值
①oracle默认排序空值在后面 如果想要排序空值在前面可用关键字 NULLS FIRST, 排序空值在后面也有关键字NULLS LAST ②sqlserver默认排序空值在前面 如果想要排序时空值在 ...
- C#——文件上传(一般处理程序ashx)
Framework版本:.Net Framework 4 1.FileInfo实体 using System; using System.Collections.Generic; using Syst ...
- ubuntu14.04 安装apache+mysql+php
1.安装apache sudo apt-get update sudo apt-get install apache2 这时http://你机器的ip,就可以访问了. 2.安装mysql sudo a ...
- HDU1087
/*记忆化dfs+dp,因为每次最多走k步,所以上下左右的方向有所扩展, dp[i][j]存的是从dp[i][j]出发能吃的最大个数*/ #include<stdio.h> #includ ...