走迷宫sdut1269(附带dfs求所有路径与bfs求最佳路径)

本文介绍了一种使用深度优先搜索(DFS)解决迷宫寻路问题的高效算法,并提供了详细的代码实现。此外,还探讨了如何利用广度优先搜索(BFS)寻找最优路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录这道题的原因是这个题的解法是一个很好的模板,特别是用数组表示方向,然后用循环进行操作,节省了不少代码量,比我之前的方法不知道高到哪里去了(噗噗噗),迷宫的问题好多可以在这基础上进行更改,值得记录。
Problem Description

有一个m*n格的迷宫(表示有m行、n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,输入这m*n个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。如果一条路都不可行,则输出相应信息(用-1表示无路)。
Input

第一行是两个数m,n(1< m, n< 15),接下来是m行n列由1和0组成的数据,最后两行是起始点和结束点。
Output

所有可行的路径,输出时按照左上右下的顺序(画重点)。描述一个点时用(x,y)的形式,除开始点外,其他的都要用“->”表示。如果没有一条可行的路则输出-1。
Example Input

5 4
1 1 0 0
1 1 1 1
0 1 1 0
1 1 0 1
1 1 1 1
1 1
5 4
Example Output

(1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(1,2)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(5,4)
(1,1)->(2,1)->(2,2)->(3,2)->(4,2)->(5,2)->(5,3)->(5,4)

#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
int map[1024][1024];
int arr[1024][1024];
int lx[] = { 0,-1,0,1 };
int ly[] = { -1,0,1,0 };
struct node {
    int x; int y;
    node() {}
    node (int a,int b):x(a),y(b){}
}a,b;
vector<node>vec;
int m, n,sum=0;
void dfs(int x,int y) {
    if (x == b.x&&y == b.y) {
        int i = 0;
        for (auto iter : vec) {
            i < vec.size()-1 ?
                cout << "(" << iter.x << "," << iter.y << ")" << "->" :
                cout << "(" << iter.x << "," << iter.y << ")" << endl;
            i++;
            sum++;
        }
        return;
    }
    else {
        for (int i = 0; i < 4; i++) {
            if (x + lx[i] <= m&&x + lx[i] >= 0 && y + ly[i] <= n&&y + ly[i] >= 0
                && map[x + lx[i]][y + ly[i]] == 1 && arr[x + lx[i]][y + ly[i]] == 0) {
                arr[x + lx[i]][y + ly[i]] = 1;
                vec.push_back(node(x + lx[i], y + ly[i]));
                dfs(x + lx[i], y + ly[i]);
                vec.pop_back();
                arr[x + lx[i]][y + ly[i]] = 0;
            }
        }
    }
}
int main(void) {
    memset(arr, 0, sizeof(arr));
    memset(map, 0, sizeof(map));
    cin >> m >> n;
    for(int i=1;i<=m;i++)
        for (int j = 1; j <= n; j++) {
            cin >> map[i][j];
        }
    cin >> a.x >> a.y >> b.x >> b.y;
    vec.push_back(node(a.x, a.y));
    arr[a.x][a.y] = 1;
    dfs(a.x, a.y);if(!sum)cout << -1;
    //system("pause");
    return 0;
}

/***************************************************
User name: 
Result: Accepted
Take time: 0ms
Take Memory: 2260KB
Submit time: 
****************************************************/

/*******************我是分割线**************/

在这再写一个关于bfs求最佳路径的方法并记录路径。方法是在结构体里加入一个指向指针。

#include<iostream>
#include<queue>
#include<stack>
using namespace std;
struct node {
    int x, y;
    node *pre;
};
int flag[1024][1024];
int ma[1024][1024];
int _move[4][2] = { {0,1},{1,0} ,{0,-1}, {-1,0} };
int m,n;
void bfs() {
    queue<node*>Q;
    stack<node>vec;
    node *t=new node;
    t->x = 0, t->y = 0,t->pre=nullptr;
    Q.push(t);
    while (!Q.empty()) {
        t = Q.front(); Q.pop();
        if (t->x == m - 1 && t->y == n - 1) {
            node *h = t;
            cout << "get" << endl;
            while (h) {
                vec.push(*h);
                cout << h->x << " " << h->y << endl;
                h = h->pre;
            }
            cout << endl;
            while (!vec.empty()) {
                auto iter = vec.top();
                vec.pop();
                cout << iter.x << " " << iter.y << endl;
            }
            return;
        }
        for (int i = 0; i < 4; i++) {
            node *v=new node;
            v->x = t->x + _move[i][0];
            v->y = t->y + _move[i][1];
            if (v->x < m&&v->x >= 0 && v->y < n&&v->y >= 0
                && !flag[v->x][v->y] && ma[v->x][v->y] == 1) {
                flag[v->x][v->y] = 1;
                Q.push(v);
                v->pre = t;
            }
        }
    }
    cout << "wrong";
}
int main(void) {
    cin >> m >> n;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            cin >> ma[i][j];
    bfs();
    system("pause");
    return 0;
}

附上两个关于bfs的博客:
http://blog.youkuaiyun.com/raphealguo/article/details/7523411

http://blog.youkuaiyun.com/acmman/article/details/38342115

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值