baihacker baihacker
关注数: 2 粉丝数: 12 发帖数: 493 关注贴吧数: 3
麻将糊牌算法(如果有C语言问题请进) 顺便广告一下 www . supcoder . cn (把空格去掉)欢迎来交流学习,我的两个QQ群都满了,不好意思.题目有个bug:一张牌可以是4张或4张以上(我提交的时候把牌限制在4张以内,所以出了问题^_^)The 30th ACM/ICPC regional contest Chengdu site has been held at Chengdu on November 5 & 6, 2005. As is known to all, Chengdu is a beautiful city. In this city, people lead a leisure life. In their spare time, they like to play a kind of card game - Mahjong. This kind of game is complicated, but now we will simplify the rules. There is only one type of cards, which are numbered from 1 to 9. Everyone has 13 cards, and the total number of same card are exactly 4. At the beginning, every player get 13 cards. If one wants to win this game, he needs to get one more card. The permutation of the 14 cards should match the following rules. First, we define two cards with the same number as pattern A, three cards with the same number as pattern B, and three cards whose numbers continuously increase as pattern C. The 14 cards should be divided into following pattern combinations:1. A B B B B2. A B B B C3. A B B C C4. A B C C C5. A C C C CIn each combination, the order of the patterns is arbitrary. For example, suppose you have owned the following cards: 1 2 3 2 2 2 3 4 5 6 7 8 9and if you get another card with number 9, you will win. (These cards can be described as: C B C C A) Your program must, given the current 13 cards, determine which card is needed to win the game. If there are several solutions, list them in increase order. Input The problem has multiple cases. For each case, there is only one line. The line contains the number of the 13 cards. The numbers are in the range from 1 to 9 each, separated by spaces. Process to the end of file. Output Output must contain an ordered list of card number(s), each of which is needed to win the game, separated by spaces. If there is no such card, print "no solution" without the quote. Sample Input 1 1 1 2 2 2 4 4 4 5 5 5 91 1 1 2 2 2 4 4 4 5 5 6 9Sample Output 9no solution#include void insert(int* a, int y, int n){ int i = n - 1; while (i >= 0 && y < a[i]) { a[i + 1] = a[i]; i--; } a[i + 1] = y;} void sort(int* a, int n){ int i; for (i = 1; i < n; i++) insert(a, a[i], i);}int mark[15], ia, ib, isolved;/*ia,对子数,ib三连数,isolved,解决方案数*/int find(int* a, int n, int b){ int i = 0; for (i = 0; i < n; i++) if (a[i] == b && mark[i] == 0) return i; return -1;}void isvalid1(int* a, int n){ int i, j, k, l; if (isolved == 1) return; i = 0; while (mark[i++] == 1); i--;/*i指向第一个未检测的牌*/ if (i == 14) { if (ia == 1 && ib == 4) isolved = 1; return; } j = i + 1; while (j < n && a[j] == a[i]) j++; k = j - i; if ( k >= 3)/*有三个牌及以上相同*/ { ib++;/*前三个牌看作三连*/ for (l = 0; l < 3; l++) mark[i + l] = 1; isvalid1(a, n); ib--; for (l = 0; l < 3; l++) mark[i + l] = 0; ia++;/*前两个牌看作一对*/ for (l = 0; l < 2; l++) mark[i + l] = 1; isvalid1(a, n); ia--; for (l = 0; l < 2; l++) mark[i + l] = 0; } if (k == 2) { int m1, m2; ia++;/*前两个牌看作一对*/ for (l = 0; l < 2; l++) mark[i + l] = 1; isvalid1(a, n); ia--; for (l = 0; l < 2; l++) mark[i + l] = 0; if ((m1 = find(a, n, a[i] + 1)) != -1 && (m2 = find(a, n, a[i] + 2)) != -1) { ib++; mark[i] = mark[m1] = mark[m2] = 1; isvalid1(a, n); ib--; mark[i] = mark[m1] = mark[m2] = 0; } } if (k == 1) { int m1, m2; if ((m1 = find(a, n, a[i] + 1)) != -1 && (m2 = find(a, n, a[i] + 2)) != -1) { ib++; mark[i] = mark[m1] = mark[m2] = 1; isvalid1(a, n); ib--; mark[i] = mark[m1] = mark[m2] = 0; } }}void isvalid(int* a, int n){ int i; sort(a, n); for (i = 0; i <= n; i++) mark[i] = 0; ia = ib = isolved = 0; isvalid1(a, n);}int main(){ int i, j, k; int a[14]; int temp[14]; int key[10]; while (1) { for (i = 0; i < 13; i++) if (scanf("%d", &a[i]) != 1) goto end; k = 0; for (i = 1; i <= 9; i++) { for (j = 0; j < 13; j++) temp[j] = a[j]; temp[13] = i; isvalid(temp, 14); if (isolved != 0) key[k++] = i; } if (k == 0) printf("no solution\n"); else { for (i = 0; i < k - 1; i++) printf("%d ", key[i]); printf("%d\n", key[k - 1]); } } end:;}
首页 1 2 下一页