//建立迷宮
voidCreateMaze(intx,inty)
{
//定義4個方向
intdir[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 };//回答錯誤
//打亂方向
intj,temp;//temp 臨時變數
for(inti = 0; i < 4; i++)
{
j =rand() % 4;//0-3 數組的下標 //j隨機之後 j同一次還會不會變
temp = dir[i][0];
dir[i][0] = dir[j][0];
dir[j][0] = temp;
temp = dir[i][1];
dir[i][1] = dir[j][1];
dir[j][1] = temp;
}
map[x][y] = ROAD;//把這個點賦值成路
for(inti = 0; i < 4; i++)
{
if(map[x + 2 * dir[i][0]][y + 2 * dir[i][1]] == WALL)//向上 0 1 //隔壁的一個
{
map[x + dir[i][0]][y + dir[i][1]] = ROAD;
CreateMaze(x + 2*dir[i][0], y + 2*dir[i][1]);
}
}
}
intmain()
{
srand((unsignedint)time(NULL));
for(inti = 0; i <= HEIGHT+1; i++)
{
for(intj = 0; j <= WIDTH + 1; j++)
{
if(i == 0 || i == HEIGHT + 1 || j == 0 || j == WIDTH + 1)
{
map[i][j] = ROAD;
}
else
{
map[i][j] = WALL;
}
}
}
CreateMaze(2 * (rand() % (HEIGHT / 2) + 1), 2 * (rand() % (WIDTH / 2) + 1));
//邊界
for(inti = 0; i <= HEIGHT + 1; i++)
{
map[i][0] = WALL;
map[i][WIDTH + 1] = WALL;
}
for(inti = 0; i <=WIDTH+1; i++)
{
map[0][i] = WALL;
map[HEIGHT + 1][i] = WALL;
}
//畫出入口和出口
map[2][1] = START;
map[HEIGHT - 1][WIDTH] = END;
for(inti = 1; i <= HEIGHT; i++)
{
for(intj = 1; j <= WIDTH; j++)
{
paint(i, j);
}
}
paint(0, 0);
return0;
}