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iYAgKHggeSk6ICZxdW90Oyk7CiAgICAgICAgc2NhbmYoJnF1b3Q7JWQgJWQmcXVvdDssICZhbXA7eCwgJmFtcDt5KTsKCiAgICAgICAgaWYgKHggJmx0OyAwIHx8IHggJmd0Oz0gU0laRSB8fCB5ICZsdDsgMCB8fCB5ICZndDs9IFNJWkUpIHsKICAgICAgICAgICAgcHJpbnRmKCZxdW90O+eEoeWKueOBquW6p+aomeOBp+OBmeOAguWGjeW6puWFpeWKm+OBl+OBpuOBj+OBoOOBleOBhOOAglxuJnF1b3Q7KTsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGlmICghaXNWYWxpZE1vdmUoYm9hcmQsIHgsIHksIGN1cnJlbnRQbGF5ZXIpKSB7CiAgICAgICAgICAgIHByaW50ZigmcXVvdDvjgZ3jga7loLTmiYDjgavjga/nva7jgZHjgb7jgZvjgpPjgIJcbiZxdW90Oyk7CiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIH0KCiAgICAgICAgcGxhY2VTdG9uZShib2FyZCwgeCwgeSwgY3VycmVudFBsYXllcik7CgogICAgICAgIC8vIOebuOaJi+OBruefs+OCkuWPluOCiumZpOOBjwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpICZsdDsgU0laRTsgaSsrKSB7CiAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqICZsdDsgU0laRTsgaisrKSB7CiAgICAgICAgICAgICAgICBpZiAoYm9hcmRbaV1bal0gIT0gRU1QVFkgJmFtcDsmYW1wOyBib2FyZFtpXVtqXSAhPSBjdXJyZW50UGxheWVyKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGlzQ2FwdHVyZWQoYm9hcmQsIGksIGopKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGJvYXJkW2ldW2pdID0gRU1QVFk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICAvLyDmiYvnlarjgpLkuqTku6MKICAgICAgICBjdXJyZW50UGxheWVyID0gKGN1cnJlbnRQbGF5ZXIgPT0gQkxBQ0spID8gV0hJVEUgOiBCTEFDSzsKICAgIH0KCiAgICByZXR1cm4gMDsKfQoKdm9pZCBpbml0aWFsaXplQm9hcmQoaW50IGJvYXJkW1NJWkVdW1NJWkVdKSB7CiAgICBmb3IgKGludCBpID0gMDsgaSAmbHQ7IFNJWkU7IGkrKykgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqICZsdDsgU0laRTsgaisrKSB7CiAgICAgICAgICAgIGJvYXJkW2ldW2pdID0gRU1QVFk7CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIHByaW50Qm9hcmQoaW50IGJvYXJkW1NJWkVdW1NJWkVdKSB7CiAgICBwcmludGYoJnF1b3Q7ICAmcXVvdDspOwogICAgZm9yIChpbnQgaSA9IDA7IGkgJmx0OyBTSVpFOyBpKyspIHsKICAgICAgICBwcmludGYoJnF1b3Q7JWQgJnF1b3Q7LCBpKTsKICAgIH0KICAgIHByaW50ZigmcXVvdDtcbiZxdW90Oyk7CgogICAgZm9yIChpbnQgaSA9IDA7IGkgJmx0OyBTSVpFOyBpKyspIHsKICAgICAgICBwcmludGYoJnF1b3Q7JWQgJnF1b3Q7LCBpKTsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiAmbHQ7IFNJWkU7IGorKykgewogICAgICAgICAgICBpZiAoYm9hcmRbaV1bal0gPT0gQkxBQ0spIHsKICAgICAgICAgICAgICAgIHByaW50ZigmcXVvdDvil48gJnF1b3Q7KTsKICAgICAgICAgICAgfSBlbHNlIGlmIChib2FyZFtpXVtqXSA9PSBXSElURSkgewogICAgICAgICAgICAgICAgcHJpbnRmKCZxdW90O+KXiyAmcXVvdDspOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcHJpbnRmKCZxdW90O+ODuyAmcXVvdDspOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHByaW50ZigmcXV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iAoYm9hcmRbeF1beV0gIT0gYm9hcmRbeF1beV0pIHJldHVybiAwOwoKICAgIHJldHVybiBjaGVja0xpYmVydGllcyhib2FyZCwgeCArIDEsIHksIHZpc2l0ZWQpIHx8CiAgICAgICAgICAgY2hlY2tMaWJlcnRpZXMoYm9hcmQsIHggLSAxLCB5LCB2aXNpdGVkKSB8fAogICAgICAgICAgIGNoZWNrTGliZXJ0aWVzKGJvYXJkLCB4LCB5ICsgMSwgdmlzaXRlZCkgfHwKICAgICAgICAgICBjaGVja0xpYmVydGllcyhib2FyZCwgeCwgeSAtIDEsIHZpc2l0ZWQpOwp9Cgp2b2lkIGNvcHlCb2FyZChpbnQgc3JjW1NJWkVdW1NJWkVdLCBpbnQgZGVzdFtTSVpFXVtTSVpFXSkgewogICAgZm9yIChpbnQgaSA9IDA7IGkgJmx0OyBTSVpFOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiAmbHQ7IFNJWkU7IGorKykgewogICAgICAgICAgICBkZXN0W2ldW2pdID0gc3JjW2ldW2pdOwogICAgICAgIH0KICAgIH0KfQo=
#include <stdio.h>
#include <stdlib.h>
#define SIZE 9
#define EMPTY 0
#define BLACK 1
#define WHITE 2
void initializeBoard(int board[SIZE][SIZE]);
void printBoard(int board[SIZE][SIZE]);
int isValidMove(int board[SIZE][SIZE], int x, int y, int color);
void placeStone(int board[SIZE][SIZE], int x, int y, int color);
int isCaptured(int board[SIZE][SIZE], int x, int y);
void copyBoard(int src[SIZE][SIZE], int dest[SIZE][SIZE]);
int checkLiberties(int board[SIZE][SIZE], int x, int y, int visited[SIZE][SIZE]);
int main() {
int board[SIZE][SIZE];
int x, y;
int currentPlayer = BLACK;
initializeBoard(board);
while (1) {
printBoard(board);
printf("現在の手番: %s\n", (currentPlayer == BLACK) ? "黒" : "白");
printf("石を置く場所 (x y): ");
scanf("%d %d", &x, &y);
if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
printf("無効な座標です。再度入力してください。\n");
continue;
}
if (!isValidMove(board, x, y, currentPlayer)) {
printf("その場所には置けません。\n");
continue;
}
placeStone(board, x, y, currentPlayer);
// 相手の石を取り除く
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] != EMPTY && board[i][j] != currentPlayer) {
if (isCaptured(board, i, j)) {
board[i][j] = EMPTY;
}
}
}
}
// 手番を交代
currentPlayer = (currentPlayer == BLACK) ? WHITE : BLACK;
}
return 0;
}
void initializeBoard(int board[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
void printBoard(int board[SIZE][SIZE]) {
printf(" ");
for (int i = 0; i < SIZE; i++) {
printf("%d ", i);
}
printf("\n");
for (int i = 0; i < SIZE; i++) {
printf("%d ", i);
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == BLACK) {
printf("● ");
} else if (board[i][j] == WHITE) {
printf("○ ");
} else {
printf("・ ");
}
}
printf("\n");
}
}
int isValidMove(int board[SIZE][SIZE], int x, int y, int color) {
if (board[x][y] != EMPTY) return 0;
int tempBoard[SIZE][SIZE];
copyBoard(board, tempBoard);
tempBoard[x][y] = color;
// 自殺手判定
if (isCaptured(tempBoard, x, y)) {
return 0;
}
return 1;
}
void placeStone(int board[SIZE][SIZE], int x, int y, int color) {
board[x][y] = color;
}
int isCaptured(int board[SIZE][SIZE], int x, int y) {
int visited[SIZE][SIZE] = {0};
return !checkLiberties(board, x, y, visited);
}
int checkLiberties(int board[SIZE][SIZE], int x, int y, int visited[SIZE][SIZE]) {
if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) return 0;
if (visited[x][y]) return 0;
visited[x][y] = 1;
if (board[x][y] == EMPTY) return 1;
if (board[x][y] != board[x][y]) return 0;
return checkLiberties(board, x + 1, y, visited) ||
checkLiberties(board, x - 1, y, visited) ||
checkLiberties(board, x, y + 1, visited) ||
checkLiberties(board, x, y - 1, visited);
}
void copyBoard(int src[SIZE][SIZE], int dest[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
dest[i][j] = src[i][j];
}
}
}