老鼠走迷宫

老鼠走迷宫

个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来搞科研为人民作出自己的贡献

博客内容:老鼠走迷宫

博客时间:2014-4-9

编程语言:C++

编程坏境:Windows 7 专业版 x64

编程工具:vs2008 32位编译器

制图工具:office 2010 ppt

 

引言

有些事情要不一直做,每天都做,要不就一点都别做。

题目

老鼠走迷宫,stack 的典型教案。

思路

利用表格作为迷宫,0 代表可以过,1代表不可以过

老鼠走过的脚印用 2 标记

举个例子,迷宫如下

老鼠可以可以到达终点的路径如下红色线标记

老鼠从开始节点走,每进步一格,就标记格子已经走过,然后把脚印放入栈里面

伪代码思路如下

maze 是代表迷宫的二维数组

result是一个栈,stack<pair<int,int>>

			result->push(std::make_pair(0,0));
			Maze[0][0] = 2;

			while(! result->empty())
			{
				// push path;
				now = result -> top();
				if(now.first == row - 1 && now.second == column -1)
				{
					cout<<"find path"<<endl;
					break;
				}
				else if(now.second < column-1 && Maze[now.first][now.second+1] == 0 )
				{
					result -> push(std::make_pair(now.first,now.second+1));

					Maze[now.first][now.second+1] = 2;
				}
				else if( now.first > 0 && Maze[now.first-1][now.second] == 0)
				{
					result -> push(std::make_pair(now.first-1,now.second));

					Maze[now.first-1][now.second] = 2;
				}
				else if( now.second > 0 && Maze[now.first][now.second-1] == 0)
				{
					result -> push(std::make_pair(now.first,now.second-1));
	
					Maze[now.first][now.second-1] = 2;
				}
				else if(now.first < row - 1 && Maze[now.first+1][now.second] == 0)
				{
					result -> push(std::make_pair(now.first+1,now.second));
	
					Maze[now.first+1][now.second] = 2;
				}
				// pop stack
				else if(!result->empty())
				{
					result->pop();
				}
			}
			if(!result->empty())
			{
				std::pair<int,int> path;
				while(!result->empty())
				{
					path = result->top();
					cout<<path.first<<" "<<path.second<<endl;
					result->pop();
				}
			}
			else cout<<"not find path"<<endl;


 

实验

一个封闭迷宫例子

程序运行结果

一个不封闭迷宫例子

程序运行结果

程序解释

find path 之后

每一行是 老鼠可以穿过迷宫的路线,即二维数组的行和列的下标

代码

test.cpp

#include<iostream>
#include<stack>
#include<fstream>
#include<utility>
using namespace std;

// function: Mise mazes
void _Mise_mazes(int **Maze,int row,int column);


int main()
{
	int row ,column ;
	ifstream reader ;
	reader.open("data.txt");
	reader>>row>>column;
	int **Maze = new int*[row];
	for(int i=0 ; i<row; i++)
	{
		Maze[i] = new int[column];
	}
	

	for(int i=0; i<row; i++)
		for(int j=0; j<column; j++)
		{
			reader>>Maze[i][j];
		}
	for(int i= 0;i<row;i++)
	{
		for(int j=0;j<column;j++)
		{
			cout<<Maze[i][j]<<" ";
		}
		cout<<endl;
	}
	reader.close();
	_Mise_mazes(Maze,row,column);
	system("pause") ;
	return 0 ;
}

// function:Mise mazes
// input: a maze with array
// output: void
// 功能: Mise mazes 老鼠走迷宫
void _Mise_mazes(int ** Maze,int row,int column)
{
	if(Maze != NULL && row > 0 && column > 0)
	{
		// first action: make space
		stack<std::pair<int,int>> * result = new stack<std::pair<int,int>> ;

		if(Maze[0][0] != 0)
		{
			cout<<"no path for Mise"<<endl;
		}
		else
		{
			int i,j;
			std::pair<int,int> now;
			result->push(std::make_pair(0,0));
			Maze[0][0] = 2;

			while(! result->empty())
			{
				// push path;
				now = result -> top();
				if(now.first == row - 1 && now.second == column -1)
				{
					cout<<"find path"<<endl;
					break;
				}
				else if(now.second < column-1 && Maze[now.first][now.second+1] == 0 )
				{
					result -> push(std::make_pair(now.first,now.second+1));

					Maze[now.first][now.second+1] = 2;
				}
				else if( now.first > 0 && Maze[now.first-1][now.second] == 0)
				{
					result -> push(std::make_pair(now.first-1,now.second));

					Maze[now.first-1][now.second] = 2;
				}
				else if( now.second > 0 && Maze[now.first][now.second-1] == 0)
				{
					result -> push(std::make_pair(now.first,now.second-1));
	
					Maze[now.first][now.second-1] = 2;
				}
				else if(now.first < row - 1 && Maze[now.first+1][now.second] == 0)
				{
					result -> push(std::make_pair(now.first+1,now.second));
	
					Maze[now.first+1][now.second] = 2;
				}
				// pop stack
				else if(!result->empty())
				{
					result->pop();
				}
			}
			if(!result->empty())
			{
				std::pair<int,int> path;
				while(!result->empty())
				{
					path = result->top();
					cout<<path.first<<" "<<path.second<<endl;
					result->pop();
				}
			}
			else cout<<"not find path"<<endl;
		}

		delete result ;
	}
	else
	{
		cout<<"exception of function _Mise_mazes input"<<endl;
	}
}


 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值