Deep-priority DFS ----- Example 3 Example 4

Source: Internet
Author: User

 

Example 3: Combination Problem

All the combinations of N numbers in the output M number. For example, all combinations of M = 5 and n = 3 are:
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

 

# Include <iostream> using namespace STD; int m, n, a [10]; // store each number of void comb (int K) {for (INT I = A [k-1] + 1; I <= m-N + k; I ++) {A [k] = I; If (k> N) {for (INT I = 1; I <= N; I ++) printf ("% 5d", a [I]); printf ("\ n "); return;} comb (k + 1) ;}} int main () {scanf ("% d", & M, & N); comb (1 ); // starting from 1st}
View code

 

# Include <iostream> using namespace STD;

Int m, n, a [10]; // store each number

Void comb (int K)

{

For (INT I = A [k-1] + 1; I <= m-N + k; I ++)

{A [k] = I;

If (k> N)

{For (INT I = 1; I <= N; I ++) printf ("% 5d", a [I]);

Printf ("\ n"); return;

}

 

Comb (k + 1 );

}

}

 

Int main ()

{

Scanf ("% d", & M, & N );

Comb (1); // starts from 1st

}

 

 

Example 4: 0-1 solve the backtracing Problem of a backpack

 

There are n items with different values and different weights,

Find a solution to select a part of the N items,

So that the total weight of the selected item does not exceed the specified weight limit, but the total value of the selected item is the largest.

For example, if the weight limit is set to 7 and there are four items, the weight and value of these items are shown in the table below. How can I sum up the maximum value of an item?

 

 

 

// Tracing algorithm for the 0-1 knapsack problem: # include <stdio. h> # define M 10int W [m] = {5, 3,}, V [m] = {4, 4, 3, 1}, limit_w = 7, n = 4; int Tw = 0, maxv = 0, TV = 0, B [m] = {0}; void find (int I) {if (I = N) return; // All items have been judged if (TW + W [I] <= limit_w) // select the I-th item if (I <= N-1) // conditions for entering part I + 1: {Tw = TW + W [I]; TV = TV + V [I]; B [I] = 1; // select the I-th If (maxv <TV) maxv = TV; find (I + 1 ); // enter the I + 1 Tw = Tw-W [I]; TV = TV-V [I]; B [I] = 0; // No I parts selected} if (I <= N-1) Find (I + 1); // No I items selected} void main () {find (0); // select printf ("maxv = % d \ n", maxv) from 0th items );}
View code

 

 

// Tracing algorithm for the 0-1 knapsack problem:
# Include <stdio. h>
# Define M 10
Int W [m] = {,}, V [m] = {,}, limit_w = 7, n = 4;


Int Tw = 0, maxv = 0, TV = 0, B [m] = {0 };


Void find (int I)
{If (I = N) return; // All items have been judged
If (TW + W [I] <= limit_w) // select the I-th item
If (I <= N-1) // conditions for entering part I + 1


{Tw = TW + W [I]; TV = TV + V [I]; B [I] = 1; // select the I part
If (maxv <TV) maxv = TV;
Find (I + 1); // enter the I + 1 part
Tw = Tw-W [I]; TV = TV-V [I]; B [I] = 0; // No I is selected
}


If (I <= N-1) Find (I + 1); // do not select item I
}


Void main ()
{Find (0); // select from 0th items


Printf ("maxv = % d \ n", maxv );}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.