搜索
热搜: NOIP OIer 神牛
查看: 281|回复: 0

123456

[复制链接]

主题

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2023-6-3 15:47:06 | 显示全部楼层 |阅读模式
#include<bits/stdc++.h>
using namespace std;
#define N 110
struct pos{
        int x,y;
};
int cnt=0;
int n,m,xx,yy;
int maze[N][N];
bool vis[N][N];
int dr[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
void bfs(){
        if(maze[xx][yy]==0) {
                return;
        }
        queue<pos>q;
        pos a;
        a.x=xx;
        a.y=yy;
        cnt++;
        vis[xx][yy]=true;
       
        q.push(a);
        while (!q.empty()){
                a=q.front();
                q.pop();
                for(int i=0;i<4;i++){
                        int nx=a.x+dr[i][0];
                        int ny=a.y+dr[i][1];
                        if(nx>=0&&nx<n&&ny>=0&&ny<m&&maze[nx][ny]>0&&!vis[nx][ny]){
                                cnt++;
                                 pos np;
                                 np.x=nx;
                                 np.y=ny;
                                 q.push(np);
                                 vis[nx][ny]=true;
                        }
                }
               
        }
}
int main(){
        cin>>n>>m;
        cin>>xx>>yy;
        for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                        cin>>maze[i][j];
                }
        }
        bfs();
        cout<<cnt;
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

津ICP备19006949号-1 | 津公网安备12010102000465号

快速回复 返回顶部 返回列表