题目:

把52张牌从左到右排好,每张牌自成一个牌堆。当某张牌与它左边那张牌或者左边第三张牌匹配时(花色或者点数相同)时,就把这张牌移到那张牌上面。

移动之后还要查看是否可以进行其他移动。只有位于牌堆顶部的牌才能移动或者参与匹配。当牌堆之间出现空隙时要立刻把右边的所有牌堆左移一格来填

补空隙。如果有多张牌可以移动,先移动最左边的那张牌;如果即可以移一格也可以移3格时,移3格。按顺序输入52张牌,输出最后牌堆数以及各牌堆的牌数。

思路:

看完之后知道要用vector和stack来结合解题,但是没有想到erase方法的使用,结果导致代码写的太冗杂。

开52个stack存到vector中,然后按照题目模拟过程。因为移动之后还要判断能否继续移动,那这里就可以结束这次的移动之后跳出这个循环,直接进行下一次

循环就可以了。

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define MAX 1000000009
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
const int maxn = ;
int n,m,d[maxn],vis[maxn];
struct Card {
char number;
char color;
Card() {}
Card(char n, char c):number(n),color(c) {}
};
vector<stack<Card> > pile; void makeString(string str) {//处理字符串,保存所有的牌
//cout<<str<<endl;
for(int i = ; i<str.length(); i+=) {
stack<Card> sta;
sta.push(Card(str[i],str[i+]));
pile.push_back(sta);
}
} bool judge(Card a,Card b) {//判断两张牌是否可以匹配
if(a.color==b.color || a.number==b.number) {
return true;
}
return false;
} void solve() {
// for(int i = 0; i<52; i++){
// cout<<pile[i].top().number<<pile[i].top().color<<" ";
// if(i==25)
// cout<<endl;
// }
// cout<<endl;
while(true) {
bool ok = false;
for(int i=; i<pile.size(); i++) {
if(i->= && judge(pile[i].top(),pile[i-].top())) {//移动3格
pile[i-].push(pile[i].top());
pile[i].pop();
ok = true;
if(pile[i].empty()) {
pile.erase(pile.begin()+i);
}
break;
} else if(i->= && judge(pile[i].top(),pile[i-].top())) {//移动1格
pile[i-].push(pile[i].top());
pile[i].pop();
ok = true;
if(pile[i].empty()) {
pile.erase(pile.begin()+i);
}
break;
}
}
if(!ok) {
break;
}
}
cout<<pile.size();
if(pile.size()==){
cout<<" pile remaining: 52"<<endl;
}else{
cout<<" piles remaining:";
for(int i=; i<pile.size(); i++){
cout<<" "<<pile[i].size();
}
cout<<endl;
}
} int main() {
//FRE();
string str;
while(true) {
pile.clear();
int idx=;
getline(cin,str);
if(str=="#") {
break;
}
makeString(str);
getline(cin,str);
makeString(str);
solve();
}
return ;
}

UVA-127 "Accordian" Patience(模拟)的更多相关文章

  1. ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)

    Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patie ...

  2. UVa 127 - "Accordian" Patience

    题目:52张扑克,从左到右在平面上排列,按着如下规则处理: 1.按照从左到右的顺序,如果一张牌和左边的第一张或者第三张匹配,就把它放到对应的牌上面. 2.如果可以移动到多个位置,移动到最左端的牌上面. ...

  3. 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 ...

  4. [刷题]算法竞赛入门经典(第2版) 6-9/UVa127 - "Accordian" Patience

    题意:52张牌排一行,一旦出现任何一张牌与它左边的第一张或第三张"匹配",即花色或点数相同,则须立即将其移动到那张牌上面,将其覆盖.能执行以上移动的只有压在最上面的牌.直到最后没有 ...

  5. UVA127- "Accordian" Patience(模拟链表)

    "Accordian" Patience You are to simulate the playing of games of ``Accordian'' patience, t ...

  6. ACM学习历程——UVA127 "Accordian" Patience(栈, 链表)

    Description  ``Accordian'' Patience  You are to simulate the playing of games of ``Accordian'' patie ...

  7. UVa 127 - &quot;Accordian&quot; Patience POJ 1214 链表题解

    UVa和POJ都有这道题. 不同的是UVa要求区分单复数,而POJ不要求. 使用STL做会比較简单,这里纯粹使用指针做了,很麻烦的指针操作,一不小心就错. 调试起来还是很费力的 本题理解起来也是挺费力 ...

  8. 【习题 6-9 UVA - 127】"Accordian" Patience

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 链表模拟即可. 1pile不能加s... [代码] #include <bits/stdc++.h> using nam ...

  9. UVA-127 "Accordian" Patience (模拟)

    题目大意:一种纸牌游戏,将52张扑克牌排成一列,每次操作可将一张扑克牌移到它的前一张或前面第三张上当牌的点数或花色匹配时.每次都移动最靠左的扑克牌,并且能移动三格就移动三格.求最终扑克牌状态. 题目分 ...

随机推荐

  1. 洛谷P3690 LCT模板

    题目:https://www.luogu.org/problemnew/show/P3690 自己竟然从没有钻研过LCT上的连通性问题! 于是被最后一个点卡了,似乎因为 find 函数只能找出连通性而 ...

  2. 双栈排序 2008年NOIP全国联赛提高组(二分图染色)

    双栈排序 2008年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master     题目描述 Description Tom最近在研究一个有 ...

  3. 汇编程序49:实验14 访问CMOS RAM(显示系统时间)

    assume cs:code ;安装程序,使用指令out和in指令 code segment start: mov ax,cs mov ds,ax mov si,offset sub1 mov ax, ...

  4. 最大化最小值poj2456Aggressive cows

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15528   Accepted: 7440 ...

  5. GIT学习之路第四天 远程仓库

    本文参考廖雪峰老师的博客进行总结,完整学习请转廖雪峰博客 git的服务器---Github,自行注册github账号后,按下面的步骤操作: 第一步,事实上,本地Git仓库和Github仓库之间的传输是 ...

  6. ACM_递推题目系列之三放苹果(递推dp)

    递推题目系列之三放苹果 Time Limit: 2000/1000ms (Java/Others) Problem Description: 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放 ...

  7. 通过创建元素从而实现三个下拉框的联动效果(create.Element("option"))和提交表单时的验证p.match("请选择")

    <html> <head> <meta charset="utf-8"> <title>下拉框</title> < ...

  8. xcode 制作静态库文件(.a)

    参考: http://www.jb51.net/article/37853.htm 摘要: 1. 获取.a文件的信息              lipo -info /Users/pjk1129/De ...

  9. 揭开WebService的神秘面纱

    一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...

  10. MySQL客户端导入数据库脚本,字段值出现乱码解决方法

    解决方法1:在MySql安装目录下找到my.ini,将[mysql]下的default-character-set=latin1改为default-character-set=utf8,保存,然后重启 ...