跳到正文

题解

P7663

max0810约 3 分钟
原文

这是 2026年4月29日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2021年7月9日
收录
2026年4月29日
更新
2026年11月11日
篇幅
约 3 分钟
题目
P7663

版本

  1. v2当前
  2. v1

思路还是比较简单的,完全没有蓝题的难度。

考虑bfs,每遇到一个点就往外广度搜索,去找最近的点。

时间看起来比较多,但到了后面有很多树,每个点的时间是非常快的,再加点优化,时间其实差不多是O(g)的。

具体请见代码:

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node
{
	int x,y;
}now,nex;
const int dir[4][2] = {0,-1,-1,0,0,1,1,0};
char a[505][505];
int r,s,g;
bool vis[505][505];
int f(int x1,int y1,int x2,int y2)//算距离 
{
	return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
}
int bfs(int x,int y)//bfs模板不用多讲吧 
{
	memset(vis,0,sizeof vis);
	queue<node> que;
	int ans = 0x7ffffff;
	now.x = x;now.y = y;
	que.push(now);
	while(!que.empty())
	{
		now = que.front();
		que.pop();
		if(f(now.x,now.y,x,y)>ans)continue;//比最小值大就没必要枚举下去了(优化) 
		for(int i = 0;i < 4;i++)
		{
			int xx = now.x+dir[i][0];
			int yy = now.y+dir[i][1];
			if(xx<1||xx>r||yy<1||yy>s||vis[xx][yy])continue;
			vis[xx][yy] = 1;
			int s = f(xx,yy,x,y);
			if(s >= ans)continue;//同上 
			if(a[xx][yy] == 'x')ans = s;//注意只有是当前位置有苹果树才更新答案 
			nex.x = xx;nex.y = yy;
			que.push(nex);
		}
	}
	return ans;
}
int main()
{
//	freopen("jabuke.in","r",stdin);
//	freopen("jabuke.out","w",stdout);
	cin >> r >> s;
	for(int i = 1;i <= r;i++)
		for(int j = 1;j <= s;j++)
			cin >> a[i][j];
	cin >> g;
	while(g--)
	{
		int x,y;
		cin >> x >> y;
		if(a[x][y] == 'x')//如果这个点是x就直接输出0(优化) 
		{
			cout << "0\n";
			continue;
		}
		cout << bfs(x,y) << endl;
		a[x][y] = 'x';//一定要记得 
	}
	return 0;
}