~cpp #include <stdio.h> #define MAX 7 #define GO 0 #define BLOCKED 1 #define HADGONE 2 int board[MAX][MAX]; int xPos[8] = {1, 1, 0, -1, -1, -1, 0, 1}; int yPos[8] = {0, 1, 1, 1, 0, -1, -1, -1}; int nextX, nextY; int x = 1; int y = 1; int count = 0; int count2 = 0; void check() { int i; count = 0; count2 = 0; for(i = 0; i < 8; i++) { if(board[y+yPos[i]][x+xPos[i]] == GO) { count++; nextX = x+xPos[i]; nextY = y+yPos[i]; } if(board[y+yPos[i]][x+xPos[i]] == HADGONE) { count2++; } } } void move() { int i; // 주위에 0이 하나있고 2는 없어. if (count == 1 && count2 == 0) { board[y][x] = 1; x = nextX; y = nextY; }// 주위에 0은 하나 있고 2가 있어. else if (count == 1 && count2 >= 1) { board[y][x] = 2; x = nextX; y = nextY; }// 주위에 0이 여러개. 현재 좌표는 2. else if (count >= 2) { board[y][x] = 2; for(i = 0; i < 8; i++) { if(board[y+yPos[i]][x+xPos[i]] == GO) { x = x+xPos[i]; y = y+yPos[i]; break; } } } } void main() { int i, j; for(i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { board[i][j] = 1; } } board[1][1] = 0; board[1][3] = 0; board[1][4] = 0; board[1][5] = 0; board[2][2] = 0; board[2][3] = 0; board[2][4] = 0; board[3][1] = 0; board[3][4] = 0; board[3][5] = 0; board[4][3] = 0; board[5][4] = 0; board[5][5] = 0; printf("현재\n(%d,%d)\n", x, y); while(!(x == 5 && y == 5)) { check(); move(); printf("(%d,%d)\n", x, y); } for(i = 0; i < MAX; i++) { for (j = 0; j < MAX; j++) { printf("%d ", board[i][j]); } printf("\n"); } }