蒟蒻求助
查看原帖
蒟蒻求助
1053122
shy_lihui楼主2024/12/19 22:16
#include<bits/stdc++.h>

using namespace std;
int n,m,d;
char a[1005][1005];
bool vis[1005][1005];
int ans;
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
struct Node
{
	int x;
	int y;
	int step;
};
queue<Node>q;
void bfs()
{
	while(!q.empty())
	{
		Node noww=q.front();
		q.pop();
		if(noww.step==0)
		{
			continue;
		}
		for(int i=0;i<4;i++)
		{
			int xx=noww.x+dx[i];
			int yy=noww.y+dy[i];
			if(xx>=1 && xx<=n && yy>=1 && yy<=m && a[xx][yy]!='#')
			{
				if(vis[xx][yy]==0)
				{
					ans++;
				}
				vis[xx][yy]=1;
				q.push({xx,yy,noww.step-1});
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m>>d;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>a[i][j];
			if(a[i][j]=='H')
			{
				q.push({i,j,d});
				vis[i][j]=1;
			}
		}
	}
	ans+=q.size(); 
	bfs();
	cout<<ans;
	return 0;
}
2024/12/19 22:16
加载中...