求写一个 迷宫问题找路线的c++ 程序

2025-01-04 10:35:15
推荐回答(2个)
回答1:

你的描述跟图不一样啊。。反正这是按照你的描述写的
这是我的程序的限定:路径从左到右(你这里没有设定)
#include
#include
#include
using namespace std;
char map[55][55];
bool vis[55][55];
int dx[5]={0,0,0,-1,1},dy[5]={0,-1,1,0,0};
vector path;
int n,m;
char NO='X';
inline void dfs(int x,int y)
{
vis[x][y]=true;
path.push_back(map[x][y]);
if(y==m)
{
for(int i=0;i printf("%c ",path[i]);
putchar('\n');
return;
}
for(int i=1;i<=4;i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(nx<=n && nx>=1 && ny<=m && ny>=1 && !vis[nx][ny] && map[nx][ny]!=NO)
dfs(nx,ny);
}
vis[x][y]=true;
path.pop_back();
}
int main()
{
n=15,m=15;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
map[i][j]=getchar();
while(map[i][j]=='\n')
map[i][j]=getchar();
}
for(int i=1;i<=n;i++)
if(!vis[i][1] && map[i][1]!=NO)
dfs(i,1);
return 0;
}

回答2:

可以参考下

//http//////www.itpon.com/web/2014/cpp_0621/1623.html

#include "stdafx.h"  
#include   
#include   
#include   
#include   
using namespace std;  
int box[8][10]={{1,1,1,1,1,1,1,1,1,1},  
{1,0,1,1,1,0,1,1,1,1},  
{1,1,0,1,0,1,1,1,1,1},  
{1,0,1,0,0,0,0,0,1,1},  
{1,0,1,1,1,0,1,1,1,1},  
{1,1,0,0,1,1,0,0,0,1},  
{1,0,1,1,0,0,1,1,0,1},  
{1,1,1,1,1,1,1,1,1,1}};  
struct sPoint  
{  
    sPoint(int x1,int y1){x=x1;y=y1;}  
    int x;  
    int y;  
};   
void calc(int *box,int width,int height,sPoint start,sPoint end)  
{  
    list s;  
    box[start.x*width+start.y]=2;  
    int x=start.x,y=start.y;  
    s.push_back(sPoint(x,y));  
    while(1)  
    {  
        if(x==end.x && y==end.y)  
        {  
            while (!s.empty())  
            {  
                cout<                s.pop_front();  
            }  
            break;  
        }  
        else  
        {  
            //cout<        }  
        bool bGo=false;  
        for(int i=0;i<8;i++)  
        {  
            switch(i)  
            {   
            case 0:  
                {  
                    if(y>0 && box[(y-1)*width+x]==0)  
                    {  
                        y=y-1;  
                        box[y*width+x]=2;  
                        s.push_back(sPoint(x,y));  
                        bGo=true;  
                    }  
                    break;  
                }  
            case 1:  
                {  
                    if(x0 && box[(y-1)*width+x+1]==0)  
                    {  
                        x=x+1;  
                        y=y-1;  
                        box[y*width+x]=2;  
                        s.push_back(sPoint(x,y));  
                        bGo=true;  
                    }  
                    break;  
                }  
            case 2:  
                {  
                    if(x                    {  
                        x=x+1;  
                        box[y*width+x]=2;  
                        s.push_back(sPoint(x,y));  
                        bGo=true;  
                    }  
                    break;  
                }  
            case 3:  
                {  
                    if(x                    {  
                        x=x+1;  
                        y=y+1;  
                        box[y*width+x]=2;  
                        s.push_back(sPoint(x,y));  
                        bGo=true;  
                    }  
                    break;  
                }  
            case 4:  
                {  
                    if(y                    {  
                        y=y+1;  
                        box[y*width+x]=2;  
                        s.push_back(sPoint(x,y));  
                        bGo=true;  
                    }  
                    break;  
                }  
            case 5:  
                {  
                    if(x>0 && y                    {  
                        x=x-1;  
                        y=y+1;  
                        box[y*width+x]=2;  
                        s.push_back(sPoint(x,y));  
                        bGo=true;  
                    }  
                    break;  
                }  
            case 6:  
                {  
                    if(x>0 && box[y*width+x-1]==0)  
                    {  
                        x=x-1;  
                        box[y*width+x]=2;  
                        s.push_back(sPoint(x,y));  
                        bGo=true;  
                    }  
                    break;  
                }  
            case 7:  
                {  
                    if(x>0 && y>0 && box[(y-1)*width+x-1]==0)  
                    {  
                        x=x-1;  
                        y=y-1;  
                        box[y*width+x]=2;  
                        s.push_back(sPoint(x,y));  
                        bGo=true;  
                    }  
                    break;  
                }  
            }  
            if(bGo)  
            {  
                break;  
            }  
        }  
        if(!bGo)  
        {  
            if(s.size()>0)  
            {  
                sPoint p=s.back();  
                s.pop_back();  
                x=p.x;  
                y=p.y;  
            }else  
            {  
                cout<<"error"<                break;  
            }  
              
        }  
    }  
}  
int main(int argc, _TCHAR* argv[])  
{  
    calc((int*)box,10,8,sPoint(1,1),sPoint(8,6));  
    return 0;
}