UVa 127 - "Accordian" Patience
1题目
===================
``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, itmay 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
===================
2思路
题目大意:这是一个模拟纸牌移动的游戏,纸牌用两个字母xy表示。首先定义了match,如果两张牌ab,cd中a==c 或者b==d,那么这 两张牌就match。其次定义了移动规则,如果一张牌和它左边第一个或者左边第三个match,那么这张牌要移动到相应位置顶部, 如果两个都match,则移动到左边第三个上。另外,需要注意,移动之后,牌的布局会发生变化,刚刚移动的牌可能和其它牌再次 match,例如和它前面的牌或者后面的牌match。
思路:1)刚开始我想用数组直接模拟,用数字大小标志当前位置有几张牌,后来发现思维量太大,控制不了,这时候才体会 到数据结构的重要性,使用数据结构后,会简化思维过程,把整个处理过程抽象成了我们大脑可控的模式。2)思考整个过程 可以发现有两种基本操作,一是从当前位置顶部拿走一张牌,二是把一张牌放在一个位置的顶部。这两个操作正好对应了数据 结构中栈的出栈和入栈操作。将数据结构和操作定义好后,模拟的过程就不难了。
实现:在实现上我没有使用栈,直接使用了ANSI C,定义了链表数据结构,所有数据操作都由自己实现。AC之后我去搜了一下 其它人的解题报告,大部分人都是用C++实现的,使用了库里的vector或者stack等等,但是用时比较长,搜了几个都在1s左右。 我最终AC的时间是0.092s。使用C++优点是代码比较短,如果要是比赛,还是用C++比较方便。我今后也会用C++实现, 现在刚开始练,就当是数据结构和指针操作的练手吧。
细节:注意当结果为1时输出pile,大于1输出piles.
3代码
/*
* Problem: UVa 127 - "Accordian" Patience
* Lang: ANSI C
* Time: 0.092
* Author: minix
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 52
#define M 3
typedef struct _Node {
int num;
char info[M];
struct _Node *left;
struct _Node *right;
struct _Node *down;
}Node;
typedef struct _List {
Node * head;
Node * tail;
int num;
}List;
/* delete node, as pop in stack */
Node * delete (List *list, Node *node) {
Node *left = node->left;
Node *right = node->right;
Node *down = node->down;
if (node == NULL) return NULL;
if (right == NULL)
list->tail = left;
if (down == NULL) {
if (left != NULL)
left->right = right;
if (right != NULL)
right->left = left;
list->num -= 1;
} else {
if (left != NULL)
left->right = down;
if (right != NULL)
right->left = down;
down->left = left;
down->right = right;
}
return node;
}
/* add node, as push in stack */
Node * add_top (Node *des, Node *src) {
Node *left = des->left;
Node *right = des->right;
if (des == NULL || src == NULL)
return NULL;
if (left != NULL)
left->right = src;
if (right != NULL)
right->left = src;
src->left = left;
src->right = right;
src->down = des;
return src;
}
/* generate new node */
Node * new_node (char cards[M]) {
Node * node = (Node *)malloc(sizeof(Node));
memset (node->info, 0, sizeof(node->info[0])*M);
strcpy (node->info, cards);
node->left = node->right = node->down = NULL;
node->num = 1;
return node;
}
/* generate new list */
List * new_list () {
List * list = (List *)malloc(sizeof(List));
Node * node = (Node *)malloc(sizeof(Node));
list->head = list->tail = node;
list->num = 0;
return list;
}
/* add node to list */
Node * add_to_list (List *list, Node *node) {
if (list == NULL || node == NULL)
return NULL;
list->tail->right = node;
node->left = list->tail;
list->tail = node;
list->num += 1;
return node;
}
/* output list, use to test */
void output_list (List *list) {
Node * node = list->head->right;
Node * down = NULL;
while (node != NULL) {
printf ("%s ", node->info);
down = node->down;
while (down != NULL) {
printf ("%s ", down->info);
down = down->down;
}
printf ("\n");
node = node->right;
}
printf ("\n");
}
/* free list */
void free_list (List *list) {
Node * node = list->head->right;
Node * next = node;
while (node != NULL) {
next = node->right;
free (node);
node = next;
}
list->tail = list->head;
list->num = 0;
}
/* determine whether match or not */
int match (Node *na, Node *nb) {
if (na->info[0] == nb->info[0] ||
na->info[1] == nb->info[1])
return 1;
else
return 0;
}
/* find pos of the node which is first match with currrent node */
Node * find_first_match (List *list, Node *node, Node *start) {
if (list == NULL || node == NULL)
return NULL;
Node *head = list->head;
if (start->left!=head \
&& start->left->left!= head \
&& start->left->left->left != head \
&& match (node, start->left->left->left)
)
return start->left->left->left;
if (start->left != head \
&& match (node, start->left)
)
return start->left;
return NULL;
}
/* find the final match node of current node */
Node * find_final_match (List *list, Node *node) {
Node * match_node = find_first_match (list, node, node);
Node * last_match_node = NULL;
while (match_node != NULL) {
last_match_node = match_node;
match_node = find_first_match (list, node, last_match_node);
}
return last_match_node;
}
/* solutions at top level */
void solve (List *list) {
Node *node = list->head->right;
Node *match_node = NULL;
while (node != NULL) {
match_node = find_final_match (list, node);
if (match_node != NULL) {
delete (list, node);
add_top (match_node, node);
node->num = match_node->num + 1;
node = node->right;
} else {
node = node->right;
}
}
}
/* output result */
void output_result (List *list) {
Node * node = list->head->right;
if (list->num > 1)
printf ("%d piles remaining:", list->num);
else
printf ("%d pile remaining:", list->num);
while (node != NULL) {
printf (" %d", node->num);
node = node->right;
}
printf ("\n");
}
int main() {
int i;
char card[M];
Node *node;
List *list = new_list();
while (scanf ("%s", card) != EOF) {
if (!strcmp (card, "#")) break;
node = new_node (card);
add_to_list (list, node);
for (i=0; i<N-1; i++) {
scanf ("%s", card);
node = new_node (card);
add_to_list (list, node);
}
solve (list);
output_result (list);
/* output_list (list); */
free_list (list);
}
return 0;
}