1.广度优先搜寻
#include <iostream>
#include <queue>
using namespace std;
#define MAXROW 10
#define MAXLINE 10
typedef struct _Point
{
int _x;
int _y;
}Point;
Point prePonit[MAXROW][MAXLINE];
int maze[MAXROW][MAXLINE] =
{
1,1,1,1,1,1,1,1,1,1,
0,0,0,1,1,1,1,1,1,1,
1,1,0,1,1,1,1,1,1,1,
1,1,0,0,0,0,1,1,1,1,
1,1,0,1,1,0,1,1,1,1,
1,1,0,1,1,0,1,1,1,1,
1,1,1,1,1,0,1,1,1,1,
1,1,1,1,1,0,0,0,1,1,
1,1,1,1,1,1,1,0,0,0,
1,1,1,1,1,1,1,1,1,1,
};
void displyMaze()
{
for (int i = 0; i < MAXROW; i++)
{
for (int j = 0; j < MAXLINE; j++)
{
if (maze[i][j] == 1) printf("%2s", " *");
else if (maze[i][j] == 2) printf("%2s", " #");
else printf("%2s", " ");
}
putchar(10);
}
printf(" ====================\n");
}
void visit(queue<Point> &s, int x, int y, Point t)
{
Point p = { x, y };
s.push(p);
prePonit[x][y] = t;
}
int main()
{
memset(prePonit, 0xFF, sizeof(Point)*MAXROW*MAXLINE);
displyMaze();
Point sp = { 1, 0 }, ep = { 8, 9 };
queue<Point> s;
s.push(sp);
bool flag = false;
while (!s.empty())
{
Point t = s.front();
s.pop();
maze[t._x][t._y] = 2;
//上
if (t._x - 1 >= 0 && maze[t._x - 1][t._y] == 0)
visit(s, t._x - 1, t._y, t);
//下
if (t._x + 1 < MAXROW && maze[t._x + 1][t._y] == 0)
visit(s, t._x + 1, t._y, t);
//左
if (t._y - 1 >= 0 && maze[t._x][t._y - 1] == 0)
visit(s, t._x, t._y - 1, t);
//右
if (t._y + 1 < MAXLINE && maze[t._x][t._y+1] == 0)
visit(s, t._x, t._y + 1, t);
if (t._x == ep._x && t._y == ep._y)
{
flag = true;
break;
}
}
displyMaze();
if (flag)
{
cout << "Find Path!" << endl;
Point t = ep;
while (t._x != -1)
{
cout << "(" << t._x << "," << t._y << ")" << endl;
t = prePonit[t._x][t._y];
}
}
else
cout << "Not Find Path!" << endl;
return 0;
}
结果:
* * * * * * * * * *
* * * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * * * *
* * * * * * * * * *
====================
* * * * * * * * * *
# # # * * * * * * *
* * # * * * * * * *
* * # # # # * * * *
* * # * * # * * * *
* * # * * # * * * *
* * * * * # * * * *
* * * * * # # # * *
* * * * * * * # # #
* * * * * * * * * *
====================
Find Path!
(8,9)
(8,8)
(8,7)
(7,7)
(7,6)
(7,5)
(6,5)
(5,5)
(4,5)
(3,5)
(3,4)
(3,3)
(3,2)
(2,2)
(1,2)
(1,1)
(1,0)
2.深度优先搜寻
#include <iostream>
#include <stack>
using namespace std;
#define MAXROW 10
#define MAXLINE 10
typedef struct _Point
{
int _x;
int _y;
}Point;
Point prePonit[MAXROW][MAXLINE];
int maze[MAXROW][MAXLINE] =
{
1,1,1,1,1,1,1,1,1,1,
0,0,0,1,1,1,1,1,1,1,
1,1,0,1,1,1,1,1,1,1,
1,1,0,0,0,0,1,1,1,1,
1,1,0,1,1,0,1,1,1,1,
1,1,0,1,1,0,1,1,1,1,
1,1,1,1,1,0,1,1,1,1,
1,1,1,1,1,0,0,0,1,1,
1,1,1,1,1,1,1,0,0,0,
1,1,1,1,1,1,1,1,1,1,
};
void displyMaze()
{
for (int i = 0; i < MAXROW; i++)
{
for (int j = 0; j < MAXLINE; j++)
{
if (maze[i][j] == 1) printf("%2s", " *");
else if (maze[i][j] == 2) printf("%2s", " #");
else printf("%2s", " ");
}
putchar(10);
}
printf(" ====================\n");
}
void visit(stack<Point> &s, int x, int y, Point t)
{
Point p = { x, y };
s.push(p);
prePonit[x][y] = t;
}
int main()
{
memset(prePonit, 0xFF, sizeof(Point)*MAXROW*MAXLINE);
displyMaze();
Point sp = { 1, 0 }, ep = { 8, 9 };
stack<Point> s;
s.push(sp);
bool flag = false;
while (!s.empty())
{
Point t = s.top();
s.pop();
maze[t._x][t._y] = 2;
//上
if (t._x - 1 >= 0 && maze[t._x - 1][t._y] == 0)
visit(s, t._x - 1, t._y, t);
//下
if (t._x + 1 < MAXROW && maze[t._x + 1][t._y] == 0)
visit(s, t._x + 1, t._y, t);
//左
if (t._y - 1 >= 0 && maze[t._x][t._y - 1] == 0)
visit(s, t._x, t._y - 1, t);
//右
if (t._y + 1 < MAXLINE && maze[t._x][t._y+1] == 0)
visit(s, t._x, t._y + 1, t);
if (t._x == ep._x && t._y == ep._y)
{
flag = true;
break;
}
}
displyMaze();
if (flag)
{
cout << "Find Path!" << endl;
Point t = ep;
while (t._x != -1)
{
cout << "(" << t._x << "," << t._y << ")" << endl;
t = prePonit[t._x][t._y];
}
}
else
cout << "Not Find Path!" << endl;
return 0;
}
结果:
* * * * * * * * * *
* * * * * * *
* * * * * * * * *
* * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * * * *
* * * * * * * * * *
====================
* * * * * * * * * *
# # # * * * * * * *
* * # * * * * * * *
* * # # # # * * * *
* * * * # * * * *
* * * * # * * * *
* * * * * # * * * *
* * * * * # # # * *
* * * * * * * # # #
* * * * * * * * * *
====================
Find Path!
(8,9)
(8,8)
(8,7)
(7,7)
(7,6)
(7,5)
(6,5)
(5,5)
(4,5)
(3,5)
(3,4)
(3,3)
(3,2)
(2,2)
(1,2)
(1,1)
(1,0)