递归求迷宫问题输出文件

#include <iostream>
#include <fstream>
using namespace std;
#define M 10
#define N 10
#define MaxSize 100
int mg[M][N]=
{
    {1,1,1,1,1,1,1,1,1,1},
    {1,0,0,1,0,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,1,1}
};
ofstream ocout;
struct 
{    int i;
int j;
}st[MaxSize];
int num=1;
void PathMap(int x1,int y1,int x2,int y2,int d)
{
    if (d==0)
    {
        st[d].i=x1;
        st[d].j=y1;
    }
    int di=1;
    mg[x1][y1]=-1;
    if (x1==x2&&y1==y2)
    {
        ocout.open("c:\\li.txt",ios::app|ios::out);
        ocout<<""<<num<<"条路线如下:"<<"\n";        
        for (;d>0;d--)
        {            
            ocout<<st[d].i<<" "<<st[d].j<<"\n";
        }
        ocout.close();
        num++;

    }
    else
        while(di<5)
        {
            if (di==1)
            {
                st[d+1].i=st[d].i-1;
                st[d+1].j=st[d].j;
                di=di+1;
                if (mg[st[d+1].i][st[d+1].j]!=-1&&mg[st[d+1].i][st[d+1].j]!=1)
                {
                    PathMap(st[d+1].i,st[d+1].j,x2,y2,d+1);
                }
            }
            else if (di==2)
            {
                st[d+1].i=st[d].i;
                st[d+1].j=st[d].j+1;
                di=di+1;
                if (mg[st[d+1].i][st[d+1].j]!=-1&&mg[st[d+1].i][st[d+1].j]!=1)
                {
                    PathMap(st[d+1].i,st[d+1].j,x2,y2,d+1);
                }
            }
            else if (di==3)
            {
                st[d+1].i=st[d].i+1;
                st[d+1].j=st[d].j;
                di=di+1;
                if (mg[st[d+1].i][st[d+1].j]!=-1&&mg[st[d+1].i][st[d+1].j]!=1)
                {
                    PathMap(st[d+1].i,st[d+1].j,x2,y2,d+1);
                }
            }
            else
            {
                st[d+1].i=st[d].i;
                st[d+1].j=st[d].j-1;
                di=di+1;
                if (mg[st[d+1].i][st[d+1].j]!=-1&&mg[st[d+1].i][st[d+1].j]!=1)
                {
                    PathMap(st[d+1].i,st[d+1].j,x2,y2,d+1);
                }                
            }
        }
        mg[x1][y1]=0;
}
int main()
{
    PathMap(1,1,8,8,0);
}

 

转载于:https://www.cnblogs.com/lisongfeng9213/p/3378904.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值