Last active
August 29, 2015 13:56
-
-
Save VienosNotes/8957773 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| void check(int*); | |
| int success; | |
| int main(void) { | |
| int max = 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8; | |
| int state = 0; | |
| //全ての配置を試行する | |
| while (state < max) { | |
| int board[8] = {0}; | |
| int cur = state; | |
| int i = 0; | |
| while(i < 8) { | |
| board[i] = cur % 8; | |
| cur = cur / 8; | |
| ++i; | |
| } | |
| check(board); | |
| ++state; | |
| } | |
| printf("found %d answer(s).\n", success); | |
| return 0; | |
| } | |
| void check(int* board) { | |
| //横方向に衝突するコマが存在しないかどうか | |
| int yoko[8] = {0}; | |
| int i = 0; | |
| while(i < 8) { | |
| if (yoko[board[i]]) { | |
| return; //衝突死 | |
| } else { | |
| yoko[board[i]] = 1; | |
| } | |
| ++i; | |
| } | |
| //左上,右下方面に衝突するコマが存在しないかどうか | |
| int left_upper[88] = {0}; | |
| i = 0; | |
| while(i < 8) { | |
| int yoko = i; | |
| int tate = board[i]; | |
| while (yoko * tate) { | |
| yoko = yoko - 1; | |
| tate = tate - 1; | |
| } | |
| if (left_upper[yoko * 10 + tate]) { | |
| return; //衝突死 | |
| } else { | |
| left_upper[yoko * 10 + tate] = 1; | |
| } | |
| ++i; | |
| } | |
| //左下,右上方面に衝突するコマが存在しないかどうか | |
| int right_upper[88] = {0}; | |
| i = 0; | |
| while(i < 8) { | |
| int yoko = i; | |
| int tate = 7 - board[i]; | |
| while (yoko * tate) { | |
| yoko = yoko - 1; | |
| tate = tate - 1; | |
| } | |
| if (right_upper[yoko * 10 + tate]) { | |
| return; //衝突死 | |
| } else { | |
| right_upper[yoko * 10 + tate] = 1; | |
| } | |
| ++i; | |
| } | |
| i = 0; | |
| while(i < 8) { | |
| printf("%d ", board[i++]); | |
| } | |
| printf("\n"); | |
| ++success; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment