dfs求调
查看原帖
dfs求调
1528563
linyutong123123楼主2025/1/18 09:42
#include<bits/stdc++.h>
using namespace std;
int n, m;
int a[105], vis[105];
int cnt = 0;
void dfs(int x, int s) {
    if (s == 0) {
        cnt++;
        return ;
    }
    for (int i = x; i < n; i++) {
        if (!vis[i]) {
            vis[i] = 1;
            dfs(i, s - a[i]);
            vis[i] = 0;
        }
    }
}
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> a[i];
    dfs(0, m);
    cout << cnt << endl;
    return 0;
}
2025/1/18 09:42
加载中...