註冊會員
|
謝謝,問題算是解決了,之前是在macbookpro上用gcc指令編譯執行,然後就卡在aaa前。
後來回到宿舍用桌機的visual studio 編譯,改掉一些bug後就好了。
改的部分最重要的是最後的break判定改為14,14,其餘也修正了一些移動方線和坐標更新不一致的變數bug,就可以跑了。
------------------------------------------------------
#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(int argc, char *argv[]) {
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 = 0, y = 0;
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("\n");
}
xtop = add(xstack, xtop, x);
ytop = add(ystack, ytop, y);
solution[0][0] = 3;
while(1) {
x = stacktop(xstack, xtop);
y = stacktop(ystack, ytop);
//system("pause");
//printf("\nx=: %d, y = %d. ", x, y);
if(maze[x][y] == 0){
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;
//printf("下\n");
}
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;
//printf("右下\n");
}
else 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;
//printf("右\n");
}
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;
//printf("右上\n");
}
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;
//printf("上\n");
}
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;
//printf("左上\n");
}
else 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;
//printf("左\n");
}
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{ //退
solution[x][y] = 1;
xtop = delete(xstack, xtop);
ytop = delete(ystack, ytop);
//printf("退\n");
}
}
else if(maze[x][y] == 2){
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;
//printf("下\n");
}
else 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;
//printf("右\n");
}
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;
//printf("上\n");
}
else 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;
//printf("左\n");
}
else{ //退
//printf("go back. ");
solution[x][y] = 1;
xtop = delete(xstack, xtop);
ytop = delete(ystack, ytop);
//printf("退\n");
}
}
/*for (int p = 0; p < 15; p++) {
for (int q = 0; q < 15; q++) {
printf("%d ", solution[p][q]);
}
printf("\n");
}*/
if((stacktop(xstack, xtop) == 14) && (stacktop(ystack, ytop) == 14))//終點到達
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");
system("pause");
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--;
}
}
---------------------------------------------------------
至於delete是保留字的事,在windows時似乎不影響,但想來這可能是我把新code
放回mac依然會卡住的原因吧!!
最後順便問一下,在mac編譯完後會給我一個執行檔,但我用visual sstudio compile完後找不到生成的檔案ㄟ,不知道藏哪裡去了...有在"電腦"搜尋過,但是除了原本的.c檔以外就沒有別的了...
|