今天的打卡题目是一个bfs

是泡泡 每日一题总教官
云计算领域优质创作者
2022-08-15 23:58:27
加精

啊啊

#include<bits/stdc++.h>
using namespace std;
const int N = 300001;
int h,t=1,g[501][501],st[501][501];
int fx[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
int ans[N][2];
struct node{
    int x,y;
}q[N];
int main()
{
    int n,m,a,b;
    cin>>n>>m>>a>>b;
    for(int i=1;i<=a;i++)
    {
        cin>>q[t].x>>q[t].y;
        st[q[t].x][q[t].y]=1;
        t++;
    }
    while(h<t)
    {
        h++;
        for(int i=0;i<4;i++)
        {
            int tx=q[h].x+fx[i][0],ty=q[h].y+fx[i][1];
            if(tx>0&&ty>0&&tx<=n&&ty<=m&&st[tx][ty]!=1)
            {
                t++;
                q[t].x=tx;q[t].y=ty;
                st[tx][ty]=1;
                g[tx][ty]=g[q[h].x][q[h].y]+1;
            }
        }
    }
    for(int i=1;i<=b;i++)   cin>>ans[i][0]>>ans[i][1];
    for(int i=1;i<=b;i++)   cout<<g[ans[i][0]][ans[i][1]]<<endl;
    return 0;
}

 

...全文
39 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
是泡泡 每日一题总教官 2022-08-16
  • 打赏
  • 举报
回复

是泡泡 每日一题总教官 2022-08-16
  • 打赏
  • 举报
回复

泡泡

是泡泡 每日一题总教官 2022-08-16
  • 打赏
  • 举报
回复

超级

是泡泡 每日一题总教官 2022-08-16
  • 打赏
  • 举报
回复

泡泡

是泡泡 每日一题总教官 2022-08-16
  • 打赏
  • 举报
回复

我是

51,713

社区成员

发帖
与我相关
我的任务
社区描述
和众多高校算法内卷分子,一起学习和交流算法那。浓郁的算法交流氛围,拒绝躺平,有效内卷。加入我们,私信我拉你入核心内卷群。
算法数据结构leetcode 个人社区
社区管理员
  • 执 梗
  • Dream-Y.ocean
  • ღCauchyོꦿ࿐
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

 刷题!

试试用AI创作助手写篇文章吧