Bessie’s Secret Pasture S
正确思路
排列出所有可能情况
错误原因
没理解题目
正确代码
#include<bits/stdc++.h>
using namespace std;
int n, s;
void dfs(int q,int v){
if(v>4){
if(q==0)s++;
return;
}
for(int i = 0;i*i <= q;i++)dfs(q-i*i,v+1);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
dfs(n,1);
cout << s;
return 0;
}Cows in a Skyscraper G
正确思路
从小到大排列所有可能的次数,判断成不成立,如果成利就输出。
错误原因
没想通思路
正确代码
#include<bits/stdc++.h>
using namespace std;
int n, W, w[30], c[30];
void dfs(int v,int k,int q){
if(v>n){
cout << k;
exit(0);
}
for(int i = 1;i <= k&&i<=q+1;i++){
if(c[v]+w[i]<=W){
w[i]+=c[v];
dfs(v+1,k,max(q,i));
w[i]-=c[v];
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> W;
for(int i = 1;i <= n;i++)cin >> c[i];
for(int i = 1;i <= n;i++)dfs(1,i,0);
return 0;
}