c++基础:queue队列总结(利用队列实现bfs)

c++队列头文件 #include<queue>

常用方法函数

1 push

2 pop

3 size

4 empty

5 front back


1 psuh 在队列尾部插入元素,根据元素形式不同举三个常用例子

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
   queue<string>q;
   q.push("hello");
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct sum
{
    int x,y;
};
int main()
{
    queue<sum>q;
    sum a;
    a.x=5,a.y=6;
    p.push(a);
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef pair<int,int>PII;
int main()
{
    queue<PII>q;
    PII a;
    a.first=5,a.second=6;
    q.push(a);
}

2 pop 删除队头(出队)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef pair<int,int>PII;
int main()
{
    queue<PII>q;
    PII a;
    a.first=5,a.second=6;
    q.push(a);
    q.pop();
}

3 size 返回队列元素个数(int)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef pair<int,int>PII;
int main()
{
    queue<PII>q;
    PII a;
    a.first=5,a.second=6;
    q.push(a);
    cout<<q.size()<<endl;
}

4 empty 队列为空返回true,否则返回false

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef pair<int,int>PII;
int main()
{
    queue<PII>q;
    PII a;
    a.first=5,a.second=6;
    q.push(a);
    cout<<q.empty()<<endl;
}

5 font back 分别返回对头和队尾

例如:向队列分别 push添加 hello my name is Leo,
q.front()=‘hello’ q.back()=‘Leo’;

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int main()
{
    queue<string>q;
    q.push("hello");
    q.push("my");
    q.push("name");
    q.push("is");
    q.push("Leo");
    cout<<q.front()<<q.back();
}

**

利用队列实现bfs(模板题)

**
迷宫用一个 R×C 的字符矩阵来表示
字符 S 表示阿尔吉侬所在的位置,字符 E 表示奶酪所在的位置,字符 # 表示墙壁,字符 . 表示可以通行。
阿尔吉侬在 1 个单位时间内可以从当前的位置走到它上下左右四个方向上的任意一个位置,但不能走出地图边界。
输入格式
第一行是一个正整数 T,表示一共有 T 组数据。
每一组数据的第一行包含了两个用空格分开的正整数 R 和 C,表示地图是一个 R×C 的矩阵。
接下来的 R 行描述了地图的具体内容,每一行包含了 C 个字符。字符含义如题目描述中所述。保证有且仅有一个 S 和 E。
输出格式
对于每一组数据,输出阿尔吉侬吃到奶酪的最少单位时间
若阿尔吉侬无法吃到奶酪,则输出“oop!”(只输出引号里面的内容,不输出引号)。
每组数据的输出结果占一行。
数据范围
1<T≤10,
2≤R,C≤200

在这里插入图片描述

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
typedef pair<int,int>PII;
int r,c;
char st[210][210];
int ds[210][210];
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
int dfs(PII starts,PII ends)
{
    queue<PII>q;
    q.push(starts);
    ds[starts.first][starts.second]=0;
    while(q.empty()!=true)
    {
        PII tt=q.front();
        for(int i=0;i<4;i++)
        {
            int xx=tt.first+dx[i];
            int yy=tt.second+dy[i];
            if(xx==ends.first&&yy==ends.second)
            {
                ds[xx][yy]=ds[tt.first][tt.second]+1;
                return ds[tt.first][tt.second]+1;
            }
            if(xx<0||yy<0||xx>=r||yy>=c)continue;
            if(ds[xx][yy]!=-1||st[xx][yy]=='#')continue;
            PII hh={xx,yy};
            ds[xx][yy]=ds[tt.first][tt.second]+1;
            q.push(hh);
        }
        q.pop();
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>r>>c;
        for(int i=0;i<r;i++)scanf("%s",st[i]);
        PII start,end;
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                if(st[i][j]=='S')start={i,j};
                if(st[i][j]=='E')end={i,j};
            }
        }
        memset(ds,-1,sizeof ds);
        int ans=dfs(start,end);
        if(ds[end.first][end.second]==-1)cout<<"oop!"<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柯基吹泡泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值