/*--------------------------*/ /* 9 Cards Answer Checker */ /*--------------------------*/ /* カード情報を入力するには 4桁の数字でカード1枚を表現します。 △マークの頂点側を偶数、底辺側を奇数とし、 紫色→0,1 緑色→2,3 水色→4,5 黄色→6,7 のいずれかを、 カードの4辺について時計回りに入力します。 その4桁の数字を9枚分、隙間無く記述した36文字の数列で 9枚のカードを表現します。 How To Enter Pattern: The state of a card is expressed by 4 digits. Even number means the peak side of the triangle mark, and odd number means the bottom side of the triangle mark. Purple -> 0 or 1, Green -> 2 or 3, Light Blue -> 4 or 5, and Yellow -> 6 or 7, check 4 sides of a card clockwise. You input 4 digits 9 times without space. So, the length of the input strings must be 36. */ #include #include #define TRUE 1 #define FALSE 0 #define SIZE 3 #define CARDS (SIZE * SIZE) #define MARKS (CARDS * 4) #define LINE (SIZE * 4) void init_search(void); void exec_search(int pos); int put_card(int pos, int num, int rot); void draw_all_cards(char card[]); void draw_cards(char card[]); void draw_mark1(char card[]); void draw_mark2(char card[]); void draw_frame(void); char orig[MARKS]; char work[MARKS]; int use[CARDS]; int find; int calc; /* Main Function */ int main() { char buff[256]; do { printf("Enter Pattern : "); gets(buff); memcpy(orig, buff, MARKS); draw_all_cards(orig); printf("Is This OK? (y/n) "); gets(buff); } while (buff[0] != 'y' && buff[0] != 'Y'); init_search(); exec_search(0); if (find) { printf("\nFind The Answer!! (Counts of Calculation = %d)\n", calc); draw_all_cards(work); } return 0; } /* Search Functions */ void init_search(void) { int i; for (i = 0; i < CARDS; i++) { use[i] = FALSE; } find = FALSE; calc = 0; return; } void exec_search(int pos) { int i, j; for (i = 0; i < CARDS && !find; i++) { if (!use[i]) { use[i] = TRUE; for (j = 0; j < 4 && !find; j++) { calc++; if (put_card(pos, i, j)) { if (pos + 1 < CARDS) { exec_search(pos + 1); } else { find = TRUE; } } } use[i] = FALSE; } } return; } int put_card(int pos, int num, int rot) { int i, ret; for (i = 0; i < 4; i++) { work[pos * 4 + i] = orig[num * 4 + rot]; rot = (rot + 1) & 3; } ret = TRUE; if (pos % SIZE > 0 && (work[pos * 4 + 2] ^ work[pos * 4 - 4]) != 1) { ret = FALSE; } else if (pos / SIZE > 0 && (work[pos * 4 + 3] ^ work[pos * 4 - LINE + 1]) != 1) { ret = FALSE; } return ret; } /* Print Functions */ void draw_all_cards(char card[]) { int i; for (i = 0; i < SIZE; i++) { draw_cards(&(card[i * LINE])); } draw_frame(); return; } void draw_cards(char card[]) { draw_frame(); draw_mark1(&(card[3])); draw_mark2(card); draw_mark1(&(card[1])); return; } void draw_mark1(char card[]) { int i; for (i = 0; i < SIZE; i++) { printf("| %c ", card[i * 4]); } printf("|\n"); return; } void draw_mark2(char card[]) { int i; for (i = 0; i < SIZE; i++) { printf("|%c %c", card[i * 4 + 2], card[i * 4]); } printf("|\n"); return; } void draw_frame(void) { int i; for (i = 0; i < SIZE; i++) { printf("+---"); } printf("+\n"); return; }