#include <stdio.h>
#define SIZEY 20
#define SIZEX 16
#define STARTY 0
#define STARTX 1
#define GOALY 19
#define GOALX 6
void print(int maze[][SIZEX], int step)
{
char *marks[] = {"■", " ", "・"};
int y, x;
for(y = 0; y < SIZEY; y ++){
for(x = 0; x < SIZEX; x ++){
printf("%s", marks[maze[y][x]]);
}
putchar('\n');
}
printf("Steps : %d\n\n", step);
return;
}
void search(int maze[][SIZEX], int y, int x, int *step, int *shortest)
{
if(*step >= *shortest) return;
if(y < 0 || SIZEY <= y) return;
if(x < 0 || SIZEX <= x) return;
if(maze[y][x] != 1) return;
maze[y][x] = 2;
if(y == GOALY && x == GOALX){
print(maze, *shortest);
*shortest = *step;
maze[y][x] = 1;
return;
}
(*step) ++;
search(maze, y, x + 1, step, shortest);
search(maze, y + 1, x, step, shortest);
search(maze, y, x - 1, step, shortest);
search(maze, y - 1, x, step, shortest);
maze[y][x] = 1;
(*step) --;
return;
}
int main(void)
{
int maze[SIZEY][SIZEX] = {{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,},
{0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0,},
{0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0,},
{0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0,},
{0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,},
{0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0,},
{0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
{0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,},
{0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,},
{0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,},
{0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0,},
{0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0,},
{0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0,},
{0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,},};
int step = 0, shortest = SIZEX * SIZEY;
print(maze, shortest);
search(maze, STARTY, STARTX, &step, &shortest);
return 0;
}
お礼
ありがとうございます。 迷路を自動生成させ、待ち時間と無限ループをするように変えてみました。 確かにこれはインパクトあります。感謝です。