码迷,mamicode.com
首页 > 其他好文 > 详细

迷宫问题

时间:2016-04-13 00:31:46      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:迷宫问题 求出口路径

#include <iostream>
#include <cassert>
#include <stack>
#include <vector>

struct Pos
{
	int _row;
	int _col;
};

void GetMaze(int* &a, int& row, int& col)
{
	std::FILE* fileMaze = fopen("C:\\Users\\朱潇翔\\Desktop\\迷宫.txt", "r");
	assert(fileMaze);
	char ch = 0;
	while (true)
	{
		ch = fgetc(fileMaze);
		if (ch == ‘ ‘ || ch == ‘\n‘)
		{
			break;
		}
		row = row * 10 + ch - ‘0‘;
	}
	while (true)
	{
		ch = fgetc(fileMaze);
		if (ch == ‘ ‘ || ch == ‘\n‘)
		{
			break;
		}
		col = col * 10 + ch - ‘0‘;
	}
	a = new int[row * col];
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col;)
		{
			ch = fgetc(fileMaze);
			if (ch == ‘1‘ || ch == ‘0‘)
			{
				a[i*col + j] = ch - ‘0‘;
				++j;
			}
		}
	}
	fclose(fileMaze);
}
void PrintMaze(int* maze, int row, int col)
{
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			cout << maze[i*col + j] << " ";
		}
		cout << endl;
	}
}
void PrintMaze(const vector<vector<int>>& maze, int row, int col)
{
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			cout << maze[i][j] << " ";
		}
		cout << endl;
	}
}
/*10 10
1 1 1 1 1 1 1 1 1 1
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 1 1 1 1 1 1 1
1 1 0 1 1 1 1 1 1 1
1 1 0 0 0 0 0 0 1 1
1 1 0 1 1 1 1 0 1 1
1 1 0 1 1 1 1 0 1 1
1 1 0 1 1 1 1 1 1 1*/
bool SearchMazePath(int* maze, int row, int col, Pos enrty,
	std::stack<Pos>& paths)
{
	assert(maze);
	paths.push(enrty);
	while (!paths.empty())
	{
		Pos cur = paths.top();
		maze[cur._row*col + cur._col] = 2;
		if (cur._row == row - 1)
		{
			return true;
		}
		//左
		Pos next = cur;
		--next._col;
		if (next._row >= 0 && next._row < row
			&&next._col >= 0 && next._col < col
			&&maze[next._row * col + next._col] == 0)
		{
			paths.push(next);
			continue;
		}
		//右
		next = cur;
		++next._col;
		if (next._row >= 0 && next._row < row
			&&next._col >= 0 && next._col < col
			&&maze[next._row * col + next._col] == 0)
		{
			paths.push(next);
			continue;
		}
		//上
		next = cur;
		--next._row;
		if (next._row >= 0 && next._row < row
			&&next._col >= 0 && next._col < col
			&&maze[next._row * col + next._col] == 0)
		{
			paths.push(next);
			continue;
		}
		//下
		next = cur; 
		++next._row;
		if (next._row >= 0 && next._row < row
			&&next._col >= 0 && next._col < col
			&&maze[next._row * col + next._col] == 0)
		{
			paths.push(next);
			continue;
		}
		maze[paths.top()._row * col + paths.top()._col] = 3;
		paths.pop();
	}
	return false;
}

void PrintPaths(stack<Pos> paths)
{
	stack<Pos> p;
	while (!paths.empty())
	{
		p.push(paths.top());
		paths.pop();
	}
	while (!p.empty())
	{
		cout << "(" << p.top()._row << ", " << p.top()._col << ") ->";
		p.pop();
	}
	cout << "终点" << endl;
}


迷宫问题

标签:迷宫问题 求出口路径

原文地址:http://10740026.blog.51cto.com/10730026/1763160

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!