zoukankan      html  css  js  c++  java
  • 迷宫问题

    原题链接

    题解

    因为每一条边的权重都是1,所以可以使用bfs查找最短路径,题目中要求的要记录最短路径并打印出来,利用一个二维数组记录该点是由哪一个点拓展而来(bfs找到的时候就是最短路径)

    代码如下

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <stack>
    #include <map>
    #include <queue>
    #include <sstream>
    #include <set>
    #include <cctype>
    #define mem(a,b) memset(a,b,sizeof(a))
    
    using namespace std;
    
    typedef pair<int, int> PII;
    const int N = 10;
    int g[N][N];
    int d[N][N];
    PII q[N * N], st[N][N];//st是记录到底的点的上一个点的坐标是什么
    int dx[4]={0,0,-1,1},dy[4]{-1,1,0,0};
    
    vector<PII> bfs(){
        int hh = 0, tt = 0;
        while(hh <= tt){
           auto t = q[hh ++];
           for(int i = 0; i < 4; ++i){
               int x = t.first + dx[i], y = t.second + dy[i];
                if(x >= 0 && x < 5 && y >= 0 && y < 5 && g[x][y] == 0 && d[x][y] == 0){
                    d[x][y] = 1;
                    st[x][y] = t;//记录这个点是由哪个点拓展的
                    q[++ tt] = {x, y};
                }       
           }
        }
    
        vector<PII> res;
        int x = 4, y = 4;
        while(x || y){
            res.push_back({x,y});
            auto t = st[x][y];
            x = t.first, y = t.second;
        }
        res.push_back({0,0});
        reverse(res.begin(), res.end());
    
        return res;
    }
    
    int main(){
        for(int i = 0; i < 5; ++i)
            for(int j = 0; j < 5; ++j)
                cin >> g[i][j];
        
        vector<PII> res = bfs();
        for(int i = 0; i < res.size(); ++i){
            cout << "(" << res[i].first << ", " << res[i].second << ")" << endl;
        }
    
        return 0;
    }
    

    这个题目还可以使用dfs做,但是找到一条路径的时候不一定是最短路径,所以要把所有的路径找到之后,进行比较

    代码如下

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    typedef pair<int,int> PII;
    vector<PII> res;
    int g[10][10];
    bool st[10][10];
    int dx[4]{0,0,1,-1}, dy[4]{1,-1,0,0};
    
    void dfs(int x, int y, vector<PII> ans){
        if(x == 4 && y == 4){
            if(ans.size() < res.size() || res.size() == 0) res = ans;
            return;
        }
    
        for(int i = 0; i < 4; ++i){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx < 0 || nx >= 5 || ny < 0 || ny >= 5 || g[nx][ny] == 1 || st[nx][ny] == 1) continue;
            ans.push_back({nx,ny});
            st[nx][ny] = 1;
            dfs(nx, ny, ans);
            ans.pop_back();
            st[nx][ny] = 0;
        }
    }
    
    int main(){
        for(int i = 0; i < 5; ++i){
            for(int j = 0; j < 5; ++j)
            scanf("%d",&g[i][j]);
        }
    
        st[0][0] = 1;
        dfs(0,0,{1,{0,0}});
    
        for(auto it1 : res){
            cout << "(" << it1.first << ", " << it1.second << ")" << endl;
        }
    
        return 0;
    }
    

    注意原题中因为是POJ(POJ比较的年代久远了)中的题目,所以有的语法不支持

  • 相关阅读:
    Apache 虚拟主机 VirtualHost 配置
    ajax无线级刷新
    Apache中 RewriteRule 规则参数介绍
    用户注册
    用户登录
    Android 侧滑菜单
    PhontoShop CS6 视频
    ArcGis地图
    Android 汉子转换成拼音
    UTF-8 转 GBK
  • 原文地址:https://www.cnblogs.com/Lngstart/p/13172113.html
Copyright © 2011-2022 走看看