C Language algorithm Training 2__ algorithm

Source: Internet
Author: User

/* N*m in The Matrix maze, the gift of different values, starting from (0,0), can only go down or to the right.
After each position, if the gift value of the location is greater than all the gifts you have on hand, you can choose to take the gift (or not) until (n-1, m-1). If the number of gifts on your hand happens to be k, then the present is yours.
How many total plans are in total so that you can finally get a K gift.  Input instance: 2 2 2 1 2 2 1 First act n M K (1<=n,m<=50, 1<=k<=12) after n row K as gift numerical matrix output: 2/#include <iostream> using

namespace Std;

Define the return type typedef enum{RT_OK, Rt_fail}fpi_error;
Define the way to make debuging the code convinient #define DEBUG//#define RELEASE typedef unsigned int uint64;
UInt64 K;


int n_count = 0; #ifdef DEBUG #define MODULE_TEST 1 #else #define MODULE_TEST 0 #endif//define MODULE log output #define FPI_DEBUG (id,fmt , Arg ...) \ if (id) \ {\ printf (fmt,arg); \}
\ else \ {\ \} #ifdef DEBUG int array[4][4]={1,2,3,2,1,4,2,3,5,2,4,2,3,2,5,6};
#else int array[][];


#endif fpi_error startgetgifts (uint64 i,uint64 j,int value_max,uint64 M); /******************************* *func.
Count the methed of getting K gifts. *aut. Wmy *dtae. 2015.10.22 *param.
@i @j The index of 2-d array.
@value_max the most value gift in your hand.
@n_gifts the number of gifts in your hand *return RT_OK. /Fpi_error startgetgifts (UInt64 i,uint64 j,int value_max,uint64 n_gifts,uint64 N,
    	 UInt64 M) {if (j = = M-1 && i = = N-1) {n_gifts++;   	
        printf ("overi=%d.j=%d.n=%d.\n", i,j,n_gifts);
        if (n_gifts = = K) {n_count++;
        }//printf ("%d", RT_OK);
    return RT_OK;
	   	} if (Array[i][j] >= value_max && n_gifts < K) {//get The gifts if (J < M-1)//go right
	   	Startgetgifts (I,J+1,ARRAY[I][J],N_GIFTS+1,N,M);
   if (i < N-1)//go down Startgetgifts (i+1,j,array[i][j],n_gifts+1,n,m); }//don ' t get the gifts if (i!= 0 && J!= 0) {if (J < M-1) Startgetgifts (i,j+1,value_max,n
	   	_GIFTS,N,M);	
    if (i < N-1) startgetgifts (i+1,j,value_max,n_gifts,n,m); int main () {UInt64 n=1,m=1; #iFdef release cout<< ' please input N: ' <<endl;
	cin>>n;
	cout<< "Please input M:" <<endl;
	cin>>m;
cout<< "Please input K:" <<endl;
cin>>k;
	#else n=4;
	m=4;
k=5; #endif if (N < 1 | | N >50 | | M <1 | | m>50 | | k<1 | | K >) {cout<< "The value input is error!"
		 <<endl;
	return rt_fail;
	int length = N*m;
	int i;
int j = 0; #ifdef release for (i = 0;i < n;i++) {cout<< ' please input the ' <<i<< ' line! '
		<<endl;
		for (; J < M; J + +) cin>>array[i][j];
	j=0;
	#endif #ifdef DEBUG i=0;
	    for (; i < length;i++) cout<<array[i]<< "";
cout<<endl;
    #endif fpi_debug (module_test, "function:%s,start!\n", __function__);
    Startgetgifts (0,0,0,0,N,M);
    printf ("%d\n", ret);
    
    Fpi_debug (Module_test, "function:%s,end!ret=%d\n", __function__,ret);
    cout<< "The methed is:" <<n_count<<endl;
return 0;
 }


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.