//술취한 영구벌레가 모든 타일을 지나갔을때의 이동횟수 구하기
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 21
int x = SIZE / 2, y = SIZE / 2;
int cnt = 0;
//전체타일 검사하는 함수
int is_tile_full(int t[][SIZE]) {//참1 거짓0
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
if (t[i][j] == 0)
return 0;
}
}
return 1;
}
//이동하는 함수 : 타일 위치를 받아서 1로바꾸고 타일위치 반환
void moving(int t[][SIZE]) {
int n = rand() % 8;
// t[x][y];
//if (x < SIZE && x > 0 && y > 0 && y < SIZE) {//타일 안에서
//case 0: if(y!=0){t[x][y++]; break;}else break;
switch (n) {
case 0:
if (y < SIZE - 1) {
t[x][y++] = 1;
cnt++;
}
break;
case 1:
if (x < SIZE - 1 && y < SIZE - 1) {
t[x++][y++] = 1;
cnt++;
}
break;
case 2:
if (x < SIZE - 1) {
t[x++][y] = 1;
cnt++;
}
break;
case 3:
if (x < SIZE - 1 && y > 0) {
t[x++][y--] = 1;
cnt++;
}
break;
case 4:
if (y > 0) {
t[x][y--] = 1;
cnt++;
}
break;
case 5:
if (x > 0 && y > 0) {
t[x--][y--] = 1;
cnt++;
}
break;
case 6:
if (x > 0) {
t[x--][y] = 1;
cnt++;
}
break;
case 7:
if (x > 0 && y < SIZE - 1) {
t[x--][y++] = 1;
cnt++;
}
break;
default:
break;
}
}
int main(void) {
int tile[SIZE][SIZE] = { 0 };
srand((unsigned)time(NULL));
tile[x][y] = 1;
while (is_tile_full(tile) == 0) {//모든타일을 돌았는지 검사
if (cnt <= 100000) {//최대한도 제어
moving(tile);//랜덤으로 움직이기
//printf("(%d, %d)\n", x,y);
}
else {
printf("한도횟수 초과!\n");
return 0;
}
}
printf("영구벌레는 %d번 이동하여 모든 타일을 지나갔습니다.\n", cnt);
}