例題:
《迷宮問題》
定義一個二維數(shù)組:
0 0 1 0 1 //0表示可走,1表示墻
0 1 1 1 0 //只能↑↓←→走,不能斜著走
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0 //題目保證了輸入是一定有解的
求從左上角(0,0)到右下角(4,4)的最短路線。
bfs解題核心邏輯偽代碼:
1,將起點推入隊列中;
2,將起點標識為已走過;
while(隊列非空){
3,取隊列首節(jié)點vt,并從隊列中彈出;
4,探索上面取出得節(jié)點的周圍是否有沒走過的節(jié)點vf,如果有將所有能走的vf的parents指向vt,并將vf加入隊列
(如果vf等于終點,說明探索完成,退出循環(huán))。
}
如果隊列為空自然跳出,說明無路可達終點。
實際c++實現(xiàn):
#include<vector>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
struct Node//定義結(jié)構(gòu)體Node
{
int xx ;//自身處于組內(nèi)的位置
int yy;
bool qiang;//是否是墻
bool walked;//是否走過
Node *parents;//指向父節(jié)點的指針
};
int main() {
int id = 0;
//int xx, yy;
queue<Node*> bfs;//創(chuàng)建Node指針隊列
vector<vector<Node>> migong;//創(chuàng)建二維迷宮組
for (int i = 0; i < 5; i++) {
vector<Node> hang;
for (int j = 0; j < 5; j++) {
int ii;
cin >> ii;
Node node{ i,j,ii,false};
hang.push_back(node);
}
migong.push_back(hang);
}
//輸入完畢
int ax[4] = { -1,1,0,0 };
int by[4] = { 0,0,1,-1 };
bfs.push(&migong[0][0]);//先將起點推進去
migong[0][0].walked = true;
Node *vt;//等下指向父節(jié)點的指針
Node *vf;//等下指向父節(jié)點引申出的子節(jié)點
while (!bfs.empty()) {
vt = bfs.front();
bfs.pop();
if ((*vt).xx >= 1) {//查詢左節(jié)點是否可以
vf = &migong[(*vt).xx + ax[0]][(*vt).yy + by[0]];
if (!(*vf).qiang && !(*vf).walked) {
bfs.push(vf);
(*vf).walked = true;
(*vf).parents = vt;//子節(jié)點指向父節(jié)點
if ((*vf).xx == 4 && (*vf).yy == 4) break;//如果是終點節(jié)點,結(jié)束尋找,跳出循環(huán)。
}
}
if ((*vt).xx <=3 ) {//查詢右節(jié)點是否可以
vf = &migong[(*vt).xx + ax[1]][(*vt).yy + by[1]];
if (!(*vf).qiang && !(*vf).walked) {
bfs.push(vf);
(*vf).walked = true;
(*vf).parents = vt;
if ((*vf).xx == 4 && (*vf).yy == 4) break;
}
}
if ((*vt).yy <= 3) {//查詢下節(jié)點是否可以
vf = &migong[(*vt).xx + ax[2]][(*vt).yy + by[2]];
if (!(*vf).qiang && !(*vf).walked) {
bfs.push(vf);
(*vf).walked = true;
(*vf).parents = vt;
if ((*vf).xx == 4 && (*vf).yy == 4) break;
}
}
if ((*vt).yy >= 1) {//查詢上節(jié)點是否可以
vf = &migong[(*vt).xx + ax[3]][(*vt).yy + by[3]];
if (!(*vf).qiang && !(*vf).walked) {
bfs.push(vf);
(*vf).walked = true;
(*vf).parents = vt;
if ((*vf).xx == 4 && (*vf).yy == 4) break;
}
}
}
//結(jié)束算法,從vf指向的節(jié)點開始尋找父節(jié)點。
vector<Node*> fin;
while (true) {
fin.push_back(vf);
vf = (*vf).parents;
if ((*vf).xx == 0 && (*vf).yy == 0) {
fin.push_back(vf);
break;
}
}
//輸出
for (int i = fin.size()-1; i >=0;i--) {
cout << (*fin[i]).xx << "," << (*fin[i]).yy << endl;
}
return 0;
}
輸出示例:
0 0 1 0 1
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
0,0
1,0
2,0
2,1
2,2
2,3
2,4
3,4
4,4
dfs解題核心邏輯偽代碼:
1,棧初始化
2,獲得起點,將起點標識為已走過,將起點入棧
while(棧非空){
取棧頂元素vt
如果vt周圍有為走過的節(jié)點vf,則:
將vf改為已走
vf入棧
沒有能走的節(jié)點,vt出棧
}
代碼:
#include<iostream>
#include<vector>
#include<list>
#include<algorithm>
#include<queue>
#include<string>
#include<stack>
#include<time.h>
#include <windows.h>
#include<set>
using namespace std;
struct Node
{
int x;
int y;
bool walked;
int g;
int f;//f = g+h
int h;
Node* parents;
};
int main() {
vector<vector<Node>> migong;//創(chuàng)建二維迷宮組
for (int i = 0; i < 5; i++) {
vector<Node> hang;
for (int j = 0; j < 5; j++) {
int ii;
cin >> ii;
Node node{ i,j,ii };
hang.push_back(node);
}
migong.push_back(hang);
}
/*-----------------------------------dfs----------------------------------------------*/
vector<vector<Node>> migong2 = migong;
stack<Node*> f;
f.push(&migong2[0][0]);
migong2[0][0].walked = true;
while (!f.empty()) {
Node *vt = f.top();
bool can = true;
if (vt->x >= 1) {
Node *vf = &migong2[vt->x - 1][vt->y];
if (vf->walked == false) {
vf->parents = vt;
vf->walked = true;
if (vf == &migong2[4][4]) {
break;
}
f.push(vf);
can = false;
}
}
if (vt->x <=3) {
Node *vf = &migong2[vt->x + 1][vt->y];
if (vf->walked == false) {
vf->parents = vt;
vf->walked = true;
if (vf == &migong2[4][4]) {
break;
}
f.push(vf);
can = false;
}
}
if (vt->y >= 1) {
Node *vf = &migong2[vt->x][vt->y - 1];
if (vf->walked == false) {
vf->parents = vt;
vf->walked = true;
if (vf == &migong2[4][4]) {
break;
}
f.push(vf);
can = false;
}
}
if (vt->y <= 3) {
Node *vf = &migong2[vt->x ][vt->y + 1];
if (vf->walked == false) {
vf->parents = vt;
vf->walked = true;
if (vf == &migong2[4][4]) {
break;
}
f.push(vf);
can = false;
}
}
if (can) {
f.pop();
}
}
vector<Node*> fin2;
Node*bb = &migong2[4][4];
while (true) {
fin2.push_back(aa);
if (bb == &migong2[0][0]) {
break;
}
bb = bb->parents;
}
int count2 = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << migong2[i][j].walked;
if (migong2[i][j].walked)count2++;
}
cout << endl;
}
reverse(fin2.begin(), fin2.end());
for (int i = 0; i < fin.size(); i++) {
cout << fin[i]->x <<" "<< fin[i]->y<< endl;
}
return 0;
}
輸出:
0 1 0 0 0
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
11000
11111
11111
11111
00011
0 0
1 0
2 0
2 1
2 2
2 3
2 4
3 4
4 4
兩種方法的時間對比以及路徑分析:
#include<iostream>
#include<vector>
#include<list>
#include<algorithm>
#include<queue>
#include<string>
#include<stack>
#include<time.h>
#include <windows.h>
#include<set>
using namespace std;
struct Node
{
int x;
int y;
bool walked;
int g;
int f;//f = g+h
int h;
Node* parents;
};
int main() {
int qiang = 0;
vector<vector<Node>> migong;//創(chuàng)建二維迷宮組
for (int i = 0; i < 5; i++) {
vector<Node> hang;
for (int j = 0; j < 5; j++) {
int ii;
cin >> ii;
if (ii) qiang++;
Node node{ i,j,ii };
hang.push_back(node);
}
migong.push_back(hang);
}
int a[10002];
int i = 0;
double run_time;
_LARGE_INTEGER time_start; //開始時間
_LARGE_INTEGER time_over; //結(jié)束時間
double dqFreq; //計時器頻率
LARGE_INTEGER ff; //計時器頻率
QueryPerformanceFrequency(&ff);
dqFreq = (double)ff.QuadPart;
QueryPerformanceCounter(&time_start);
/*-----------------------------------dfs----------------------------------------------*/
vector<vector<Node>> migong2 = migong;
stack<Node*> f;
f.push(&migong2[0][0]);
migong2[0][0].walked = true;
while (!f.empty()) {
Node *vt = f.top();
bool can = true;
if (vt->x >= 1) {
Node *vf = &migong2[vt->x - 1][vt->y];
if (vf->walked == false) {
vf->parents = vt;
vf->walked = true;
if (vf == &migong2[4][4]) {
break;
}
f.push(vf);
can = false;
}
}
if (vt->x <=3) {
Node *vf = &migong2[vt->x + 1][vt->y];
if (vf->walked == false) {
vf->parents = vt;
vf->walked = true;
if (vf == &migong2[4][4]) {
break;
}
f.push(vf);
can = false;
}
}
if (vt->y >= 1) {
Node *vf = &migong2[vt->x][vt->y - 1];
if (vf->walked == false) {
vf->parents = vt;
vf->walked = true;
if (vf == &migong2[4][4]) {
break;
}
f.push(vf);
can = false;
}
}
if (vt->y <= 3) {
Node *vf = &migong2[vt->x ][vt->y + 1];
if (vf->walked == false) {
vf->parents = vt;
vf->walked = true;
if (vf == &migong2[4][4]) {
break;
}
f.push(vf);
can = false;
}
}
if (can) {
f.pop();
}
}
QueryPerformanceCounter(&time_over); //計時結(jié)束
run_time = 1000000 * (time_over.QuadPart - time_start.QuadPart) / dqFreq;
float time1 = run_time;
QueryPerformanceFrequency(&ff);
dqFreq = (double)ff.QuadPart;
QueryPerformanceCounter(&time_start);
/*-----------------------------------bfs----------------------------------------------*/
int ax[4] = { -1,1,0,0 };
int by[4] = { 0,0,1,-1 };
queue<Node*> bfs;
bfs.push(&migong[0][0]);//先將起點推進去
migong[0][0].walked = true;
Node *vt;//等下指向父節(jié)點的指針
Node *vf;//等下指向父節(jié)點引申出的子節(jié)點
while (!bfs.empty()) {
vt = bfs.front();
bfs.pop();
if ((*vt).x >= 1) {//查詢左節(jié)點是否可以
vf = &migong[(*vt).x + ax[0]][(*vt).y + by[0]];
if (!(*vf).walked && !(*vf).walked) {
bfs.push(vf);
(*vf).walked = true;
(*vf).parents = vt;//子節(jié)點指向父節(jié)點
if ((*vf).x == 4 && (*vf).y == 4) break;//如果是終點節(jié)點,結(jié)束尋找,跳出循環(huán)。
}
}
if ((*vt).x <= 3) {//查詢右節(jié)點是否可以
vf = &migong[(*vt).x + ax[1]][(*vt).y + by[1]];
if (!(*vf).walked && !(*vf).walked) {
bfs.push(vf);
(*vf).walked = true;
(*vf).parents = vt;
if ((*vf).x == 4 && (*vf).y == 4) break;
}
}
if ((*vt).y <= 3) {//查詢下節(jié)點是否可以
vf = &migong[(*vt).x + ax[2]][(*vt).y + by[2]];
if (!(*vf).walked && !(*vf).walked) {
bfs.push(vf);
(*vf).walked = true;
(*vf).parents = vt;
if ((*vf).x == 4 && (*vf).y == 4) break;
}
}
if ((*vt).y >= 1) {//查詢上節(jié)點是否可以
vf = &migong[(*vt).x + ax[3]][(*vt).y + by[3]];
if (!(*vf).walked && !(*vf).walked) {
bfs.push(vf);
(*vf).walked = true;
(*vf).parents = vt;
if ((*vf).x == 4 && (*vf).y == 4) break;
}
}
}
QueryPerformanceCounter(&time_over); //計時結(jié)束
run_time = 1000000 * (time_over.QuadPart - time_start.QuadPart) / dqFreq;
float time2 = run_time;
/*-----------------------------------A*----------------------------------------------*/
vector<vector<Node>> migong3 = migong;
set<Node*> openNode;
set<Node*> closeNode;
openNode.insert(&migong3[0][0]);
/*-----------------------------------結(jié)束----------------------------------------------*/
vector<Node*> fin;
Node*aa = &migong[4][4];
while (true) {
fin.push_back(aa);
if (aa == &migong[0][0]) {
break;
}
aa = aa->parents;
}
vector<Node*> fin2;
Node*bb = &migong2[4][4];
while (true) {
fin2.push_back(bb);
if (bb == &migong2[0][0]) {
break;
}
bb = bb->parents;
}
cout << "bfs運行后矩陣" << endl;
int count = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << migong[i][j].walked;
if (migong[i][j].walked)count++;
}
cout << endl;
}
reverse(fin.begin(), fin.end());
cout << "dfs運行后矩陣" << endl;
int count2 = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cout << migong2[i][j].walked;
if (migong2[i][j].walked)count2++;
}
cout << endl;
}
reverse(fin2.begin(), fin2.end());
for (int i = 0; i < fin.size(); i++) {
cout << fin[i]->x <<" "<< fin[i]->y<< endl;
}
cout << "Totle Time of dfs : " << time1 << "s" << endl;
cout << "Totle Time of bfs: " << time2 << "s" << endl;
cout << "bfs共搜索過的節(jié)點數(shù):" << count- qiang << endl;
cout << "dfs共搜索過的節(jié)點數(shù):" << count2- qiang << endl;
return 0;
//https://blog.csdn.net/u012878643/article/details/46723375
}
輸出示例1:
0 1 0 0 0
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
bfs運行后矩陣
11001
11111
11111
11111
11111
dfs運行后矩陣
11000
11111
11111
11111
00011
bfs路徑
0 0
1 0
2 0
2 1
2 2
2 3
2 4
3 4
4 4
dfs路徑
0 0
1 0
2 0
2 1
2 2
2 3
2 4
3 4
4 4
Totle Time of dfs : 65.5013s
Totle Time of bfs: 67.3427s
bfs共搜索過的節(jié)點數(shù):15
dfs共搜索過的節(jié)點數(shù):11
輸出示例2:
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
bfs運行后矩陣
11111
11111
11111
11111
11111
dfs運行后矩陣
11111
11111
11111
11111
11111
bfs路徑
0 0
1 0
2 0
3 0
4 0
4 1
4 2
4 3
4 4
dfs路徑
0 0
0 1
0 2
0 3
0 4
1 4
2 4
2 3
2 2
2 1
2 0
3 0
4 0
4 1
4 2
4 3
4 4
Totle Time of dfs : 133.107s
Totle Time of bfs: 131.792s
bfs共搜索過的節(jié)點數(shù):25
dfs共搜索過的節(jié)點數(shù):25
輸出示例3:
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
bfs運行后矩陣
11111
11111
11111
11111
11111
dfs運行后矩陣
11111
11111
11111
11111
11111
bfs路徑
0 0
1 0
2 0
3 0
4 0
4 1
4 2
4 3
4 4
dfs路徑
0 0
0 1
0 2
0 3
0 4
1 4
2 4
2 3
2 2
2 1
2 0
3 0
4 0
4 1
4 2
4 3
4 4
Totle Time of dfs : 120.217s
Totle Time of bfs: 99.1726s
bfs共搜索過的節(jié)點數(shù):19
dfs共搜索過的節(jié)點數(shù):19
參考:dfs詳解