使用栈解决迷宫问题

/******************************************************************************************
Copyright (c), Inc.(2016), All rights reserved.

Purpose: 使用栈解决迷宫问题

Author: 迷宫实现

Created Time: 2016_9
******************************************************************************************/
#pragma once
#include <stack>


struct Pos
{
	int row; // 行
	int col; // 列
};

int** CreateMaze(int n)
{
	int** ppMaze = new int*[n];
	for (int i = 0; i < n; ++i)
	{
		ppMaze[i] = new int[n];
		memset(ppMaze[i], 0, sizeof(int)*n);
	}

	return ppMaze;
}

Pos InitMaze(int** ppMaze, int n)
{
	Pos entry;
	entry.row = 2;
	entry.col = 0;

	FILE* fOut = fopen("MazeMap.txt", "r");
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n;)
		{
			char ch = fgetc(fOut);
			if (ch == '0' || ch == '1')
			{
				ppMaze[i][j++] = ch - '0';
			}
			else if (ch == EOF)
			{
				cout<<"Maze Map Error"<<endl;
			}
		}
	}

	fclose(fOut);

	return entry;
}

void PrintMaze(int** ppMaze, int n)
{
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			cout<<ppMaze[i][j]<<" ";
		}
		cout<<endl;
	}

	cout<<endl;
}

bool CheckIsAccess(int** ppMaze, int n, Pos pos)
{
	if (pos.col > 0 && pos.col < n 
		&& pos.row > 0 && pos.row < n
		&& ppMaze[pos.row][pos.col] == 0)
	{
		return true;
	}

	return false;
}

bool Maze(int** ppMaze, int n, const Pos& entry, stack<Pos>& path)
{
	assert(entry.col < n && entry.row < n);
	
	Pos cur = entry;
	ppMaze[cur.row][cur.col] = 2;
	path.push(cur);

	while (!path.empty())
	{
		 Pos next = path.top();
		
		 //
		 // 出口在最后一行
		 //
		 if (next.row == n-1)
			 return true;

		 // 上
		 next.row -= 1;
		 if (CheckIsAccess(ppMaze, n, next))
		 {
			ppMaze[next.row][next.col] = 2;
			path.push(next);
			continue;
		 }

		 // 右
		 next = path.top();
		 next.col += 1;
		 if (CheckIsAccess(ppMaze, n, next))
		 {
			 ppMaze[next.row][next.col] = 2;
			 path.push(next);
			 continue;
		 }

		 // 下
		 next = path.top();
		 next.row += 1;
		 if (CheckIsAccess(ppMaze, n, next))
		 {
			 ppMaze[next.row][next.col] = 2;
			 path.push(next);
			 continue;
		 }

		 // 左
		 next = path.top();
		 next.col -= 1;
		 if (CheckIsAccess(ppMaze, n, next))
		 {
			 ppMaze[next.row][next.col] = 2;
			 path.push(next);
			 continue;
		 }

		 Pos tmp = path.top();
		 ppMaze[tmp.row][tmp.col] = 3;
		 path.pop();
	}

	return false;
}

void TestMaze()
{
	int n = 10;
	int** ppMaze = CreateMaze(n);
	Pos entry = InitMaze(ppMaze, n);
	PrintMaze(ppMaze, n);

	stack<Pos> path;
	Maze(ppMaze, n, entry, path);
	PrintMaze(ppMaze, n);

	cout<<"Path:";
	while (!path.empty())
	{
		const Pos& pos = path.top();
		cout<<"("<<pos.row<<","<<pos.col<<")"<<"<-";
		path.pop();
	}

	cout<<"Entry"<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值