题目:

把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. YTU 2543: 数字整除

    2543: 数字整除 时间限制: 1 Sec  内存限制: 128 MB 提交: 33  解决: 8 题目描述 定理:把一个至少两位的正整数的个位数字去掉,再从余下的数中减去个位数的5倍.当且仅当差是 ...

  2. .net 接收存储过程的返回值 。。。。

    .net 接收存储过程的返回值 .... Posted on 2009-06-10 20:26 且行且思 阅读(...) 评论(...) 编辑 收藏 例如在向数据库添加新数据时,需要检测是否有重复 本 ...

  3. 洛谷 P2312 & bzoj 3751 解方程 —— 取模

    题目:https://www.luogu.org/problemnew/show/P2312 https://www.lydsy.com/JudgeOnline/problem.php?id=3751 ...

  4. bzoj2594 [Wc2006]水管局长数据加强版——LCT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2594 时间倒序一下,就是 魔法森林 那道题: 有个不解的地方,是 access 里面关于 p ...

  5. Centos7 配置防火墙 firewall

    一.firewall 1.从CentOS7开始,默认使用firewall来配置防火墙,没有安装iptables(旧版默认安装). 2.firewall的配置文件是以xml的格式,存储在 /usr/li ...

  6. WP8 中使用HTML Agility Pack与友盟分享SDK遇到的 System.Xml.XPath加载问题

    今晚在尝试使用友盟最新的社交分享SDK时,按照官方Demo,并未做多少多少改动,就是去除了对微信.脸书和推特的分享.然后运行之后就一直报错 : {System.IO.FileLoadException ...

  7. E20170606-gg

    complete adj. 完整的; 完成的; (用以强调) 完全的; 达到结尾的;  vt. 完成,使完满; 完成或结束; 填写(表格); process   n. 过程; 工序; 做事方法; 工艺 ...

  8. bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛【差分】

    s[i]为差分后的"i这头牛前有几头比它高",计算答案的时候加成前缀和,假设第一头最高减一下即可 用map记录一下被加过的区间,避免重复 #include<iostream& ...

  9. P4196 [CQOI2006]凸多边形

    传送门 半平面交的讲解 然而这个代码真的是非常的迷--并不怎么看得懂-- //minamoto #include<bits/stdc++.h> #define fp(i,a,b) for( ...

  10. [App Store Connect帮助]三、管理 App 和版本(6.3)转让 App:接受 App 转让

    您必须在转让发起的 60 天内接受转让. 必要职能:“帐户持有人”职能.请参见职能权限. 以具有“帐户持有人”职能用户的身份登录至 App Store Connect. 系统会显示一条通知,指示 Ap ...