Skip to content

Instantly share code, notes, and snippets.

@dchiji
Created April 3, 2010 12:52
Show Gist options
  • Save dchiji/354455 to your computer and use it in GitHub Desktop.
Save dchiji/354455 to your computer and use it in GitHub Desktop.
麻雀さんに殺されました
#include <stdio.h>
#include <string.h>
#define MODE_ATAMA 0
#define MODE_SHUNTSU 1
#define NEW_PATTERN(i, copy_n, ns, ns_size, pats, pats_i, pats_size) \
do { \
int j; \
pat_t pat; \
if(ns_size == 13) { \
pats_size++; \
pats[pats_i][0].i = 0; \
} \
for(j = 0; j < copy_n; j++) { \
pat.nums[j] = ns[i + j]; \
} \
pats[pats_i][++(pats[pats_i][0].i)] = pat; \
pattern(ns, ns_size, i, i, i+1, mode, pats, pats_i, &pats_size); \
pats_i = pats_size; \
}while(0) \
typedef struct {
int nums[3];
int i;
} pat_t;
int pattern(int *old_ns, int old_ns_size, int exception1, int exception2, int exception3, int mode, pat_t pats[][6], int pats_i, int *pats_size)
{
int ns[13];
int ns_size = old_ns_size;
int i = 0;
for(i = 0; i < ns_size; i++) {
if(i != exception1 && i != exception2 && i != exception3) {
ns[j] = old_ns[j];
j++;
} else {
ns_size--;
}
}
if(ns_size < 3) {
if(ns[i] == ns[i+1] && ns[i] != ns[i+2] && mode != MODE_ATAMA) { // ATAMA
NEW_PATTERN(i, 2, ns, ns_size, pats, pats_i, pats_size);
return pats_size;
} else {
pats[pats_i][0].i = -1;
return pats_size;
}
}
for(i = 0; i < old_ns_size - 2; i++) {
if(ns[i] == ns[i+1] && ns[i] == ns[i+2]) { // KOTSU
NEW_PATTERN(i, 3, ns, ns_size, pats, pats_i, pats_size);
}
if(ns[i] == ns[i+1] && ns[i] != ns[i+2] && mode != MODE_ATAMA) { // ATAMA
NEW_PATTERN(i, 3, ns, ns_size, pats, pats_i, pats_size);
}
if(ns[i] == ns[i+1]+1 && ns[i] == ns[i+2]+2) { // SHUNTSU
NEW_PATTERN(i, 3, ns, ns_size, pats, pats_i, pats_size);
}
if(!(ns[i] == ns[i+1] && ns[i] == ns[i+2]) ||
!(ns[i] == ns[i+1] && ns[i] != ns[i+2] && mode != MODE_ATAMA) ||
!(ns[i] == ns[i+1]+1 && ns[i] == ns[i+2]+2)) {
pats[pats_i][0].i = -1;
}
return pats_size;
}
}
int main()
{
int ns[13];
pat_t pats[1000][6];
int pats_size = 0;
int i = 0;
for(i = 0; i < 13; i++) {
ns[i] = getchar();
}
for(i = 0; i < 13; i++) {
if(ns[i] + 1 == ns[i+1] || ns[i] == ns[i+1]) {
int mode;
if(ns[i] == ns[i+1]) {
mode = MODE_ATAMA;
} else {
mode = MODE_SHUNTSU;
}
pattern(ns, 13, i, i, i+1, mode, pats, pats_size, &pats_size);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment