C語言 迷宮(棧實現)

2021-06-20 11:22:52 字數 2040 閱讀 9727

problem i:**迷宮(全部**自己編寫)

description 

編寫乙個程式求解迷宮問題。迷宮是乙個n行m列的矩陣,其中'0'表示無障礙,'1'表示有障礙。設入口為(1,1),出口為(n,m),每次移動只能從乙個無障礙的單元移到其周圍4個方向上任一無障礙的單元。若該迷宮存在一條從入口到出口的路徑,則輸出"yes";否則輸出"no"。 

演算法可參考課本50頁

input 

每組輸入資料中,第1行為迷宮的行數n和列數m(0 < n<=50,0< m<=50),接下來n行,每行包括m個字元,其中'0'表示無障礙,'1'表示有障礙,輸入資料中只有這兩種字元。

output 

若該迷宮存在一條從入口到出口的路徑,則輸出"yes";否則輸出"no"。

sample input 

本題系統測試樣例並不止以下兩例

第一組輸入

4 500100

00100

00001

01100

第一組輸出

yes第二組輸入

3 500100

00001

01110

第二組輸出

nosample output 

#include "stdio.h"

#include "malloc.h"

#define ok 1

#define error 0

#define true 1

#define false 0

typedef int status;   

typedef int selemtype;

typedef struct snode

snode,*linkstackptr;

typedef struct linkstack

linkstack,* lstack;

void initstack(lstack &s)  

//構造乙個空棧 

status push(lstack &s,selemtype e1,selemtype e2,selemtype e3)//進棧

//插入元素e為新的棧頂元素

status pop(lstack &s,selemtype &e1,selemtype &e2,selemtype &e3)//出棧

//若棧不為空,刪除s的棧頂元素,用e返回其值,並返回ok,否則返回error

status gettop1(lstack &s,selemtype &e1,selemtype &e2,selemtype &e3)//出棧

//若棧不為空,獲取s的棧頂元素,用e返回其值,並返回ok,否則返回error

void stackprint(lstack s)

//從棧頂到棧底依次列印元素

while(p != null)

printf("\n");

}int chazhao(lstack &s,selemtype e1,selemtype e2)//查詢e1  e2 是否在棧中

while(p != null)

return error;

}int main()

if(a[0][0] == 1)

push(s,0,0,1);

gettop1(s,q1,q2,q3);

while(1)

}else if (q3 == 2)

}else if (q3 == 3)

}else 

}gettop1(s,q1,q2,q3);

if((q1 == n-1 && q2 == m-1) || (q1 ==0 && q2 ==0 && q3 == 5))

break;

}if(q1 == n-1 && q2 == m-1)

printf("yes\n");

else

printf("no\n");

return 0;

}思路是借鑑書上的,,,理清一下,,就可以實現簡單的迷宮了,,,這裡的入口在(1,1),出口在(n,m),,,可能不存在出口或入口,,,路徑的話,,想看到的話,,,也可以自己嘗試修改一些就能列印出來了

棧實現迷宮(c語言)

利用棧實現迷宮問題,1 輸入迷宮的大小m行n列,兩者為整數。2 由隨機數產生0或1,建立迷宮。3 輸出資料。4 首先輸出迷宮,在列印由入口到出口的入線。如無通道,則列印出無。include include include include define maxsize 100 define row 6...

迷宮問題 c語言棧實現

我們用乙個二維陣列表示迷宮的點,1能走,0不能走,用回溯法寫,用乙個簡單一點的迷宮做事例 define crt secure no warnings 1 include include include define n 6 n n的迷宮 define length n n 最大行走步數 typede...

C語言資料結構 棧實現迷宮

include define max 30 typedef struct box typedef struct stack int map 10 10 int search int beginx,int beginy,int endx,int endy else return 1 find 0 wh...