This repository has been archived by the owner on Feb 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparrow.c
67 lines (62 loc) · 1.38 KB
/
sparrow.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "sparrow.h"
// string map
const char* mahjong[34] = {
"1T", "2T", "3T", "4T", "5T", "6T", "7T", "8T", "9T",
"1S", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S",
"1W", "2W", "3W", "4W", "5W", "6W", "7W", "8W", "9W",
"DONG", "NAN", "XI", "BEI",
"ZHONG", "FA", "BAI"
};
// count for each mahjong
int cnt[34];
int convert(const char *s) {
int i;
for (i = 0; i < 34; ++i)
if (s == mahjong[i]) return i;
return -1;
}
int search(int dep, int type) {
int i;
for (i = 0; i < 34; ++i)
// triplet
if (cnt[i] >= 3) {
if (dep == type/3 - 1) return 1;
cnt[i] -= 3;
if (search(dep + 1, type)) return 1;
cnt[i] += 3;
}
for (i = 0; i <= 24; ++i)
if (i % 9 <= 6 && cnt[i] > 0 && cnt[i + 1] > 0 && cnt[i + 2] > 0) {
// sequence
if (dep == type/3 - 1) return 1;
--cnt[i], --cnt[i + 1], --cnt[i + 2];
if (search(dep + 1, type)) return 1;
++cnt[i], ++cnt[i + 1], ++cnt[i + 2];
}
return 0;
}
int check(int type) {
int i;
for (i = 0; i < 34; ++i) {
// eye
if (cnt[i] >= 2) {
cnt[i] -= 2;
if (search(0, type)) return 1;
cnt[i] += 2;
}
}
return 0;
}
int test(const int *mj, int *list, int size) {
int i, j, count = 0;
for (i = 0; i < 34; ++i) {
memset(cnt, 0, sizeof(cnt));
for (j = 0; j < size; ++j) ++cnt[mj[j]];
// kong
if (cnt[i] >= 4) continue;
++cnt[i];
if (check(size)) list[count++] = i;
--cnt[i];
}
return count;
}