广搜玄学WA73求助(帮助必关)
查看原帖
广搜玄学WA73求助(帮助必关)
601747
xibaohe楼主2023/3/11 14:32

rt,蒟蒻在别的刷题网站上面过了,求助大佬!

#include<iostream>
#include<queue>
using namespace std;

int n, m, x1, y1, ans;
char mp[505][505];
bool vis[505][505];
int dx[8] = {0, 0, 1, -1,1,1,-1,-1};
int dy[8] = {1, -1, 0, 0,-1,1,-1,1};

struct node
{
	int x, y, t;
};

queue<node> q;

int bfs()
{
	int t;
	q.push(node{x1, y1, 0});
	vis[x1][y1] = true;
	while(!q.empty())
	{
		int x = q.front().x, y = q.front().y;
		t = q.front().t;
		q.pop();
		for(int i = 0; i <= 7; i++)
		{
			int x_new = x + dx[i], y_new = y + dy[i];
			if(x_new < 1 || x_new > n || y_new < 1 || y_new > m) continue;
			if(vis[x_new][y_new] == true) continue;
			if(mp[x_new][y_new] == '*') continue;
			ans--;
			q.push(node{x_new, y_new, t + 1});
			vis[x_new][y_new] = true;
		}
	}
	return t;
}

int main()
{
	cin >> n >> m >> x1 >> y1;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
		{
			cin>>mp[i][j];
			if(mp[i][j]=='.') ans++;
		}
	ans--;
	cout << bfs() << endl;
	return 0;
}
2023/3/11 14:32
加载中...