算法经典搜索问题--迷宫

在讨论迷宫问题之前咱们先来说说bfs

BFS

广度优先搜索,顾名思义优先向同高度搜索,这就有个好处,bfs搜索出来的路径一定是最短的,如果理解了,那就直接上迷宫代码

迷宫

下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可 以通行的地方。
010000
000100
001001
110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。 对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式, 其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。 请注意在字典序中D<L<R<U。(如果你把以下文字复制到文本文件中,请务 必检查复制的内容是否与文档中的一致。在试题目录下有一个文件 maze.txt, 内容与下面的文本相同)
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一 个字符串,包含四种字母 D、U、L、R,在提交答案时只填写这个字符串,填 写多余的内容将无法得分
【题目数据】
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

上代码

#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
using namespace std;

const int N=100;

//非常好用的向量表示
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};
string s[4]={"D","U","L","R"};
struct Pos
{
    string str;
    int x;
    int y;
};
int digram[N][N];
bool chosen[N][N];
queue<Pos> q;
int main(){
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        string str;
        cin>>str;
        for(int j=0;j<str.size();j++)
            digram[i][j+1]=str[j]-'0';
    }
    q.push({"",1,1});
    while(q.size())
    {
        auto temp=q.front();
        q.pop();
        if(temp.x==n&&temp.y==m)
        {
            cout<<temp.str<<endl;
            break;
        }
        chosen[temp.x][temp.y]=true;
        for(int i=0;i<4;i++)
        {
            int x=dx[i]+temp.x;
            int y=dy[i]+temp.y;
            if(x>=1&&x<=n&&y>=1&&y<=m&&!chosen[x][y]&&!digram[x][y])
            {
                q.push({temp.str+s[i],x,y});
            }
        }
        
    }
    return 0;
}
/*
最后的答案:DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRUUURRRRDDDDRDRRRRRURRRDRRDDDRRRRUURUUUUUUUULLLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
*/

大概思路就是优先走下上左右,然后挨个枚举,思路还是比较简单的,没什么好说的,如果这题好不够爽,那么试试ZOJ 1005也是道非常经典的广搜题,这里给出ZOJ 1005的代码,就不讲了

#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
#include<set>

#define pii pair<int,int>

using namespace std;

struct Water{
    int a;
    int b;
    vector<string> v;
    Water(int aa=0,int bb=0)
    {
        a=aa;
        b=bb;
    }
};
int main(){
    //freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
    int a,b,cap;
    while(cin>>a>>b>>cap)
    {
        set<pii> s;
        s.insert({0,0});
        queue<Water> q;
        q.push(Water());
        while(q.size())
        {
            auto temp=q.front();
            q.pop();
            if(temp.b==cap)
            {
                for(auto str:temp.v)
                {
                    cout<<str<<endl;
                }
                break;
            }
            if(temp.a!=0)
            {
                auto t=temp;
                t.a=0;
                if(s.count({t.a,t.b})==0)
                {
                    s.insert({t.a,t.b});
                    t.v.push_back("empty A");
                    q.push(t);
                }
            }
            if(temp.b!=0)
            {
                auto t=temp;
                t.b=0;
                if(s.count({t.a,t.b})==0)
                {
                    s.insert({t.a,t.b});
                    t.v.push_back("empty B");
                    q.push(t);
                }
            }
            if(temp.a!=a)
            {
                auto t=temp;
                t.a=a;
                if(s.count({t.a,t.b})==0)
                {
                    s.insert({t.a,t.b});
                    t.v.push_back("fill A");
                    q.push(t);
                }
            }
            if(temp.b!=b)
            {
                auto t=temp;
                t.b=b;
                if(s.count({t.a,t.b})==0)
                {
                    s.insert({t.a,t.b});
                    t.v.push_back("fill B");
                    q.push(t);
                }
            }
            if(temp.a!=0&&temp.b!=b)
            {
                auto t=temp;
                t.a=t.a-min(temp.a,b-temp.b);
                t.b=t.b+min(temp.a,b-temp.b);
                if(s.count({t.a,t.b})==0)
                {
                    s.insert({t.a,t.b});
                    t.v.push_back("pour A B");
                    q.push(t);
                }
            }
            if(temp.b!=0&&temp.a!=a)
            {
                auto t=temp;
                t.a=t.a+min(temp.b,a-temp.a);
                t.b=t.b-min(temp.b,a-temp.a);
                if(s.count({t.a,t.b})==0)
                {
                    s.insert({t.a,t.b});
                    t.v.push_back("pour B A");
                    q.push(t);
                }
            }
        }
        puts("success");
    }

    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
程序在VC++ 6下顺利编译通过。 一、 实验目的: (1) 熟练掌握链栈的基本操作及应用。 (2) 利用链表作为栈的存储结构,设计实现一个求解迷宫的非递归程序。 二、实验内容: 【问题描述】 以一个m×n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对信任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。 【基本要求】 首先实现一个链表作存储结构的栈类型,然后编写一个求解迷宫的非递归程序。求得的通路以三元组(i,j,d)的形式输出,其中:(i,j)指示迷宫中的一个坐标,d表示走到下一坐标的方向。如:对于下列数据的迷宫,输出的一条通路为:(1,1,1),(1,2,2),(2,2,2),(3,2,3),(3,1,2),……。 【测试数据】 迷宫的测试数据如下:左上角(1,1)为入口,右下角(8,9)为出口。 1 2 3 4 5 6 7 8 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0 以方阵形式输出迷宫及其通路。 输出: 请输入迷宫的长和宽:5 5 请输入迷宫内容: 0 1 1 0 0 0 0 1 1 0 1 0 0 1 1 1 0 0 1 0 1 1 0 0 0 迷宫的路径为 括号内的内容分别表示为(行坐标,列坐标,数字化方向,方向) (1,1,1,↓) (2,1,2,→) (2,2,1,↓) (3,2,1,↓) (4,2,2,→) (4,3,1,↓) (5,3,2,→) (5,4,2,→) (5,5,0,) 迷宫路径探索成功!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值