璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


icpc:problems:usaco21feb_comfortable_cows_s
problems
名称Comfortable Cows S
题目编号USACO21FEB_S2
题目链接luogu.com.cn/…
来源USACO
算法分类BFS
难易程度容易

Comfortable Cows S

想法

为了消除舒适的牛,需要增加补全的牛,但是会出现补全的牛也变舒适。所以使用队列来维护「需要补完的牛」,使用BFS来补完这些牛,在补全的过程中产生的新的需要补全的牛则加入队列。

代码实现

#include<iostream>
#include<queue>
using namespace std;
const int N = 3010;
int cnt[N][N], ans;
bool vis[N][N];
int x_turn[5]={1, -1, 0, 0, 0};
int y_turn[5]={0, 0, 1, -1, 0};
 
void add(int x, int y)
{
	vis[x][y] = true;
	for(int i=0; i<4; i++) {
		int xx = x + x_turn[i], yy = y + y_turn[i];
		cnt[xx][yy]++;
	}
	return ;
}
 
void BFS(int x, int y)
{
	queue<pair<int, int> >qu;
	for(int i=0; i<=4; i++)
	{
		int xx = x + x_turn[i], yy = y + y_turn[i];
		if(vis[xx][yy] && cnt[xx][yy] == 3)
			qu.push(make_pair(xx, yy));
	}
 
	while(!qu.empty())
	{
		pair<int, int>now = qu.front();
		qu.pop();
		for(int i=0; i<4; i++)
		{
			int xx = now.first + x_turn[i], yy = now.second + y_turn[i];
			if(!vis[xx][yy]) {
				add(xx, yy);
				ans++;
				for(int j=0; j<=4; j++) {
					int xxx = xx + x_turn[j], yyy = yy + y_turn[j];
					if(vis[xxx][yyy] && cnt[xxx][yyy] == 3)
						qu.push(make_pair(xxx, yyy));
				}
			}
		}
	}
 
	return ;
}
 
int main()
{
	int n, x, y;
	cin >> n;
	while(n--)
	{
		cin >> x >> y;
		x += 1000;
		y += 1000; 
		if(vis[x][y]) {
			ans--;
		}
		else {
			add(x, y);
			BFS(x, y);
		}
		cout << ans << '\n';
	}
 
	return 0;	
}
/app/www/public/data/pages/icpc/problems/usaco21feb_comfortable_cows_s.txt · 最后更改: 2023/04/01 12:46 由 温婕莺