迷宮問題 192132 01

2021-06-29 03:07:43 字數 3846 閱讀 4210

迷宮問題

以乙個m*n

的長方陣表示迷宮,0和

1分別表示迷宮中的通路和障礙。設計乙個程式,對任意設定的迷宮,求出一條從入口到出口的通路,或得出沒有通路的結論。

要求:(1)首先實現乙個以煉表作儲存結構的棧型別,然後編寫乙個求解迷宮的非遞迴程式。求得的通路以三元組

(i,j,d)

的形式輸出,其中

(i,j)

指示迷宮中的乙個座標,

d表示走到下一座標的方向。 (2

)測試幾組資料,資料的規模由小變大,即網格越來越小,障礙越來越複雜。 (3

)實現該問題的視覺化介面。

#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#includeusing namespace std;  

int n, m, sx, sy, ex, ey, t;

int p[110][110];

char map[110][110];

int vis[110][110];

int dir[4][2] = , , , };

struct node

; struct node pre[30][30];//儲存節點前乙個位置

// 棧

#define datatype node

struct node;

typedef struct node *pnode;

typedef struct node

node;

typedef struct linkstack

linkstack;

typedef struct linkstack * plinkstack;

plinkstack createemptystack(void);

int isemptystack(plinkstack stack);

int push(plinkstack stack, datatype x);

int pop(plinkstack stack);

datatype gettop(plinkstack stack);

void showstack(plinkstack stack);

void setempty(plinkstack stack);

void destroystack(plinkstack stack);

plinkstack createemptystack(void)

int isemptystack(plinkstack stack)

int push(plinkstack stack, datatype x)

else

} int pop(plinkstack stack)

else

} datatype gettop(plinkstack stack)

return (stack->top->info);

} void showstack(plinkstack stack)

printf("%d ", p->info);

printf("-->底\n");

} } void setempty(plinkstack stack)

void destroystack(plinkstack stack)

} ///

int check(int x, int y)

void printf_path()

int pres, presy;

printf (" 行走路徑 文字描述如下:\n\n\n");

while (!(isemptystack(stack)))

}printf("到達終點!\n\n\n\n");

printf (" 行走路徑圖如下:\n");

for(int i=0;i<=m+1;i++)

printf("\n");

}}

佇列

#define error( str ) fatalerror( str )

#define fatalerror( str ) fprintf( stderr, "%s\n", str ), exit( 1 )

#define elementtype node

#define maxqueue 100

typedef struct node

node;

typedef struct queue

*ptrqueue;

typedef ptrqueue queue;

int isempty(queue q);

int isfull(queue q);

queue createqueue(void);

void disposequeue(queue q);

void makeempty(queue q);

void enqueue(elementtype x, queue q);

elementtype front(queue q);

void dequeue(queue q);

int isfull(queue q)

int isempty(queue q)

queue createqueue(void)

void disposequeue(queue q)

void makeempty(queue q)

void enqueue(elementtype x, queue q)

void dequeue(queue q)

elementtype front(queue q)

elementtype frontanddequeue(queue q)

return x;

} ///

int bfs()

for (int i = 0; i < 4; i++)

} return 0;

} int main()

} int ans = bfs();

if (ans == 0)

printf("不存在起點到終點的通路\n\n\n\n");

printf("\n\n\n\n");

printf("是否繼續 1.yes 2.no\n ");

scanf("%d",&t);

if (t == 2)

printf("\n\n\n\n");

printf("請輸入迷宮長和寬\n");

} return 0;

}

測試資料:

5 50 0 4 4

0 1 0 0 0

0 1 0 1 0

0 0 0 0 0

0 1 1 1 1

0 0 0 1 0

15 5

0 0 4 4

0 1 0 0 0

0 1 0 1 0

0 0 0 0 0

0 1 1 1 0

0 0 0 1 0

問題 A 迷宮求解問題

時間限制 10 sec 記憶體限制 2048 mb 提交 574 解決 306 提交 狀態 討論版 用乙個m n的矩陣表示迷宮,0和1分別表示迷宮中的通路和障礙。設計乙個程式,對給定的迷宮,求出找到的第一條從入口到出口的通路,或得到沒有通路的結論。我們指定 1 迷宮的入口為矩陣的左上角 1,1 迷宮...

迷宮問題求解(1) 簡單迷宮

標頭檔案 include include include include include maze.h define max 100 typedef struct position datatype typedef struct stack stack void stackinit stack s ...

迷宮問題1

c 語言程式設計練習1 程式設計練習 編寫程式尋找迷宮路徑。入口 x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 出口x 圖1讀取迷宮檔案 如圖1所示 不限於此迷宮 以入口為開始 出口為終點 程式設計尋找一條穿越迷宮的路...