不過不知道為何一開始的列印迷宮就不會動了...
PHP 語法:
include <stdio.h>
#include <stdlib.h>
#define MAX 225
int creates(int[]); // 建立堆疊
int isEmpty(int); // 堆疊已空
int stacktop(int[], int); // 傳回頂端元素
int add(int[], int, int); // 插入元素
int delete(int[], int); // 刪除元素
void list(int[], int); // 顯示所有內容
int main(void) {
int maze[15][15]={
{0, 2, 1, 1, 1, 1, 2, 0, 2, 0, 1, 2, 1, 0, 2}
,{2, 0, 0, 2, 1, 2, 1, 0, 1, 2, 0, 2, 1, 0, 0}
,{1, 2, 0, 1, 0, 2, 1, 0, 2, 1, 0, 2, 0, 1, 2}
,{0, 0, 2, 1, 2, 0, 1, 0, 2, 1, 0, 0, 1, 1, 2}
,{1, 2, 1, 0, 1, 2, 0, 0, 1, 2, 1, 0, 1, 0, 2}
,{0, 0, 0, 1, 0, 2, 0, 1, 2, 1, 2, 0, 0, 1, 0}
,{1, 0, 2, 1, 0, 1, 2, 0, 2, 1, 0, 2, 1, 2, 1}
,{2, 1, 2, 0, 0, 1, 2, 1, 0, 2, 1, 0, 0, 1, 2}
,{0, 2, 0, 1, 2, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1}
,{2, 1, 0, 2, 1, 0, 2, 1, 0, 1, 0, 2, 1, 0, 2}
,{1, 2, 0, 0, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 0}
,{1, 0, 2, 0, 1, 1, 1, 0, 1, 0, 2, 0, 1, 2, 0}
,{0, 2, 1, 0, 2, 1, 0, 2, 1, 0, 0, 2, 0, 1, 2}
,{1, 2, 0, 1, 2, 0, 2, 1, 0, 2, 0, 2, 0, 1, 0}
,{0, 1, 2, 0, 1, 0, 0, 2, 0, 1, 2, 1, 2, 1, 0}};
int solution[15][15];
int xstack[MAX];
int ystack[MAX];
int xtop, ytop;
int x, y;
xtop = creates(xstack);
ytop = creates(ystack);
printf("這是迷宮:\n");//印出迷宮&複制迷宮副本
for(int p = 0; p < 15; p++){
for(int q = 0; q < 15; q++){
printf("%d ", maze[p][q]);
solution[p][q] = maze[p][q];
}
}
printf("aaa");
while(1) {
x = stacktop(xstack, xtop);
y = stacktop(ystack, ytop);
if(maze[x][y] == 0){
if((solution[x][y+1] == 0)||(solution[x][y+1] == 2)){//下
xtop = add(xstack, xtop, x);
ytop = add(ystack, ytop, y+1);
solution[x][y+1] = 3;
}
else if((solution[x+1][y+1] == 0)||(solution[x+1][y+1] == 2)){//右下
xtop = add(xstack, xtop, x+1);
ytop = add(ystack, ytop, y+1);
solution[x+1][y+1] = 3;
}
else if((solution[x+1][y] == 0) ||(solution[x+1][y] == 2)){//右
xtop = add(xstack, xtop, x+1);
ytop = add(ystack, ytop, y);
solution[x+1][y] = 3;
}
else if((solution[x+1][y-1] == 0)||(solution[x+1][y-1] == 2)){//右上
xtop = add(xstack, xtop, x+1);
ytop = add(ystack, ytop, y-1);
solution[x+1][y-1] = 3;
}
else if((solution[x][y-1] == 0) ||(solution[x+1][y] == 2)){//上
xtop = add(xstack, xtop, x);
ytop = add(ystack, ytop, y-1);
solution[x+1][y] = 3;
}
else if((solution[x-1][y-1] == 0) ||(solution[x+1][y] == 2)){//左上
xtop = add(xstack, xtop, x-1);
ytop = add(ystack, ytop, y-1);
solution[x+1][y] = 3;
}
else if((solution[x-1][y] == 0) ||(solution[x+1][y] == 2)){//左
xtop = add(xstack, xtop, x-1);
ytop = add(ystack, ytop, y);
solution[x+1][y] = 3;
}
else if((solution[x-1][y+1] == 0) ||(solution[x+1][y] == 2)){//左下
xtop = add(xstack, xtop, x-1);
ytop = add(ystack, ytop, y+1);
solution[x+1][y] = 3;
}
else{ //退
solution[x][y] = 1;
xtop = delete(xstack, xtop);
ytop = delete(ystack, ytop);
}
}
else if(maze[x][y] == 2){
if((solution[x][y+1] == 0)||(solution[x][y+1] == 2)){//下
xtop = add(xstack, xtop, x);
ytop = add(ystack, ytop, y+1);
solution[x][y+1] = 3;
}
else if((solution[x+1][y] == 0) ||(solution[x+1][y] == 2)){//右
xtop = add(xstack, xtop, x+1);
ytop = add(ystack, ytop, y);
solution[x+1][y] = 3;
}
else if((solution[x][y-1] == 0) ||(solution[x+1][y] == 2)){//上
xtop = add(xstack, xtop, x);
ytop = add(ystack, ytop, y-1);
solution[x+1][y] = 3;
}
else if((solution[x-1][y] == 0) ||(solution[x+1][y] == 2)){//左
xtop = add(xstack, xtop, x-1);
ytop = add(ystack, ytop, y);
solution[x+1][y] = 3;
}
else{ //退
solution[x][y] = 1;
xtop = delete(xstack, xtop);
ytop = delete(ystack, ytop);
}
}
if((stacktop(xstack, xtop) == 15) && (stacktop(ystack, ytop) == 15))//終點到達
break;
}
printf("路徑為:\n");//列出結果
for(int p = 0; p < 15; p++){
for(int q = 0; q < 15; q++){
if(solution[p][q] == 3)
printf("3");
else
printf("*");
}
printf("\n");
}
printf("\n");
return 0;
}
// 以下為堆疊操作的實作
int creates(int stack[]) {
int i;
for(i = 0; i < MAX; i++)
stack[i] = 0;
return -1;
}
int isEmpty(int top) {
return (top == -1);
}
int stacktop(int stack[], int top) {
return stack[top];
}
int add(int stack[], int top, int item) {
int t = top;
if(t >= MAX-1) {
printf("\n堆疊已滿!");
return t;
}
stack[++t] = item;
return t;
}
int delete(int stack[], int top) {
int t = top;
if(isEmpty(t)) {
printf("\n堆疊已空!");
return t;
}
return --t;
}
void list(int stack[], int top) {
int t = top;
printf("\n堆疊內容:");
while(!isEmpty(t)) {
printf("%d ", stack[t]);
t--;
}
}