[問題] 迷宮問題

看板C_and_CPP (C/C++)作者 (kururu)時間14年前 (2012/05/09 22:02), 編輯推噓0(004)
留言4則, 4人參與, 最新討論串1/1
VC語言 問題(Question): 題目規定要顯示出可走路徑和原始路徑 可走路徑可以顯示出來 但是原始路徑要怎麼讓他在下面顯示出來 有沒有高手可以教我 程式碼: # include <iostream> # include <stdlib.h> # include <time.h> int main (void) { int m,n; printf("請輸入列 0~40 :\n"); scanf("%d",&m); printf("請輸入行 0~40 :\n"); scanf("%d",&n);/*迷宮大小*/ int move[8][2] ={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};/*限 定移動方向*/ struct position/*宣告一個"position"的結構*/ { int x,y ; }; /*宣告變數*/ int maze[40][40] ; position stack[1600]; int top ; int i,x,y,ok ; position p ; srand(time(0));/*設定亂數種子*/ for(x=1;x<=m;x++) for(y=1;y<=n;y++) maze[x][y]=48+rand()%2;/*隨機產生 rand()%2後的數字除與二後的餘數*/ maze[1][1]='0'; maze[m][n]='0';/*產生0和1的迷宮*/ for(x=0;x<=m+1;x++) { maze[x][0]='1';maze[x][m+1]='1'; } for(y=0;y<=m+1;y++) { maze[0][y]='1';maze[n+1][y]='1';/*在外圍圍起一道牆以免還沒跑到正確終點時就 亂跑出去*/ } p.x=1;p.y=1;/*設定判別可以走的路徑*/ top=1;stack[top]=p ;/*走過的路徑作stack,遇到死路就pop out回到上一個叉路口*/ maze [1][1]='.'; do { p=stack[top]; ok=0;i=0; while ((ok==0)&&(i<8)) { x=p.x+move[i][0];/*判別可以走的方向*/ y=p.y+move[i][1]; if (maze[x][y]=='0') { p.x=x;p.y=y; stack[++top]=p; maze[x][y]='.';/*可以走的路徑顯示"."*/ ok=1 ; } i++ ; } if(i==8)/*不可以走的路徑顯示"*"*/ { maze[p.x][p.y]='*'; top-- ; } } while((top>0)&&((p.x!=m)||(p.y!=n)));/*如果((top>0)且(p.x不等於m)或(p.y不等於 m))*/ if (top==0) printf("沒有路徑\n");/*輸出*/ else printf("有路徑\n"); for (x=1;x<=m;x++) { printf("\n"); for (y=1;y<=n;y++ ) {printf ("%c ",maze[x][y]) ;} } printf ("\n") ; printf("迷宮路徑圖(從左上角到右下角): \n"); for ( x = 0; x <= m-1; x++) { /* 顯示迷宮圖形 */ for ( y = 0; y <= n-1; y++) printf("%d", maze[x][y]); /* 顯示座標值 */ printf("\n"); } system("pause"); return 0; } 預期的正確結果(Expected Output): 01001 01011 11101 11011 10100 迷宮路徑圖 01001 01011 11101 11011 10100 錯誤結果(Wrong Output): 01001 01011 11101 11011 10100 迷宮路徑圖 4949494949 4942494849 4942494848 4949494948 4949494849 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.32.192.79

05/09 22:25, , 1F
原PO自學的嗎?沒助教?
05/09 22:25, 1F

05/09 22:28, , 2F
算是自學吧
05/09 22:28, 2F

05/09 23:58, , 3F
把程式碼貼在http://ideone.com/吧..
05/09 23:58, 3F

05/10 18:09, , 4F
%c
05/10 18:09, 4F
文章代碼(AID): #1Fgda0w3 (C_and_CPP)
文章代碼(AID): #1Fgda0w3 (C_and_CPP)