1 DFS
#include <iostream>
#include<cstring>#include<sstream>
using namespace std;
struct node
{
int x, y;
};
char map[4][4] = { { '.', '.', '.', '.' }, { '.', '#', '#', '.' }, { '.', '#', '.', '.' }, { '.', '.', '.', '.' } };
int visit[4][4] = { 0 };
node star{ 0, 0 }, end{ 2, 2 };
int minstep = 600;
int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
int judge(int x, int y,int step)
{
if ((2- x + 2 - y + step) > minstep)
return 0;
else
return 1;
}
vector<node> minpath;
vector<node> path;
int step = -1;
void dfs(int x, int y,int step, vector<node> path)
{
path.push_back(node{ x, y });
visit[x][y] = 1;
step++;
if (x == 2 && y == 2)
{
if (step <= minstep)
{
minstep = step;
minpath = path;
for (int i = 0; i < minpath.size(); i++)
{
cout << minpath[i].x << " " << minpath[i].y << endl;
}
cout << "--------------------------"<< endl;
}
path.pop_back();
visit[x][y] = 0;
step--;
}
else
{
for (int i = 0; i < 4; i++)
{
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (judge(nx, ny, step) && nx >= 0 && nx < 4 && ny >= 0 && ny < 4 && map[nx][ny] == '.'&&visit[nx][ny] == 0)
{
dfs(nx, ny, step, path);
}
}
path.pop_back();
visit[x][y] = 0;
step--;
}
}
int main()
{
dfs(0, 0,step,path);
cout << minstep << endl;
system("pause");
return 0;
}
跟剑指offer25题一模一样,区别是树每条路径是向下所有孩子搜索,而迷宫是上下左右搜索,为了防止上下左右四个孩子回溯到本节点导致循环递归,设置visit变量。但是要记得本条路径搜索完毕之后visiti要复位,防止影响其他搜索路径的搜索!!
方法2 错误
#include<string>
using namespace std;
#include<iostream>
#include <vector>
#include<deque>
#include <list>
#include<queue>
#include<stack>
#include<algorithm>
#include <iomanip>
#include <functional>
#include <iostream>
#include<cstring>
#include<sstream>
using namespace std;
struct node
{
int x, y;
};
char map[4][4] = { { '.', '.', '.', '.' }, { '.', '#', '#', '.' }, { '.', '#', '.', '.' }, { '.', '.', '.', '.' } };
int visit[4][4] = { 0 };
node star{ 0, 0 }, end{ 2, 2 };
int minstep = 600;
int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
int judge(int x, int y,int step)
{
if ((2- x + 2 - y + step) > minstep)
return 0;
else
return 1;
}
vector<node> minpath;
vector<node> path;
int step = 0;
void dfs(int x, int y,int step, vector<node> path)
{
path.push_back(node{ x, y });
visit[x][y] = 1;
//step++;
if (x == 2 && y == 2)
{
if (step <= minstep)
{
minstep = step;
minpath = path;
for (int i = 0; i < minpath.size(); i++)
{
cout << minpath[i].x << " " << minpath[i].y << endl;
}
cout << "--------------------------"<< endl;
}
path.pop_back();
visit[x][y] = 0;
//step--;
}
else
{
for (int i = 0; i < 4; i++)
{
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (judge(nx, ny, step) && nx >= 0 && nx < 4 && ny >= 0 && ny < 4 && map[nx][ny] == '.'&&visit[nx][ny] == 0)
{
int nstep = ++step;//ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!应该修改为int nstep=(step+1),并不能改变step的值,否则上下左右四个孩子的step起始值不一样了,影响其他三个孩子的最短路径判断!!!!
dfs(nx, ny, nstep, path);
}
}
path.pop_back();
visit[x][y] = 0;
//step--;
}
}
int main()
{
dfs(0, 0,step,path);
cout << minstep << endl;
system("pause");
return 0;
}
2 BFS
#include <iostream>
#include<cstring>
#include<sstream>
using namespace std;
struct node
{
int x, y, level;
};
int dir[4][2] = { { -1, 0 }, { 1, 0 }, {0,-1}, {0,1} };
int visit[10][10] = {0};
int sx = 0, sy = 0, ex = 2, ey = 2;
char map[4][4] = { { '.', '.', '.', '.' }, { '.', '#', '#', '.' }, { '.', '#', '.', '.' }, { '.', '.', '.', '.' } };
int bfs(char map[4][4])
{
queue<node > q;
node start{ 0, 0, 0 }, end{ 2, 2, 0 };
q.push(start);
visit[0][0] = 1;
int m = 4, n = 4;
while (!q.empty())
{
node now = q.front(),next;
q.pop();
for (int i = 0; i < 4; i++)
{
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.level = now.level + 1;
if (next.x == ex&&next.y == ey)
return next.level;
if (next.x >= 0 && next.x < m && next.y >= 0 && next.y < n && visit[next.x][next.y] == 0 && map[next.x][next.y] == '.')
{
q.push(next);
visit[next.x][next.y] = 1;
}
}
}
return 0;
}
int main()
{
cout << bfs(map) << endl;
system("pause");
return 0;
}