• 迷宫问题 POJ


    定义一个二维数组: 
    int maze[5][5] = {
     	0, 1, 0, 0, 0,
     	0, 1, 0, 1, 0,
     	0, 0, 0, 0, 0,
     	0, 1, 1, 1, 0,
     	0, 0, 0, 1, 0,
     };
    它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

    Input

    一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

    Output

    左上角到右下角的最短路径,格式如样例所示。

    Sample Input

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

    Sample Output

    (0, 0)
    (1, 0)
    (2, 0)
    (2, 1)
    (2, 2)
    (2, 3)
    (2, 4)
    (3, 4)
    (4, 4)
    记录路径的题目,这里是用双向队列做的
    #include<iostream>
    #include<queue>
    #include<map>
    #include <deque>//双向队列
    using namespace std;
    int mapn[8][8],vis[8][8];
    struct node{
        int x,y;
    };
    struct ss{
        deque<node> qq;//队列套队列
    };
    int dx[]={0,0,1,-1},dy[]={1,-1,0,0},num;
    ss bfs(){
        node p;
        p.x = 1,p.y = 1;
        ss qz;
        qz.qq.push_back(p);
        vis[1][1] = 1;
        queue<ss> q;
        q.push(qz);
        num = 0;
        while(!q.empty()){
            ss mm = q.front();
            node tmp = mm.qq.back();
            q.pop();
            if(tmp.x == 5 && tmp.y == 5){
                return mm;
            }
            for(int i=0;i<4;i++){
                int xx = tmp.x + dx[i];
                int yy = tmp.y + dy[i];
                if(!vis[xx][yy] && mapn[xx][yy] == 0 && xx>0 && xx<=5 && yy>0 && yy<=5){
                    vis[xx][yy] = 1;
                    ss tm = mm;
                    node tp = tm.qq.back();
                    tp.x = xx,tp.y = yy;
                    tm.qq.push_back(tp);
                    q.push(tm);
                }
            }
        }
        return q.front();
    }
    int main(){
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5;j++){
                cin >> mapn[i][j];
            }
        }
        ss q = bfs();
        while(!q.qq.empty()) {
            node sl = q.qq.front();
            q.qq.pop_front();//取前面删除前面
            cout << "(" << sl.x-1 << ", " << sl.y-1 << ")" << endl;
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    Java集合框架知多少——干货!!!
    Java基础小记
    初识Java
    HTML5入门必知
    密码技术小结
    [MDK]Keil在下载程序一直提示更新J-Link
    Python 循环
    2021年7月14日
    bzoj 2653 middle (主席树+二分)
    bzoj 3932 [CQOI2015]任务查询系统 (主席树)
  • 原文地址:https://www.cnblogs.com/l609929321/p/7531703.html
Copyright © 2020-2023  润新知