-------------------------------------------------------------------------------------------
Flash版老鼠走迷宮:
-------------------------------------------------------------------------------------------
// BCB 程式碼範例
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{ Memo1->Clear();
move[0].dx = -1; move[0].dy = 0;
move[1].dx = -1; move[1].dy = 1;
move[2].dx = 0; move[2].dy = 1;
move[3].dx = 1; move[3].dy = 1;
move[4].dx = 1; move[4].dy = 0;
move[5].dx = 1; move[5].dy = -1;
move[6].dx = 0; move[6].dy = -1;
move[7].dx = -1; move[7].dy = -1;
}
//以上 請寫在 Form1 初始化的地方 (如上)
int u, v, i, j;
position step;
int mark[200][200];
boolean found = false;
Memo1->Lines->Add("m="+IntToStr(m)+" p="+IntToStr(p));
// Memo1->Lines->Add(found);
for (i=1; i<=m; i++)
{ for (j=1; j<=p; j++)
{ mark[i][j] = maze[i][j];
}
}
for (i=0; i<=m; i++) { mark[i][0] = 1; mark[i][p+1] = 1; }
for (j=0; i<=p; i++) { mark[0][j] = 1; mark[m+1][j] = 1; }
step.x = 1;
step.y = 1;
step.dir = 2; // (i,j,dir)=(1,1, 2)
push(step);
while (top != -1)
{ step = pop();
while ((step.dir <= 7) && (!found))
{ u = step.x+move[step.dir].dx;
v = step.y+move[step.dir].dy;
int x = step.dir;
Memo1->Lines->Add(".. (u,v,dir)="+
IntToStr(u)+","+IntToStr(v)+","+IntToStr(x));
//str = ;
Memo1->Lines->Add("...come:"+IntToStr(u)+","+
IntToStr(v)+","+IntToStr(x));
// (u,v) = 自(i,j)欲嘗試的下一步座標
if ((u == m) && (v == p))
{ found = true; //成功找到出口,輸出路徑,可以停止
str = "("+IntToStr(m)+","+IntToStr(p)+")";
StringGrid1->Cells[p][m] = "&";
str = "("+IntToStr(step.x)+","+IntToStr(step.y)+","+IntToStr(x)+")-->"+str;
StringGrid1->Cells[step.y][step.x] = "->";
while (top != -1)
{ step = pop();
int x = step.dir;
str = "("+IntToStr(step.x)+","+IntToStr(step.y)+","+IntToStr(x)+")-->"+str;
StringGrid1->Cells[step.y][step.x] = "->";
}
Memo1->Lines->Add(str);
}
else if ((maze[u][v]==0) && (mark[u][v]==0))
//(u,v)可以走,且不曾走過
{ mark[u][v] = 1;
StringGrid1->Cells[v][u] = "*";
step.dir = step.dir+1; //?
push(step);
step.x = u; step.y = v; step.dir = 0;
}
else step.dir += 1;
}
}
// Visual C++ 程式碼範例
step.x = 1;
step.y = 1;
step.dir = 2; // (i,j,dir)=(1,1,2)
push(step);
while (top != -1)
{ step = pop();
while ((step.dir <= 7) && (!found))
{ u = step.x+move[step.dir].dx;
v = step.y+move[step.dir].dy;
int x = step.dir;
listBox1->Items->Add("(u,v,dir) = "+u+","+v+","+x);
listBox1->Items->Add("come: "+u+","+v+","+x);
// (u,v) = 自(i,j)欲嘗試的下一步座標
if ((u == m) && (v == p))
{ found = true; //成功找到出口,輸出路徑,可以停止 }
str = "("+m.ToString()+","+p.ToString()+")";
dataGridView1->Rows[m]->Cells[p]->Value = "&";
str = "("+step.x.ToString()+","+step.y.ToString()+
","+x.ToString()+")-->"+str;
dataGridView1->Rows[step.x]->Cells[step.y]->Value = "->";
while (top != -1)
{ step = pop();
int x = step.dir;
str = "("+step.x.ToString()+","+step.y.ToString()+
","+x.ToString()+")-->"+str;
dataGridView1->Rows[step.x]->Cells[step.y]->Value = "->";
}
listBox1->Items->Add(str);
}
else if ((maze[u][v]==0) && (mark[u][v]==0))
//(u,v)可以走,且不曾走過
{ mark[u][v] = 1;
dataGridView1->Rows[u]->Cells[v]->Value = "*";
step.dir = step.dir+1;
push(step);
step.x = u; step.y = v; step.dir = 0;
}
else
step.dir = step.dir+1;
}
}
以下為在 codeblocks 上實作的版本
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <iostream>
using namespace std;
int ** maze;
int m, n;
int ** generatemaze(int ** maze, int x, int y)
{ int direction;
maze[x][y] = 0; // 標示此格已設定
while ((y+2 < n && maze[x][y+2]==1) || (x+2 < m && maze[x+2][y]==1) || (y-2 > 0 && maze[x][y-2]==1) || (x-2 > 0 && maze[x-2][y]==1)) // 如果不是外牆
{ direction = rand()%4+1; // 決定下一個位置
if (direction==1 && (y+2 < n && maze[x][y+2]==1))// 向右走
{ maze[x][y+1] = 0; // 拆掉右牆
maze = generatemaze(maze, x, y+2);
}
else if (direction==2 && (x-2 > 0 && maze[x-2][y]==1)) // 向上走
{ maze[x-1][y] = 0; // 拆掉上牆
maze = generatemaze(maze, x-2, y);
}
else if (direction==3 && (y-2 > 0 && maze[x][y-2]==1)) // 向左走
{ maze[x][y-1] =0; // 拆掉右牆
maze = generatemaze(maze, x, y-2);
}
else if (direction==4 && (x+2 < m && maze[x+2][y]==1)) // 向下走
{ maze[x+1][y] =0; // 拆掉上牆
maze = generatemaze(maze, x+2,y);
}
}
return maze;
}
int main()
{ int i, j, x, y;
cin >> m >> n;
cout << "m=" << m << ", n=" << n << endl;
maze = new int * [m];
for (i=0; i<m; i++)
{ maze[i] = new int [n];
}
int Start_i=1, Start_j=1, End_i=m-2, End_j=n-2;
srand(time(NULL));
for (x=0; x<m; x++)
{ for (y=0; y<n; y++)
{ if (x==0 || y==0 || x==m-1 || y==n-1 ) maze[x][y] = 2; // 設定外牆
else maze[x][y] = 1; // 初始迷宮內部
}
}
maze = generatemaze(maze, End_i, End_j); // 產生迷宮
maze[Start_i][Start_j-1] =0; // 拆掉入口左牆
maze[End_i][End_j+1] =0; // 拆掉出口右牆
string out, mark;
for (i=0; i<m; i++)
{ for (out = "", j=0; j<n; j++)
{ if (maze[i][j]==2 || maze[i][j]==1) mark = "#";
else mark = ".";
out += mark;
}
cout << out << endl;
}
getchar();
return 0;
}