⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 这事传统的老鼠迷宫的源代码
💻 CPP
字号:
#include <stack>
#include <iostream>
using namespace std;
#define M 10
    stack <int> stack1,stack2,stack3,stack4;    
   	int move_up=1;       //用于避免发生沿原路返回的往复运动
	int move_down=1;
	int move_right=1;
	int move_left=1;
	int way[M+2][M+2]=    //对迷宫中的障碍进行初始化
	{{1,1,1,1,1,1,1,1,1,1,1,1},
	{1,0,1,1,1,1,1,0,0,0,0,1},
	{1,0,0,0,0,0,1,0,1,0,0,1},
	{1,0,0,0,1,0,1,0,0,0,0,1},
	{1,0,1,0,1,0,1,0,1,1,0,1},
	{1,0,1,0,1,0,1,0,1,0,0,1},
	{1,0,1,1,1,0,1,0,1,0,1,1},
	{1,0,1,0,0,0,1,0,1,0,1,1},
	{1,0,1,0,1,1,1,0,1,0,0,1},
	{1,1,0,0,0,0,0,0,1,0,0,1},
	{1,0,0,0,0,1,1,1,1,0,0,1},
	{1,1,1,1,1,1,1,1,1,1,1,1}};
    void judge(int line,int col)    //按右、下、左、上的顺序判断可以向哪个方向走
{
	if(!((line==M)&&(col==M)))      //老鼠没有到迷宫的出口时
	{
		if((way[line][col+1]==0)&&(move_right==1))//判断是否可以向右走
		{
			col++;                //可以向右走,则将列数加一
			stack1.push(line);    //将坐标送入堆栈,stack1存放横坐标,stack2存放列坐标
			stack2.push(col);
			move_left=0;         //此时不能向左返回,可以走其他三个方向
			move_up=1;
			move_down=1;
			move_right=1;
			judge(line,col);    //在下一点继续重复判断
		}
		else
		{
			if((way[line+1][col]==0)&&(move_down==1))  //判断是否可以向下走
			{
				
				line++;                //可以向下走,则行数加一
				stack1.push(line);     //将坐标送入堆栈
				stack2.push(col);
				move_up=0;             //此时不能向上走,可以走其他三个方向
				move_down=1;
				move_left=1;
				move_right=1;
				judge(line,col);
			}
			else
			{
				if((way[line][col-1]==0)&&(move_left==1))  //判断是否可以向左走
				{
					
					col--;           //可以向左走,则列数减一
					stack1.push(line);  //将坐标送入堆栈
					stack2.push(col);
					move_right=0;       //此时不能向右走,可以走其他三个方向
					move_up=1;
					move_down=1;
					move_left=1;
					judge(line,col);
				}
				else
				{
					if((way[line-1][col]==0)&&(move_up==1))    //判断是否可以向上走
					{
						
						line--;                 //可以向上走,则行数减一
						stack1.push(line);      //将坐标送入堆栈
						stack2.push(col);
						move_down=0;            //此时不能向下走,可以走其他三个方向
						move_up=1;
						move_right=1;
						move_left=1;
						judge(line,col);
					}
					else                    //四个方向都不能走时
					{
						line=stack1.top();  //保存上一次压入堆栈的坐标
						col=stack2.top();
						stack1.pop();       //将上一次压入堆栈的坐标pop出来
						stack2.pop(); 
						
						way[line][col]=1;   //将这一点置为有障碍
						line=stack1.top();  //回到前一点
						col=stack2.top();
						
						judge(line,col);    //继续判断向哪个方向走
					}
				}
			}
		}
	}
	
}
	
	void print()
{
	
	if(!((stack3.empty())||(stack4.empty())))  //将堆栈中的坐标打印出来
	{
		int x=stack3.top();
		int y=stack4.top();
		stack3.pop();
		stack4.pop();
		
		cout<<x<<","<<y<<endl;
		print();
	}
	
}
	void main()
{
	
   	cout<<"1,1"<<endl;
	judge(1,1);
	while(!(stack1.empty()))      //将stack1中的横坐标压入stack3
	{                             //使得横坐标能按路径顺序先后打印出来
		int path1=stack1.top();
		stack3.push(path1);
		stack1.pop();
	}
	while(!(stack2.empty()))      //将stack2中的纵坐标压入stack4
	{                             //使得纵坐标能按路径顺序先后打印出来
		int path2=stack2.top();
		stack4.push(path2);
		stack2.pop();
	}
	print();                     //打印坐标
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -