PTA-迷宮尋路(輸出最短路徑)


給定一個M行N列的迷宮圖,其中 "0"表示可通路,"1"表示障礙物,無法通行。在迷宮中只允許在水平或上下四個方向的通路上行走,走過的位置不能重復走。

5行8列的迷宮如下:

0 1 1 1 0 0 0 0
0 0 0 1 0 0 0 0
0 1 0 0 0 1 0 0
0 1 1 1 0 1 1 0
1 0 0 0 0 0 0 0

則從左上角(1,1)至右下角(5,8)的最短路徑為:

1,1--》2,1--》2,2--》2,3--》3,3--》3,4--》3,5--》4,5--》5,5--》5,6--》5,7--》5,8

題目保證每個迷宮最多只有一條最短路徑。

請輸出該條最短路徑,如果不存在任何通路,則輸出"NO FOUND".

輸入格式:

第一行,輸入M和N值,表示迷宮行數和列數。

接着輸入M行數值,其中,0表示通路,1表示障礙物。每列數值用空格符間隔。

接下來可能輸入多組迷宮數據。

當輸入M的值為-1時結束輸入。

輸出格式:

按行順序輸出路徑的每個位置的行數和列數,如 x,y

如果不存在任何路徑,則輸出"NO FOUND".

每組迷宮尋路結果用換行符間隔。

輸入樣例:

在這里給出一組迷宮。例如:

8 8	
0 0 1 0 0 0 1 0
0 0 1 0 0 0 1 0
0 0 0 0 1 1 0 0
0 1 1 1 0 0 0 0
0 0 0 1 0 0 0 0
0 1 0 0 0 1 0 0
0 1 1 1 0 1 1 0
1 0 0 0 0 0 0 0
4 4	
0 0 1 0
0 0 0 0
0 0 1 1 
0 1 0 0
-1 -1

輸出樣例:

在這里給出相應的輸出。例如:1,1

2,1
3,1
4,1
5,1
5,2
5,3
6,3
6,4
6,5
7,5
8,5
8,6
8,7
8,8

NO FOUND


【答案,雖然是滿分,但健壯性不一定,不想奮斗的,隨意復制粘貼吧!】
#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class node
{
    public:
        int x,y;
    node()
    {
        
    }    
    node(int ax,int ay)
    {
        x=ax;
        y=ay;
    }
};
int mv[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int vis[10005][10005];
int pre[10005][10005];
int main(int argc, char *argv[]) {
    int m,n;
    int aa=1;
    cin>>m>>n;
    while(m!=-1)
    {
        memset(vis,m*n,0);
        memset(pre,m*n,0);
        vector<vector<int> > v;
    for(int i=0;i<m;i++)
    {
        vector<int> t;
        v.push_back(t);
        for(int j=0;j<n;j++)
        {
            int ta;
            cin>>ta;
            v[i].push_back(ta);
        }
    }
    
    queue<node> q;
    q.push(node(0,0));
    vis[0][0]=1;
    int f=0;
    while(!q.empty())
    {
        node qf = q.front();
        if(qf.x==m-1&&qf.y==n-1) 
        {
            f=1;
            
            break;
            
        }
        q.pop();
        for(int i=0;i<4;i++)
        {
            node tt(qf.x+mv[i][0],qf.y+mv[i][1]);
            if((tt.x>=0&&tt.x<m&&tt.y>=0&&tt.y<n)&&!vis[tt.x][tt.y]&&!v[tt.x][tt.y])
            {
                vis[tt.x][tt.y] =1;
                pre[tt.x][tt.y] =qf.x*n+qf.y;
                q.push(tt);
                //cout<<tt.x<<" "<<tt.y<<endl;
            }
        }
    }
    if(f)
    {
        stack<node> sn;
        int xy = pre[m-1][n-1];
        while(xy)
        {
            
            sn.push(node(xy/n+1,xy%n+1));
            xy = pre[xy/n][xy%n];
        }
        cout<<"1,1"<<endl;
        
        while(!sn.empty())
        {
            cout<<sn.top().x<<","<<sn.top().y<<endl;
            sn.pop();
        }
        cout<<m<<","<<n<<endl<<endl;
        
    }else
    {
        cout<<"NO FOUND"<<endl;
    }
    cin>>m>>n;
    }
    
    return 0;
}

【獻給那些努力奮斗的同學兒,努力學習為國爭光!】

#include <iostream>
#include <string.h>
using namespace std;
/*
 *迷宮最短路徑
 *迷宮數組是從1 開始 
 */
int n,m,p,q,Min=9999;//迷宮n行m列,出口位置(p,q) 
int a[51][51]={0};//迷宮數組 int book[51][51]={0};//標記是否走過 int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//向下,向右,向上,向左 

int stepA[100][2]={-1};//記錄當前路徑 
int stepB[100][2]={-1};//記錄最小路徑 

void dfs(int x,int y,int step)
{
    int tx,ty,k;
    if(x==p&&y==q)
    {
        if(step<Min)
        {
            for(int t=0;t<100;t++)
            {
                if(stepA[t][0]==-1&&stepA[t][1]==-1)
                {
                    break;
                }
                stepB[t][0]=stepA[t][0];
                stepB[t][1]=stepA[t][1];
            }
            
            Min=step;
        }
        return;
    }

    for(k=0;k<=3;k++)
    {
        //計算下一點的坐標
        tx=x+next[k][0];
        ty=y+next[k][1];
        if(tx<1||tx>n||ty<1||ty>m)
            continue;

        if(a[tx][ty]==0&&book[tx][ty]==0)
        {
            stepA[step][0]=tx;
            stepA[step][1]=ty;
            
            book[tx][ty]=1;//標記已經走過了這個點
            dfs(tx,ty,step+1);//嘗試下一個點
            book[tx][ty]=0;//取消這個點的標記
        }
    }
    return;
}
int main()
{
    int startx=0,starty=0; 
    cout<<"請輸入迷宮的大小(行、列):"<<endl; 
    cin>>n>>m;
    cout<<"請輸入迷宮布局('0'代表通路,'1'代表牆):"<<endl; 
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
    }
    cout<<"請輸入起點與終點坐標:" <<endl; 
    cin>>startx>>starty>>p>>q;
    book[startx][starty]=1;
    dfs(startx,starty,0);
    if(Min==9999)//沒有最短路徑就說明沒有通路 
    {
        cout<<'\n'<<"【提示】兩點之間沒有通路!"<<endl;
        return 0; 
    } 
    cout<<"走出迷宮最少步數:"<<endl; 
    cout<<Min<<endl;
    cout<<"迷宮最短路徑:"<<endl; 
    for(int t=0;t<Min;t++)
        {
            if(t==0)
            cout<< startx<<" "<<starty<<endl;
            cout<<stepB[t][0]<<" "<<stepB[t][1]<<endl;
            
        }
    return 0;
}

 

代碼截圖:【帥不帥氣,一給我里giaogiao】

 

 

 

 

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM