UVA-127 "Accordian" Patience(模拟)
题目:
把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(模拟)的更多相关文章
- 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张扑克牌排成一列,每次操作可将一张扑克牌移到它的前一张或前面第三张上当牌的点数或花色匹配时.每次都移动最靠左的扑克牌,并且能移动三格就移动三格.求最终扑克牌状态. 题目分 ...
随机推荐
- Spring Boot Controller
接上篇文章.HelloWorld程序中我们已经创建了一个HellController,里面包括了响应JSON的方法.本文针对Controller再做一下解说. 回想上篇文章,我们在Controller ...
- Linux在终端启动程序关闭终端不退出的方法
一般情况下关闭终端时,那么在这个终端中启动的后台程序也会终止,要使终端关闭后,后台程序保持执行,使用这个指令: nohup 命令 & 如:nohup ./studio.sh & 网上其 ...
- 【Poj 1330】Nearest Common Ancestors
http://poj.org/problem?id=1330 题目意思就是T组树求两点LCA. 这个可以离线DFS(Tarjan)-----具体参考 O(Tn) 0ms 还有其他在线O(Tnlogn) ...
- 揭秘Node.js深受欢迎的原因
揭秘Node.js深受欢迎的原因 http://www.php100.com/html/dujia/2014/1127/7922.html
- POJ 2452 Sticks Problem (暴力或者rmq+二分)
题意:给你一组数a[n],求满足a[i] < a[k] < a[j] (i <= k <= j)的最大的 j - i . 析:在比赛时,我是暴力做的,虽然错了好多次,后来说理解 ...
- BEM --Yandex的CSS 命名方法论
人们问我最多的问题之一是在CSS类名中--和__是什么意思?它们的出现是源于BEM和Nicolas Gallagher... BEM的意思就是块(block).元素(element).修饰符(modi ...
- [App Store Connect帮助]五、管理构建版本(3)在您提交以供审核前选择构建版本
在提交 App 至“App 审核”前,请(从您为该版本上传的所有构建版本中)选择您想要提交的版本.一个 App Store 版本仅可关联一个构建版本.但是,在提交该版本至“App 审核”之前,您可以任 ...
- 【题解】TES-Intelligence Test
[题解]\(TES-Intelligence\) \(Test\) 逼自己每天一道模拟题 传送:\(TES-Intelligence\) \(Test\) \([POI2010]\) \([P3500 ...
- JavaScript编程艺术-第7章代码汇总(2)
[7.4节] 重回“JavaScript美术馆”代码 ***亲测可用*** HTML: JS:
- [W3School]JavaScript教程学习
JavaScript 简介 JavaScript 是世界上最流行的编程语言.这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记本电脑.平板电脑和智能手机等设备. JavaScript ...