这题还是很简单的。
提供两种写法。
枚举
思路:通过二重循环暴力枚举每一格种的种类,在二重循环内判断这个方案是否可行。
实现:枚举第 i ( 1 ≤ i ≤ n ) i(1\le i\le n) i(1≤i≤n) 格,种第 j ( 1 ≤ j ≤ 4 ) j(1\le j\le 4) j(1≤j≤4) 种草,枚举第 k ( 1 ≤ k ≤ m ) k(1\le k\le m) k(1≤k≤m) 个要求,判断是否可行,可行就输出 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;
}