自己写的一个迷宫问题C语言


#include <iostream>
#include <fstream>
#include<stack>

using namespace std;

struct Pos
{
    int x;
    int y;
    int di;
};
void mazePath();
void printMazePath();

#define MAZE_SIZE = 10
定义一个迷宫,0表示通道,1表示墙
int maze[10][10] = 
{
	{0,1,1,1,1,1,1,1,1,1},
	{0,0,0,1,1,0,0,1,0,1},
	{1,0,0,1,0,0,0,1,0,1},
	{1,0,0,0,0,1,1,0,0,1},
	{1,0,1,1,1,0,0,0,0,1},
	{1,0,0,0,1,0,0,0,0,1},
	{1,0,1,0,0,0,1,0,0,1},
	{1,0,1,1,1,0,1,1,0,1},
	{1,1,0,0,0,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,0,0}
};
stack <Pos> posStack;
int xe = 9;
int ye = 9;
ofstream ff("me.txt");
int main(void)
{
    Pos pos;
    pos.x = 0;
    pos.y = 0;
    pos.di = 0;
    posStack.push(pos);
    mazePath();
    system("PAUSE");
    return 0;
}

void mazePath()
{
    if(posStack.empty())
    {
        //ff<<"No answer"<<endl;
        return;
    }
    Pos pos = posStack.top();
    if(pos.x==xe && pos.y==ye)
    {
        //ff<<"Over"<<endl;
        printMazePath();
        
        //以下代码开启,表示求出所有可能路径  
        posStack.pop();
        maze[pos.y][pos.x] = 0;
        mazePath();
        
        return;
    }
    
    if(pos.di<4)
    {
        int nextX;
        int nextY;
        switch(pos.di)
        {
        case 0:
             nextX = pos.x+1;
             nextY = pos.y;
             break;
        case 1:
             nextX = pos.x;
             nextY = pos.y+1;
             break;
        case 2:
             nextX = pos.x-1;
             nextY = pos.y;
             break;
        case 3:
             nextX = pos.x;
             nextY = pos.y-1;
             break;
        default:
             break;
        }
        
        if((nextX>=0 && nextX<10) && (nextY>=0 && nextY<10) && (maze[nextY][nextX]==0))
        {
            pos.di += 1;
            posStack.pop();
            posStack.push(pos);
            
            Pos nextPos;
            nextPos.di = 0;
            nextPos.x = nextX;
            nextPos.y = nextY;
            posStack.push(nextPos);
            
            maze[pos.y][pos.x] = -1;
            //ff<<"NextOK"<<" "<<pos.x<<" "<<pos.y<<" "<<pos.di-1<<endl;
            
            
            
            mazePath();
        }
        else
        {
            pos.di += 1;
            posStack.pop();
            posStack.push(pos);
            //ff<<"NextNotOK"<<" "<<pos.x<<" "<<pos.y<<" "<<pos.di<<endl;
            mazePath();
        }
    }
    else
    {
        //该位置不同,从栈顶删除,且mg[][]=0 
        //ff<<"Back"<<" "<<pos.x<<" "<<pos.y<<" "<<pos.di<<endl;
        posStack.pop();
        maze[pos.y][pos.x] = 0;
        mazePath();
    }
    
    
}
void printMazePath()
{
    stack <Pos> backupStack;
    ff<<"Path:"; 
    while(!posStack.empty())
    {
        Pos pos = posStack.top();
        posStack.pop();
        backupStack.push(pos);
    }
    while(!backupStack.empty())
    {
        Pos pos = backupStack.top();
        backupStack.pop();
        posStack.push(pos);
        ff<<"<"<<pos.x<<","<<pos.y<<"> ";
    }
    ff<<endl;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值