maze.h
#pragma once
#include #include #define n 6
static int _maze[n][n] = ,
, ,, ,
,};typedef struct pos
pos;
void mazeprint();
int mazegetpath(pos entry, pos exit); //
int mazecheckaccess(pos pos); //檢查是否走過
int mazeshortpath(pos entry); //最短路徑
思想:1.建立乙個棧,先將入口點座標壓入棧中
2.將此座標標記,為走過(2)
3.如果滿足出口條件,則計算棧中的元素個數,就是走過的步數
4.否則,就判斷下乙個座標 next;如果下乙個座標未走過,(或者next - cur >1)也表示此路未走過,則壓入棧中
5.如果該座標四面都不能走,則pop掉該座標,即回退
6.直到棧中沒有元素了,就可以返回其步數
maze.c
#include "maze.h"
#include "stack.h"
int mazecheckaccess(pos pos) //檢查是否走過
int mazegetpath(pos entry,pos exit)
else
}//return size; //出口
} pos next; //下乙個座標
/ // 上
next._row = cur._row - 1;
next._col = cur._col;
if (mazecheckaccess(next) - _maze[cur._row][cur._col] > 1 || mazecheckaccess(next) == 1)
/// 下
next._row = cur._row + 1;
next._col = cur._col;
if (mazecheckaccess(next) - _maze[cur._row][cur._col] > 1 || mazecheckaccess(next) == 1)
/ // 左
next._row = cur._row;
next._col = cur._col - 1;
if (mazecheckaccess(next) - _maze[cur._row][cur._col] > 1 || mazecheckaccess(next) == 1)
/ // 右
next._row = cur._row;
next._col = cur._col + 1;
if (mazecheckaccess(next) - _maze[cur._row][cur._col] > 1 || mazecheckaccess(next) == 1)
stackpop(&s); //如果程式走到這裡,說明以無路可走,回溯
} return size;
}void mazeprint()
printf("\n"); }}
void testmazeshortpath()
}int main()
棧和佇列迷宮問題
define n 6 int maze n n 通過乙個數字來創造乙個6 6的迷宮,其中 0代表牆,1代表能夠走的路。這裡將陣列通過畫圖軟體畫出來,這裡紅色的1代表迷宮的入口,綠色的 1代表迷宮的出口。這個陣列所建立的迷宮是相對複雜的一種迷宮,首先這個迷宮是存在環的 這幾個1,如果你的迷宮函式只是用...
解決迷宮問題, 棧和佇列
includeusing namespace std const int m 10,n 10 int mg m 1 n 1 const maxsize 200 struct qu maxsize int front 1,rear 1 隊首指標和隊尾指標 1 首先將 1,1 入隊 2 在佇列qu不為空...
基於棧和佇列的迷宮問題求解
問題描述 以乙個m n的長方陣表示迷宮,0和1分別表示迷宮中的通路和障礙。設計乙個程式,對任意設定的迷宮,求出一條從入口到出口的通路,或得出沒有通路的結論。測試資料 迷宮的測試資料如下 左上角 1,1 為入口,右下角 8,9 為出口。一 需求分析 值得注意的是,題目要求中的測試資料給的是右下角 8,...