Educational Codeforces Round 1 D. Igor In the Museum(BFS)

题目链接
题意:给你一个图,再给你几个点,然后问这个点能到达的*有几个。
解答:BFS,为了避免超时,我们BFS某一个点的时候,可以把这个点到达的’.’点编上序号,最后记下这个序号对应的能到达的墙数,这样我们读入一个点,如果已经算出,就不用再BFS了。


#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define X first
#define Y second
#define cl(a,b) memset(a,b,sizeof(a))
typedef pair<int,int> P;
const int maxn=1005;
const int inf=1<<27;
#define mod 1000000007

char a[maxn][maxn];

int dir[][2]={1,0,0,1,-1,0,0,-1};
int vis[maxn][maxn];//标记的同时,编上BFS的序号
int num[100005];//记下序号对应的值
int n,m,k;
int tt;
void bfs(int x,int y){
    queue<P> q;
    q.push(P(x,y));
    //cl(vis,-1);
    vis[x][y]=tt;
    int ans=0;
    while(!q.empty()){
        P t=q.front();q.pop();
        for(int i=0;i<4;i++){
            int xx=t.X+dir[i][0];
            int yy=t.Y+dir[i][1];
            if(xx<0||xx>=n||yy<0||yy>=m||vis[xx][yy])continue;
            if(a[xx][yy]=='*'){
                ans++;continue;
            }
            q.push(P(xx,yy));
            vis[xx][yy]=tt;
        }
    }
    num[tt]=ans;
}
int main(){
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<n;i++){
        scanf("%s",a[i]);
    }
    tt=1;
    while(k--){
        int x,y;
        scanf("%d%d",&x,&y);
        x--;y--;
        if(vis[x][y]){printf("%d\n",num[vis[x][y]]);continue;}
        bfs(x,y);
        printf("%d\n",num[tt]);tt++;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值