AcWing 1113 红与黑 题解(蓝桥杯 BFS)

比较板子的一道广搜题,注意数据给的模型m、n不止一组,注意读入,注意给的m、n和常规的行数和列数是相反的
AcWing 1113 红与黑

#include<bits/stdc++.h>

using namespace std;

#define x first
#define y second

typedef pair<int, int>PII;

const int N = 25;

char g[N][N];
int ans;
int n, m;
bool st[N][N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int main()
{
	while(scanf("%d%d", &m, &n) != EOF){
		if(m == 0 && n == 0) break;
		ans = 0;
		memset(st, 0, sizeof st);
		queue<PII>q;
		bool flag = false;
		for(int i = 1; i <= n; i ++ ){
			for(int j = 1; j <= m; j ++ ){
				cin>>g[i][j];
				if(g[i][j] == '@'){
					ans ++ ;
					q.push({i, j});
					st[i][j] = true;
				}	
			}
		}
		
		while(!q.empty()){
			auto op = q.front();
			q.pop();
			int x1 = op.x, y1 = op.y;
			for(int i = 0; i < 4; i ++ ){
				int xx = x1 + dx[i];
				int yy = y1 + dy[i];
				if(xx >= 1 && xx <= n && yy >= 1 && yy <= m && !st[xx][yy] && g[xx][yy] == '.'){
					st[xx][yy] = true;
					q.push({xx, yy});
					ans ++ ;
					flag = true;
				}
			}
		}		
		cout<<ans<<endl;		
	}	
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值