跳到正文

个人记录

jkxzdflksdzh

SEF_711约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年5月8日
更新
2026年5月8日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;

const int N = 105;
int n, m;
int ans;
char a[N][N];
bool vis[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
struct node
{
	int x, y, step;
};
void bfs()
{
	queue<node> q;
	memset(vis, 0, sizeof vis);
	for(int i = 1; i <= n; i ++)
		for(int j = 1; j <= n; j ++)
			if(a[i][j] == '@')
			{
				q.push({i, j, 0});
				vis[i][j] = 1;
			}
	while(q.empty() == 0)
	{
		node cur = q.front();
		q.pop();
		if(cur.step == m)
			return;
		ans ++;
		for(int i = 0; i < 4; i ++)
		{
			int nx = cur.x + dx[i];
			int ny = cur.y + dy[i];
			if(nx <= 0 || ny <= 0 || nx > n || ny > n)
				continue;
			if(a[nx][ny] == '#')
				continue;
			if(vis[nx][ny] == 1)
				continue;
			vis[nx][ny] = 1;
			q.push({nx, ny, cur.step + 1});
		}
	}
}
int main()
{
	cin >> n;
	for(int i = 1; i <= n; i ++)
		for(int j = 1; j <= n; j ++)
			cin >> a[i][j];
	cin >> m;
	bfs();
	cout << ans;
	return 0;
}
原文