UVa 127 纸牌游戏(栈)
2024-09-07 03:33:41
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 ;
}
最新文章
- 4. SVM分类器求解(2)
- 音乐播放器 EasyMusic (一)
- Java多线程编程核心技术---对象及变量的并发访问(二)
- Ruby--strftime
- 第一零四天上课 PHP TP框架下的文件上传
- Customize the SharePoint 2013 search experience with a Content Enrichment web service
- JavaScript 兼容 Array.prototype.slice.call
- cogs 2507 零食店
- Android项目文件结构
- (转)Elasticsearch 5 Ik+pinyin分词配置详解
- 阻止微信浏览器/QQ浏览器长按弹框“在浏览器打开”
- win10被微软流氓更新后编译基于visual Studio的web项目报[ArgumentOutOfRangeException: 指定的参数已超出有效值的范围
- RobotFramework和Eclipse集成-安装和使用说明
- python语法_json_pickle
- hdu-1728(贪心&;&;bfs的灵活运用吧)
- MyBatis-Plus 3.0.3 Sql注入器添加,即全局配置Sql注入器,sqlInjector改写
- 外网访问局域网ip的方法
- MySQL 各级别事务的实现机制
- opencv学习之路(3)、批量读取图片、视频分解、视频合成
- 阻止事件冒泡,阻止默认事件,event.stopPropagation()和event.preventDefault(),return false的区别
热门文章
- vue.js常用的
- 万恶之源 - Python数据类型二
- sdut2193救基友记3(三维)
- PAT 1016 Phone Bills[转载]
- 主成分分析(PCA)算法,K-L变换 角度
- Leetcode: Longest Consecutive Sequence &;&; Summary: Iterator用法以及ConcurrentModificationException错误说明
- DOM EVENT
- php 5.0 新字符串
- ngxin开启rewrite伪静态
- zw版【转发&#183;台湾nvp系列Delphi例程】HALCON MirrorImage