ACM学习历程——UVA127 "Accordian" Patience(栈, 链表)
Description
``Accordian'' Patience |
You are to simulate the playing of games of ``Accordian'' patience, the rules for which are as follows:
Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate neighbour on the left, or matches the third card to the left, it may be moved onto that card. Cards match if they are of the same suit or same rank. After making a move, look to see if it has made additional moves possible. Only the top card of each pile may be moved at any given time. Gaps between piles should be closed up as soon as they appear by moving all piles on the right of the gap one position to the left. Deal out the whole pack, combining cards towards the left whenever possible. The game is won if the pack is reduced to a single pile.
Situations can arise where more than one play is possible. Where two cards may be moved, you should adopt the strategy of always moving the leftmost card possible. Where a card may be moved either one position to the left or three positions to the left, move it three positions.
Input
Input data to the program specifies the order in which cards are dealt from the pack. The input contains pairs of lines, each line containing 26 cards separated by single space characters. The final line of the input file contains a # as its first character. Cards are represented as a two character code. The first character is the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen, K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades).
Output
One line of output must be produced for each pair of lines (that between them describe a pack of 52 cards) in the input. Each line of output shows the number of cards in each of the piles remaining after playing ``Accordian patience'' with the pack of cards as described by the corresponding pairs of input lines.
Sample Input
QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
#
Sample Output
6 piles remaining: 40 8 1 1 1 1
1 pile remaining: 52 根据题目意思,两种操作,固然可以看出每个牌堆需要靠栈来实现,故结构体来存放,内部有一个数组成员,可以开52。
然而,对于牌堆来说,似乎要支持访问后一个和前一个牌堆两种操作,故可以使用顺序表,然后访问时忽略0牌堆。
也可以使用双向链表。
下面贴两种方式的代码: 双向链表:
用时432MS:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define eps 1e-10 using namespace std; struct node
{
char Stack[52][2];
int top;
node *pre;
node *next;
}; node *head;
int num; void Del(node *p)
{
node *t;
t = p->pre;
t->next = p->next;
if (p->next != NULL)
p->next->pre = t;
free(p);
} bool Input()
{
num = 0;
head = (node *)malloc(sizeof(node));
node *p = head, *t;
char ch;
ch = getchar();
if (ch == '#')
return 0;
p->Stack[0][0] = ch;
ch = getchar();
p->Stack[0][1] = ch;
p->top = 1;
p->pre = NULL;
p->next = NULL;
num++;
getchar();
for (int i = 0; i < 51; ++i)
{
p->next = (node *)malloc(sizeof(node));
t = p;
p = p->next;
ch = getchar();
p->Stack[0][0] = ch;
ch = getchar();
p->Stack[0][1] = ch;
p->top = 1;
p->next = NULL;
p->pre = t;
num++;
getchar();
}
return 1;
} void Output()
{
if (num == 1)
{
printf("1 pile remaining: %d\n", head->top);
return;
}
printf("%d piles remaining:", num);
node *p = head;
for (;;)
{
printf(" %d", p->top);
if (p->next == NULL)
break;
p = p->next;
}
printf("\n");
} bool Do()
{
node *p, *t;
p = head;
for (;;)
{
t = p;
for (int i = 0; t != NULL && i < 3; ++i)
t = t->pre;
if (t != NULL &&
(t->Stack[t->top-1][0] == p->Stack[p->top-1][0] ||
t->Stack[t->top-1][1] == p->Stack[p->top-1][1]))
{
t->Stack[t->top][0] = p->Stack[p->top-1][0];
t->Stack[t->top][1] = p->Stack[p->top-1][1];
t->top++;
p->top--;
if (p->top == 0)
{
Del(p);
num--;
}
return 1;
}
t = p->pre;
if (t != NULL &&
(t->Stack[t->top-1][0] == p->Stack[p->top-1][0] ||
t->Stack[t->top-1][1] == p->Stack[p->top-1][1]))
{
t->Stack[t->top][0] = p->Stack[p->top-1][0];
t->Stack[t->top][1] = p->Stack[p->top-1][1];
t->top++;
p->top--;
if (p->top == 0)
{
Del(p);
num--;
}
return 1;
}
if (p->next == NULL)
break;
p = p->next;
}
return 0;
} int main()
{
//freopen("test.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while (Input())
{
while (Do());
Output();
}
return 0;
}
顺序表:
用时:879MS
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define esp 1e-10
#define N 100005 using namespace std; struct node
{
int top;
char card[53][3];
}s[52]; bool Input()
{
scanf ("%s", s[0].card[0]);
if (s[0].card[0][0] == '#')
return 0;
s[0].top = 1;
for (int i = 1; i < 52; ++i)
{
s[i].top = 1;
scanf ("%s", s[i].card[0]);
}
return 1;
} void Output()
{
queue <int> q;
for (int i = 0; i < 52; ++i)
if (s[i].top != 0)
q.push(i);
int sum = q.size();
if (sum == 1)
printf ("1 pile remaining: %d\n", s[q.front()].top);
else
{
printf ("%d piles remaining:", sum);
int k;
while (!q.empty())
{
k = q.front();
q.pop();
printf (" %d", s[k].top);
}
printf ("\n");
}
} void qt()
{
int p = 0;
for (;;)
{
if (p == 52)
break;
if (s[p].top != 0)
{
int one = -1, two = -1;
int flag = 0, j = p-1;
for (;;)
{
if (j < 0)
break;
if (s[j].top != 0)
{
flag++;
if (flag == 3)
{
one = j;
break;
}
if (flag == 1)
two = j;
}
j--;
}
if (one != -1)
{
if (s[p].card[s[p].top-1][0] == s[one].card[s[one].top-1][0] ||
s[p].card[s[p].top-1][1] == s[one].card[s[one].top-1][1])
{
strcpy (s[one].card[s[one].top], s[p].card[s[p].top-1]);
s[p].top--;
s[one].top++;
p = 0;
continue;
}
}
if (two != -1)
{
if (s[p].card[s[p].top-1][0] == s[two].card[s[two].top-1][0] ||
s[p].card[s[p].top-1][1] == s[two].card[s[two].top-1][1])
{
strcpy (s[two].card[s[two].top], s[p].card[s[p].top-1]);
s[p].top--;
s[two].top++;
p = 0;
continue;
}
}
}
++p;
}
} int main()
{
//freopen ("test.txt", "r", stdin);
while (Input())
{
qt();
Output();
}
return 0;
}
ACM学习历程——UVA127 "Accordian" Patience(栈, 链表)的更多相关文章
- ACM学习历程——UVA11234 Expressions(栈,队列,树的遍历,后序遍历,bfs)
Description Problem E: Expressions2007/2008 ACM International Collegiate Programming Contest Unive ...
- [刷题]算法竞赛入门经典(第2版) 6-9/UVa127 - "Accordian" Patience
题意:52张牌排一行,一旦出现任何一张牌与它左边的第一张或第三张"匹配",即花色或点数相同,则须立即将其移动到那张牌上面,将其覆盖.能执行以上移动的只有压在最上面的牌.直到最后没有 ...
- ACM学习历程——UVA 127 "Accordian" Patience(栈;模拟)
Description ``Accordian'' Patience You are to simulate the playing of games of ``Accordian'' patie ...
- ACM学习历程——UVA11111 Generalized Matrioshkas(栈)
Description Problem B - Generalized Matrioshkas Problem B - Generalized Matrioshkas Vladimir wo ...
- ACM学习历程——UVA442 Matrix Chain Multiplication(栈)
Description Matrix Chain Multiplication Matrix Chain Multiplication Suppose you have to evaluate ...
- ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)
Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—HDU5521 Meeting(图论)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...
随机推荐
- MySQL的安装过程
近期对MySQL做了一些研究. 曾经主要接触的是SQL SERVER.所以,今天对该安装过程做了一些总结以及使用过程中的一些心得.并分享给大家. 记得前面.分享过一篇关于数据库的几种连接方式.而 ...
- [python学习] 简单爬取图片站点图库中图片
近期老师让学习Python与维基百科相关的知识,无聊之中用Python简单做了个爬取"游讯网图库"中的图片,由于每次点击下一张感觉很浪费时间又繁琐.主要分享的是怎样爬取HTML的知 ...
- linux 时间格式
版权为个人所有,欢迎转载如转载请说明出处.(东北大亨) http://www.cnblogs.com/northeastTycoon/p/5511718.html 时间域 % H 小时(00..23) ...
- Android 红色小圆球提示气泡 BadgeView
今天给大家分享两个实用有简单的一个小圆球提示气泡: BadgeView 参考地址: https://github.com/qstumn/BadgeView; 个人地址:http://git ...
- delphi 颜色 引用http://www.cnblogs.com/del/archive/2008/02/19/1073568.html
颜色名称 颜色效果 Hex HTML clBlack $000000 #000000 clMaroon $000080 #800000 clGreen $008000 #00800 ...
- Altium Designer 敷铜间距设置,真实有效
在任一PCB视图时,点击设计->规则,弹出规则设置对话框,如下图 找到Clearance,如下图, 使用右键单击,选择 新规则,如下图 在新规则上单击,在右侧 where the first ...
- android菜鸟学习笔记14----Android控件(三) ListView的简单使用
MVC模式: MVC的基本原理就是通过Controller连接View和Model.当View中所显示的数据发生变化时,会通知Controller,然后由Controller调用Model中的相关方法 ...
- HTML5+ 权限设置
API分模块封装调用了系统各种原生能力,而部分能力需要使用到Android的permissions,以下列出了各模块(或具体API)使用的的权限: -------------------------- ...
- 【题解】P1407国家集训队稳定婚姻
[题解][P1407 国家集训队]稳定婚姻 很好的一道建模+图论题. 婚姻关系?很像二分图匹配呀,不过不管怎么办先建模再说.婚姻关系显然用图方面的知识解决.建图! 它给定的是字符串,所以我们使用\(a ...
- 我的Java开发学习之旅------>使用循环递归算法把数组里数据数组合全部列出
面试题如下:把一个数组里的数组合全部列出,比如1和2列出来为1,2,12,21. (面试题出自<Java程序员面试宝典>) 代码如下: import java.util.Arrays; i ...