日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用栈解决深度搜索问题

發(fā)布時間:2025/3/11 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用栈解决深度搜索问题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

#include
#include
struct Pos
{
int _row{};
int _col{};
Pos(int row, int col) :_row(row)
, _col(col) {
}
Pos() {
}
};
std::stacks;
bool CheckIsAccess(int* a, int row_size, int col_size, Pos cur)
{
//行坐標(biāo)不合法
if (cur._row <0 || cur._row >= row_size){
return false;
}
//列坐標(biāo)不合法
if (cur._col <0 || cur._col>=col_size){
return false;
}
//走過路
if (a[cur._row * col_size + cur._col] >0){
return false;
}
return true;
}
bool check_pos(int* maze, int rows, int cols, Pos entry)
{
s.push(entry);
while (!s.empty()) {
Pos cur = s.top();
Pos next = cur;
//記迷宮的出口僅在下邊緣
if (next._row == rows-1){
return true;
}
//試探上面
next._row–;
if (CheckIsAccess(maze,rows,cols, next)){
maze[cur._row * cols + cur._col] = 2;
s.push(next);
continue;
}
//試探下面
next = cur;
next._row++;
if (CheckIsAccess(maze, rows, cols, next)) {
maze[cur._row * cols + cur._col] = 2;
s.push(next);
continue;
}
//試探左面
next = cur;
next._col–; //列開始減
if (CheckIsAccess(maze, rows, cols, next)) {
maze[cur._row * cols + cur._col] = 2;
s.push(next);
continue;
}
//試探右面
next = cur;
next._col++;
if (CheckIsAccess(maze, rows, cols, next)) {
maze[cur._row * cols + cur._col] = 2;
s.push(next);
continue;
}
//回溯,如果其余三個方向均不跳出,則表示無通路
maze[cur._row * cols + cur._col] = 3;//將走過的點標(biāo)記為3,也可以不標(biāo)記
//如果此節(jié)點無路,就刪除節(jié)點
s.pop();
}
return false;
}
int main()
{
int map[10][10] = { 1,1,1,1,1,1,1,1,1,1,
0,0,1,1,1,1,1,1,1,1,
1,0,1,1,1,1,1,1,1,1,
1,0,0,1,1,1,1,1,1,1,
1,1,0,0,0,0,0,1,1,1,
1,1,0,1,1,1,0,1,1,1,
1,1,0,1,1,1,0,1,1,1,
1,1,0,1,1,1,0,1,1,1,
1,1,0,1,1,1,0,0,1,1,
1,1,1,1,1,1,1,0,1,1
};
bool flag = check_pos((int*)map, 10, 10, Pos(1, 0));
}

總結(jié):利用棧的思想,去遍歷每個坐標(biāo),可以走的坐標(biāo)放入棧中,然后每個取遍歷,直到此坐標(biāo)不能走位置,若不能走,就把此坐標(biāo)刪除掉,然后彈出下一個坐標(biāo)即可

總結(jié)

以上是生活随笔為你收集整理的利用栈解决深度搜索问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。