首页 > 其他分享 >P7411 [USACO21FEB] Comfortable Cows S (搜索)

P7411 [USACO21FEB] Comfortable Cows S (搜索)

时间:2024-07-08 18:41:44浏览次数:9  
标签:P7411 int chk dfs vis USACO21FEB && Cows mp

P7411 [USACO21FEB] Comfortable Cows S

搜索

容易知道任意时刻的不合法的位置,并且决策只有将空着的位置补起来。

每次加入一个点,判断其自身、上下左右是否变得不合法,往下递归即可。

复杂度分析,每个点只会不合法一次(修改后就变得合法),所以只会遍历一次,复杂度是 \(O(n^2)\)。

#include <bits/stdc++.h>
#define pii std::pair<int, int>
#define mk std::make_pair
#define fi first
#define se second
#define pb push_back

using i64 = long long;
using ull = unsigned long long;
const i64 iinf = 0x3f3f3f3f, linf = 0x3f3f3f3f3f3f3f3f;
const int N = 1510;
int n, ans;
bool vis[N][N], mp[N][N];
bool chk(int x, int y) {
	int cnt = vis[x + 1][y] + vis[x][y + 1];
	if(x - 1 >= 0) cnt += vis[x - 1][y];
	if(y - 1 >= 0) cnt += vis[x][y - 1];
	return cnt == 3;
}
void dfs(int x, int y) {
	vis[x][y] = 1;
	if(chk(x, y)) {
		ans++;
		if(!vis[x - 1][y] && x > 0) dfs(x - 1, y), mp[x - 1][y] = 1;
		if(!vis[x + 1][y]) dfs(x + 1, y), mp[x + 1][y] = 1;
		if(!vis[x][y - 1] && y > 0) dfs(x, y - 1), mp[x][y - 1] = 1;
		if(!vis[x][y + 1]) dfs(x, y + 1), mp[x][y + 1] = 1;
	}
	if(x > 0 && chk(x - 1, y) && vis[x - 1][y]) dfs(x - 1, y);
	if(chk(x + 1, y) && vis[x + 1][y]) dfs(x + 1, y);
	if(y > 0 && chk(x, y - 1) && vis[x][y - 1]) dfs(x, y - 1);
	if(chk(x, y + 1) && vis[x][y + 1]) dfs(x, y + 1);
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
	std::cin >> n;

	for(int i = 1; i <= n; i++) {
		int x, y;
		std::cin >> x >> y;
		x++, y++;
		if(!vis[x][y]) dfs(x, y);
		if(mp[x][y]) ans--, mp[x][y] = 0;
		std::cout << ans << "\n";
	}

	return 0;
}

标签:P7411,int,chk,dfs,vis,USACO21FEB,&&,Cows,mp
From: https://www.cnblogs.com/FireRaku/p/18290532

相关文章

  • 有趣的Python库——CowSay
    有趣的Python库——CowSay安装:pipinstallcowsay命令式使用:cowsay-cpig-t你好,我是一只猪哦!输出:__________|你好,我是一只猪哦!|==========\\\\,.(_|,......
  • P3056 [USACO12NOV] Clumsy Cows S
    [USACO12NOV]ClumsyCowsS题目描述Bessiethecowistryingtotypeabalancedstringofparenthesesintohernewlaptop,butsheissufficientlyclumsy(duetoherlargehooves)thatshekeepsmis-typingcharacters.Pleasehelpherbycomputingthemi......
  • 洛谷 P1204 [USACO1.2] 挤牛奶Milking Cows
    题意:给定n个区间,左端点和右端点表示工作开始时间和结束时间。求最长一直有人在工作的时间和无人工作的时间。思路:想到了并查集,还有差分树状数组,最后选择差分数组。左端点加,右端点减,然后一次遍历即可。总结:习惯性的在右端点+1的位置减少了1,但是不适用于这个题目的逻辑。因为在右......
  • P3052 [USACO12MAR] Cows in a Skyscraper G
    原题链接题解模拟,遍历n个物品,一开始一个箱子不给,遍历到某个物品时,先把所有已经给了的箱子放进去试试,再创一个新箱子放进去试试code#include<bits/stdc++.h>usingnamespacestd;intn,w;intcnt,ans;intchongdie=0;intbox[20],c[20];voidmoni(intnow,intcnt)//now......
  • P2742 [USACO5.1] 圈奶牛Fencing the Cows /【模板】二维凸包
    原题链接题解这么优质的文章我写什么题解好难解释必然性感觉像模拟??code#include<bits/stdc++.h>usingnamespacestd;intq[100005]={0};structnode{doublex,y;}a[100005];doubledis(intb,intc){nodei=a[b],j=a[c];returnsqrt((i.x-j.x)*(i.x-......
  • P3047 [USACO12FEB] Nearby Cows G
    原题链接题解核心技巧:两次搜索第一次搜索:搜索出\(f[now][i]\)以\(now\)为根节点的子树且距离根节点恰好为\(i\)的节点的个数搜索完了之后,把范围\(k\)以内的累加第二次搜索:由于整棵树的根节点的\(f\)等于整棵树里距离不大于\(k\)的节点个数,即已经符合题目要求......
  • P7154 Sleeping Cows 题解
    传送门题意:给定两个数组\(a_i,b_i\),若\(a_i\leb_j\),则他俩可配对。求极大匹配的方案数。(极大不是最大,最大一定是极大)先考虑最大匹配方案数怎么求。把\(a\)和\(b\)从小到大排序。则每个\(a_i\)能匹配的\(b\)都是一段后缀,且随着\(i\)增大,这个后缀越来越小。于是从......
  • P1204 [USACO1.2] 挤牛奶Milking Cows
    原题链接题解细节颇多看代码code#include<bits/stdc++.h>usingnamespacestd;structunit{ints,e;}milk[5005];boolcmp(unita,unitb){returna.s<b.s;}intmain(){intn;cin>>n;for(inti=1;i<=n;i++)cin>>milk[i].s......
  • [USACO07DEC] Sightseeing Cows G
    [USACO07DEC]SightseeingCowsG题目描述FarmerJohnhasdecidedtorewardhiscowsfortheirhardworkbytakingthemonatourofthebigcity!Thecowsmustdecidehowbesttospendtheirfreetime.Fortunately,theyhaveadetailedcitymapshowingthe$L......
  • P9194 [USACO23OPEN] Triples of Cows P 题解
    Description给定一棵初始有\(n\)个点的树。在第\(i\)天,这棵树的第\(i\)个点会被删除,所有与点\(i\)直接相连的点之间都会两两连上一条边。你需要在每次删点发生前,求出满足\((a,b)\)之间有边,\((b,c)\)之间有边且\(a\not=c\)的有序三元组\((a,b,c)\)对数。\(n\leq2......