经典算法<一>迷宫问题 1.单条路径 DFS求解 C++实现

/*
* File name  : maze_BFS.cpp
* Function   : 迷宫问题 单条路径DFS求解 C++实现
* Created on : 2016年5月12日
* Author     : beijiwei@qq.com
* Copyright  : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。
任何单位和个人不经本人允许不得用于商业用途
*
题目:迷宫问题(1): 只有一条通道的迷宫
0为墙,1为通道

入口
1 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 1 0 1 1 1 1 0 0
0 0 1 0 1 0 0 1 0 0
0 0 1 1 1 0 0 1 1 0
0 0 0 0 0 0 0 0 1 1
出口

*
*/
#include <cstdio>
#include <iostream>

#pragma warning(disable:4996)

using namespace std;

typedef struct {
	int data;
	bool mark;
}Spot;

#define SIZE 10
Spot sarray[SIZE][SIZE];

typedef struct {
	int x;
	int y;
}Pos;
Pos store[SIZE * SIZE];
int store_pos = 0;
//int offset[4][2] = {-1,0,0,1,1,0,0,-1};

int offset[4][2] = { { -1,0 },
{ 0,1 },
{ 1,0 },
{ 0,-1 }
};


void DFS(Spot data[][SIZE], int x, int y);

int main(int argc, char** argv)
{
	int M = 0, N = 0;

	freopen("input.txt", "r", stdin);
	cin >> M >> N;

	for (int i = 0; i<M; i++)
		for (int j = 0; j < N; j++) {
			cin >> sarray[i][j].data;  // get input data
			sarray[i][j].mark = 0;
		}


	DFS(sarray, 0, 0);

	for (int i = 0; i < store_pos; i++) {
		cout << "( " << store[i].x << " , " << store[i].y << " )" << endl;
	}

	return 0;
}

void DFS(Spot data[][SIZE], int x, int y) {
	int tmpx, tmpy;

	data[x][y].mark = 1;
	store[store_pos].x = x;
	store[store_pos++].y = y;

	for (int k = 0; k < 4; k++) {
		tmpx = x + offset[k][0];
		tmpy = y + offset[k][1];
		if (tmpx >= 0 && tmpx < SIZE  // x 不越界
			&&	tmpy >= 0 && tmpy < SIZE // y 不越界
			&&  data[tmpx][tmpy].mark == 0// 未被访问过
			&& data[tmpx][tmpy].data == 1
			) {
			DFS(data, tmpx, tmpy);
		}
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于深度优先搜索算法迷宫问题法的代码实现,包括添加显示迷宫求解一条迷宫路径求解所有迷宫路径求解最短迷宫路径等功能: ```c++ #include <iostream> #include <vector> #include <stack> #include <queue> using namespace std; // 定义迷宫结构体 struct Maze { int row; // 迷宫行数 int col; // 迷宫列数 vector<vector<int>> maze; // 二维迷宫数组 }; // 定义坐标结构体 struct Pos { int x; int y; }; // 添加迷宫函数 void AddMaze(Maze& maze) { cout << "请输入迷宫的行数和列数: "; cin >> maze.row >> maze.col; // 初始化迷宫为全部为墙 vector<vector<int>> mz(maze.row, vector<int>(maze.col, -1)); maze.maze = mz; // 添加迷宫地图 cout << "请输入迷宫地图(0代表通路,1代表障碍): " << endl; for (int i = 0; i < maze.row; i++) { for (int j = 0; j < maze.col; j++) { cin >> maze.maze[i][j]; } } } // 显示迷宫函数 void ShowMaze(Maze& maze) { cout << "当前迷宫地图如下: " << endl; for (int i = 0; i < maze.row; i++) { for (int j = 0; j < maze.col; j++) { cout << maze.maze[i][j] << " "; } cout << endl; } } // 判断当前位置是否合法 bool IsValidPos(Maze& maze, Pos& pos) { if (pos.x < 0 || pos.x >= maze.row) { return false; } if (pos.y < 0 || pos.y >= maze.col) { return false; } if (maze.maze[pos.x][pos.y] == 1) { return false; } return true; } // 深度优先搜索求解一条迷宫路径 bool DFS(Maze& maze, Pos& start, Pos& end, vector<Pos>& path, vector<vector<int>>& visited) { // 如果当前位置为终点,则返回true if (start.x == end.x && start.y == end.y) { path.push_back(start); return true; } // 如果当前位置不合法,则返回false if (!IsValidPos(maze, start)) { return false; } // 如果当前位置已被访问过,则返回false if (visited[start.x][start.y] == 1) { return false; } // 将当前位置标记为已访问 visited[start.x][start.y] = 1; // 将当前位置添加到路径中 path.push_back(start); // 分别向上、下、左、右四个方向搜索 Pos next; next.x = start.x - 1; next.y = start.y; if (DFS(maze, next, end, path, visited)) { return true; } next.x = start.x + 1; next.y = start.y; if (DFS(maze, next, end, path, visited)) { return true; } next.x = start.x; next.y = start.y - 1; if (DFS(maze, next, end, path, visited)) { return true; } next.x = start.x; next.y = start.y + 1; if (DFS(maze, next, end, path, visited)) { return true; } // 如果四个方向都搜索不到终点,则将当前位置从路径中删除 path.pop_back(); return false; } // 求解一条迷宫路径函数 void SolveOnePath(Maze& maze, Pos& start, Pos& end) { vector<Pos> path; vector<vector<int>> visited(maze.row, vector<int>(maze.col, 0)); // 深度优先搜索求解一条迷宫路径 if (DFS(maze, start, end, path, visited)) { cout << "迷宫路径如下: " << endl; for (int i = 0; i < path.size(); i++) { cout << "(" << path[i].x << "," << path[i].y << ") "; } cout << endl; } else { cout << "没有找到迷宫路径!" << endl; } } // 求解所有迷宫路径函数 void SolveAllPaths(Maze& maze, Pos& start, Pos& end) { stack<Pos> s; vector<vector<int>> visited(maze.row, vector<int>(maze.col, 0)); s.push(start); cout << "所有迷宫路径如下: " << endl; while (!s.empty()) { Pos cur = s.top(); s.pop(); // 如果当前位置为终点,则输出路径 if (cur.x == end.x && cur.y == end.y) { cout << "(" << start.x << "," << start.y << ") "; stack<Pos> temp; while (!s.empty()) { temp.push(s.top()); s.pop(); } while (!temp.empty()) { cout << "(" << temp.top().x << "," << temp.top().y << ") "; s.push(temp.top()); temp.pop(); } cout << "(" << end.x << "," << end.y << ")" << endl; continue; } // 如果当前位置不合法,则跳过 if (!IsValidPos(maze, cur)) { continue; } // 如果当前位置已被访问过,则跳过 if (visited[cur.x][cur.y] == 1) { continue; } // 将当前位置标记为已访问,并加入路径 visited[cur.x][cur.y] = 1; s.push(cur); // 分别向上、下、左、右四个方向搜索 Pos next; next.x = cur.x - 1; next.y = cur.y; s.push(next); next.x = cur.x + 1; next.y = cur.y; s.push(next); next.x = cur.x; next.y = cur.y - 1; s.push(next); next.x = cur.x; next.y = cur.y + 1; s.push(next); } } // 求解最短迷宫路径函数 void SolveShortestPath(Maze& maze, Pos& start, Pos& end) { queue<Pos> q; vector<vector<int>> visited(maze.row, vector<int>(maze.col, 0)); vector<vector<int>> dist(maze.row, vector<int>(maze.col, 0)); q.push(start); visited[start.x][start.y] = 1; while (!q.empty()) { Pos cur = q.front(); q.pop(); // 如果当前位置为终点,则输出路径长度 if (cur.x == end.x && cur.y == end.y) { cout << "最短迷宫路径长度为: " << dist[cur.x][cur.y] << endl; return; } // 分别向上、下、左、右四个方向搜索 Pos next; next.x = cur.x - 1; next.y = cur.y; if (IsValidPos(maze, next) && visited[next.x][next.y] == 0) { visited[next.x][next.y] = 1; dist[next.x][next.y] = dist[cur.x][cur.y] + 1; q.push(next); } next.x = cur.x + 1; next.y = cur.y; if (IsValidPos(maze, next) && visited[next.x][next.y] == 0) { visited[next.x][next.y] = 1; dist[next.x][next.y] = dist[cur.x][cur.y] + 1; q.push(next); } next.x = cur.x; next.y = cur.y - 1; if (IsValidPos(maze, next) && visited[next.x][next.y] == 0) { visited[next.x][next.y] = 1; dist[next.x][next.y] = dist[cur.x][cur.y] + 1; q.push(next); } next.x = cur.x; next.y = cur.y + 1; if (IsValidPos(maze, next) && visited[next.x][next.y] == 0) { visited[next.x][next.y] = 1; dist[next.x][next.y] = dist[cur.x][cur.y] + 1; q.push(next); } } cout << "没有找到迷宫路径!" << endl; } int main() { Maze maze; AddMaze(maze); ShowMaze(maze); Pos start, end; cout << "请输入起点坐标: "; cin >> start.x >> start.y; cout << "请输入终点坐标: "; cin >> end.x >> end.y; SolveOnePath(maze, start, end); SolveAllPaths(maze, start, end); SolveShortestPath(maze, start, end); return 0; } ``` 注意:以上代码仅供参考,可能存在一些细节问题,具体实现可根据需要进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值