题解:P1694 [USACO19FEB] The Great Revegetation B

这题还是很简单的。

提供两种写法。

枚举

思路:通过二重循环暴力枚举每一格种的种类,在二重循环内判断这个方案是否可行。

实现:枚举第 i ( 1 ≤ i ≤ n ) i(1\le i\le n) i(1in) 格,种第 j ( 1 ≤ j ≤ 4 ) j(1\le j\le 4) j(1j4) 种草,枚举第 k ( 1 ≤ k ≤ m ) k(1\le k\le m) k(1km) 个要求,判断是否可行,可行就输出 j j j

递归

思路:使用 c c c 数组记录每一个要求,然后从第 1 1 1 格开始爆搜,对于每一格只放满足要求的草种。

实现:见代码。

#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
typedef pair<int, int> pr;
#define up(i, l, r) for(int i = (l); i <= (r); i++)
#define down(i, r, l) for(int i = (r); i >= (l); i--)
const int mod = 1000000007;
const int base = 2333;
const double eps = 1e-6;

inline int read() {
	int x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
	while(ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
	return x * f;
}

int n, m, k, Q, T, _, ans = 0;
bool flag = 0;
int c[107][107], cnt[107];
int t[107];
bool vis[7];

inline void dfs(int x) {
	if(x > n) {
		for(int i = 1; i <= n; i++) cout << t[i];
		exit(0);
	}
	for(int i = 1; i <= 4; i++) vis[i] = 0;
	for(int i = 1; i <= cnt[x]; i++) vis[t[c[x][i]]] = 1;
	for(int i = 1; i <= 4; i++) {
		if(vis[i]) continue;
		t[x] = i; dfs(x + 1); t[x] = 0;
	}
}

signed main() {
	n = read(), m = read();
	for(int i = 1; i <= m; i++) {
		int x = read(), y = read();
		if(x < y) swap(x, y);
		c[x][++cnt[x]] = y;
	}
	dfs(1);
	return 0;
}
  • 25
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值