Catch fish in C Language
Fish fishing problem: 20 buckets with 10 fish in each bucket. The number of fish captured in each bucket is random. Each bucket can only be caught once, ask how many sort of 180 entries are caught.
Analysis: Let's look at the dual problem of this problem. After capturing 180 fish, 20 fish are left in 20 buckets, different capture methods correspond to the distribution of these fish in 20 buckets, as a result, the problem is transformed into how many different classification methods are used to divide 20 fish into 20 buckets (this problem is also equivalent to how many different methods are used to divide 180 fish into 20 buckets).
Dp [I] [j]: The method for placing j fish in the first I bucket is to place j-k in the first I-1 bucket (0 <= k <= 10) (Because bucket I can only hold 0 to 10 fish) the total number of fish methods. We can obtain the state equation: f (I, j) = sum {f (I-1, j-k), 0 <= k <= 10}
/* Fish fishing: put 20 fish in 20 buckets, and put up to 10 fish in each bucket. Find all the arrangement methods/* bottom-up DP f (I, j) = sum {f (I-1, j-k), 0 <= k <= 10}/* This method tests 20 barrels of 180 fish, comparison with recursive speed */void CatchFish () {int dp [21] [200]; // number of methods for placing j fish in the first I bucket int bucketN = 20; int fishN = 20; memset (dp, 0, sizeof (dp); for (int I = 0; I <= 10; ++ I) // initialize valid status {dp [1] [I] = 1 ;}for (int I = 2; I <= bucketN; ++ I) // start from the second bucket {for (int j = 0; j <= fishN; ++ j) {for (int k = 0; k <= 10 & j-k> = 0; ++ k) {dp [I] [j] + = dp [I-1] [j-k] ;}} printf (% d, dp [bucketN] [fishN]);}