[蓝桥杯2019初赛]试题D:迷宫

13 篇文章 0 订阅
5 篇文章 0 订阅

在这里插入图片描述
呜呜呜 这题做了我好久好久,一开始拿DFS写了半天,写是写出来了,但是实在是太大了,笑死,根本搜不出来,后来又在网上看各种各样的题解,总算给我给搞出来了,因为BFS本身没有DFS掌握的牢固,所以有些不太敢拿BFS写,晚上要把BFS的基本功加强一下,加油加油!

#include<queue>
#include<iostream>
using namespace std;
int n=30,m=50;
string mp[35];
bool vis[35][55];
int dir[4][2]={{1,0},{0,-1},{0,1},{-1,0}};
struct node
{
    int x,y,step;
    string str;
    node(int _x,int _y,int _step,string _str)
    {
        x=_x;
        y=_y;
        step=_step;
        str=_str;
    }
};


int main()
{
    for(int i=0;i<n;i++)
    {
        cin>>mp[i];
    }
    string s="";
    queue<node> q;
    q.push(node(0,0,0,""));
    vis[0][0]=true;
    while(!q.empty())
    {
        node now=q.front();
        q.pop();
        if(now.x==n-1&&now.y==m-1)
        {
            cout<<now.step<<endl;
            cout<<now.str<<endl;
        }
        for(int i=0;i<4;i++)
        {
            int tx=now.x+dir[i][0];
            int ty=now.y+dir[i][1];
            if(tx<0||tx>=n||ty<0||ty>=m||vis[tx][ty]||mp[tx][ty]=='1')
            {
                continue;
            }
            if(i==0)s="D";
            else if(i==1)s="L";
            else if(i==2)s="R";
            else s="U";
        vis[tx][ty]=true;
        q.push(node(tx,ty,now.step+1,now.str+s));
        }
    }
    
    
    
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值