bfs广搜

p1443 马的遍历 

https://www.luogu.com.cn/problem/P1443

啊啊啊 纪念广搜第一道题!

ac代码:

#include<iostream>
#include<cstring>
#include<cstdio>//用printf的时候这个要加上 不然洛谷会显示编译错误 不知道为什么编译器就可以www
#include<queue>
int main()
{
    using namespace std;
    int n,m,x,y;
    int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};//每步都有八个方向可以走
    bool visit[500][500];//判断走没走过
    int step[500][500];//存储步数
    //因为本题存储的是坐标 所以这里所有数组都从开始存 变量直接代表坐标
    while(cin>>n>>m>>x>>y)
    {
        queue<pair<int,int> >q;//这里 两个>中间一定要加上空格 不然洛谷上会编译错误
        memset(visit,false,sizeof(visit));//原来memset也可以置false
        memset(step,-1,sizeof(step));
        step[x][y]=0;//起点到起点的步数当然是0啦
        visit[x][y]=true;//这样起点的步数就一直是0不会再被访问了
        q.push(make_pair(x,y));//把起点入队
        while(!q.empty())
        {
            int xx=q.front().first;
            int yy=q.front().second;
            q.pop();//当前点出队
            for(int i=0;i<8;++i)//依次历经能去的八个点
            {
                int u=xx+dir[i][0];
                int v=yy+dir[i][1];
                if(u<1||u>n||v<1||v>m||visit[u][v]==true)//如果超过边界或者已经遍历过这个点 就去看下一个点
                    continue;
                visit[u][v]=true;//标记已经访问过该点了
                q.push(make_pair(u,v));//把该点入队
                step[u][v]=step[xx][yy]+1;步数+1
            }
        }
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=m;++j)
                printf("%-5d",step[i][j]);
            cout<<endl;
        }
    }
    return 0;
}

突然发现自己没有标记输出-1 这题就过了耶

p1135 奇怪的电梯 

https://www.luogu.com.cn/problem/P1135

上面那题其实是一边看着别人的代码一边写的 这一题是自己写出来的!好感动wwww

ac代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
int main()
{
    using namespace std;
    int k[205];//记录每一层的Ki
    bool visit[205];//标记该楼层有没有被访问过 也可以理解为该楼层的步数有没有被记录过
    int step[205];//记录步数
    bool flag;//标记有没有到达最终楼层
    int current;//记录当前楼层
    int N,A,B;
    while(cin>>N>>A>>B)
    {
        for(int i=1;i<=N;++i)
            cin>>k[i];
        memset(visit,false,sizeof(visit));
        queue<int>q;//这个记得要放在里面 因为每次都队列都要初始化
        flag=false;
        q.push(A);//把起点入队
        visit[A]=true;//访问过起点了
        step[A]=0;//起点到起点的步数为0
        while(!q.empty())
        {
            current=q.front();
            if(current==B)
            {
                flag=true;
                break;
            }
            q.pop();
            int up=current+k[current];//向下
            if(up<=N&&visit[up]==false)
            {
                q.push(up);
                visit[up]=true;
                step[up]=step[current]+1;
            }
            int down=current-k[current];//向上
            if(down>=1&&visit[down]==false)
            {
                q.push(down);
                visit[down]=true;
                step[down]=step[current]+1;
            }
        }
        if(flag==true)
            cout<<step[B]<<endl;
        else
            cout<<-1<<endl;
    }
    return 0;
}

讲道理 暂时也只写了这两道广搜 没有什么感触和对这些题目的理解..

等我以后接触多了再来!


现在学图学广搜学到了一种新的模板 是用邻接表的方式存储图

847. 图中点的层次 - AcWing题库

代码:

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N=1e5+10;
int h[N],e[N],ne[N],idx=0;
int dis[N];
int n,m;
void add(int a,int b)//建边
{
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}

int bfs(int u)
{
    queue<int>q;//这里也可以用数组模拟队列
    memset(dis,-1,sizeof(dis));
    q.push(u);
    dis[u]=0;
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        for(int i=h[t];i!=-1;i=ne[i])
        {
            int j=e[i];
            if(dis[j]==-1)
            {
                dis[j]=dis[t]+1;
                q.push(j);
            }
        }
    }
    return dis[n];
}
int main()
{
    cin>>n>>m;
    memset(h,-1,sizeof(h));//这个要放到add前面
    int a,b;
    while(m--)
    {
        cin>>a>>b;
        add(a,b);
    }
    cout<<bfs(1)<<endl;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值