UVa 127 纸牌游戏(栈)
https://vjudge.net/problem/UVA-127
题意:
按从左至右的顺序发牌,并摆成一行,发牌不要相互重叠。游戏中一旦出现任何一张牌与它左边的第一张或第三张“匹配”,即花色或点数相同,则须立即将其移动到那张牌上面。如果牌被移动后又出现了上述情况,则需再次向左移动。每叠牌只能移动最上面的一张。如果一叠牌被移空,应该立即将右边各叠整体向左移动,补上这个空隙。依次将整副牌都发完,并不断的向左合并。输出最后的牌堆数以及各牌堆的牌数。
思路:
代码虽然写着挺长,但题目很简单。用栈解决。
每个牌堆用一个栈来表示,如果牌堆空了也不需要左移啥的,如果栈空则跳过它就好了。每次移动之后,需要重新去前面的那个牌堆开始检验。
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std; struct node
{
char str[];
}c[]; int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (cin >> c[].str)
{
if (c[].str[] == '#') break;
stack<node> sta[];
sta[].push(c[]);
for (int i = ; i < ; i++)
{
cin >> c[i].str;
sta[i].push(c[i]);
}
for (int i = ; i < ; i++)
{
if (sta[i].empty()) continue; //如果第i堆牌为空,继续下一堆
int cnt = ;
int t = i - ;
while (t >= )
{
if (!sta[t].empty())
cnt++;
if (cnt == ) break; //找到i左边第3个牌堆
t--;
}
if (cnt==)
{
node s1 = sta[t].top();
node s2 = sta[i].top();
if (s1.str[] == s2.str[] || s1.str[] == s2.str[])
{
sta[i].pop();
sta[t].push(s2);
i = t-;
continue; //如果和第3个匹配,则不再进行左边第1个的匹配
}
}
t = i - ;
while (t >= && sta[t].empty()) t--;
if (t >= )
{
node s1 = sta[t].top();
node s2 = sta[i].top();
if (s1.str[] == s2.str[] || s1.str[] == s2.str[])
{
sta[i].pop();
sta[t].push(s2);
i = t-; //重新去检查第t个牌堆,因为循环最后会i++,所以这里i要等于t-1
}
}
}
int num[];
int cnt = ;
for (int i = ; i < ; i++)
{
if (sta[i].size() != )
num[cnt++] = sta[i].size();
}
cout << cnt << " pile";
if (cnt>) cout << "s";
cout << " remaining:";
for (int i = ; i < cnt; i++)
cout << " "<< num[i];
cout << endl;
}
return ;
}
UVa 127 纸牌游戏(栈)的更多相关文章
- UVa 1637 纸牌游戏(全概率公式)
https://vjudge.net/problem/UVA-1637 题意: 36张牌分成9堆,每堆4张牌.每次可以拿走某两堆顶部的牌,但需要点数相同.每种拿法的概率均为1/5.求成功概率. 思路: ...
- UVA 127 链表和栈的使用
刘汝佳的题目感觉都是比较难以处理的,就像这道题目,一看数据简直觉得头大...加上这个英文我也看的想死 最后看别人博客的题意讲解才知道原来是要移牌. 然后如果熟练的使用stack和手写链表的话,这个题目 ...
- python 全栈开发,Day30(纸牌游戏,异常和错误,异常处理)
一.纸牌游戏 ...
- HDU 2209 翻纸牌游戏 状态BFS
翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem De ...
- hdu2209翻纸牌游戏
翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 翻纸牌游戏(dfs回溯)
翻纸牌游戏 Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- HDU 2209 翻纸牌游戏(DFS)
题目链接 Problem Description 有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌.但是麻烦 ...
- HDU 2209 翻纸牌游戏
翻纸牌游戏 Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- Python 纸牌游戏
纸牌游戏 # card.py from random import shuffle class Card: # 黑桃,红桃,方块,梅花 suits = ['spades', 'hearts', 'di ...
随机推荐
- SQL 4
SQL WHERE 子句 WHERE 子句用于过滤记录. SQL WHERE 子句 WHERE 子句用于提取那些满足指定标准的记录. SQL WHERE 语法 SELECT column_name,c ...
- hdu1181 (变形课)简单地dfs
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/F Description 呃......变形课上Harr ...
- .Net Core 使用依赖注入
ASP.NET Core 源码阅读笔记(1) ---Microsoft.Extensions.DependencyInjection 在asp .net中使用依赖注入很简单,只需要在Startup类的 ...
- [LeetCode] 196. Delete Duplicate Emails_Easy tag: SQL
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- vs2015 相关
VS2015一共有3个版本,Visual Studio Community(社区版).Visual Studio Professional(专业版).Visual Studio Enterprise( ...
- Django的基本开发环境配置和MTV模型
一.MTV模型 Django的MTV分别代表: Model(模型):负责业务对象与数据库的对象(ORM) Template(模版):负责如何把页面展示给用户 View(视图):负责业务逻辑,并在适当的 ...
- Here we take a closer look at the Jordans Unveil
Here we take a closer look at the Jordans Unveil. This Mens release is both unique and striking. The ...
- VirtualBox 虚拟磁盘的UUID修改
个人测试环境,想构建一套Standby RAC环境,不想再重复去安装系统浪费时间,直接复制之前安装RAC前的一套VirtualBox的虚拟环境,不过打开时报错: 未能打开位于 Z:\Vbox\Stan ...
- yii2中关联查询
yii2 ActiveRecord多表关联以及多表关联搜索的实现 一个老生常谈的问题.最近通过群里的反馈,觉得很多人还是没有去理解这个问题.今天把这个问题讲明白了,看看yii2 ActiveRecor ...
- 机器学习中的范数规则化 L0、L1与L2范数 核范数与规则项参数选择
http://blog.csdn.net/zouxy09/article/details/24971995 机器学习中的范数规则化之(一)L0.L1与L2范数 zouxy09@qq.com http: ...