Poj -- 1950 dessert (DFS)

Source: Internet
Author: User

 

/* I feel that my practice is very troublesome. One can only store +-symbol arrays, one can store all operators in the queue, and the other one.
Queue storage operations (such as 1011, 10.11 )). And then brute force search... */

/* My code: 297 Ms */

# Include <iostream>
# Include <cstdio>
# Include <cstring>

Using namespace STD;

Const int n = 50;

Char opr2 [N];
Char OPR [N];
Int OPN [N];
Int CNT, N;

Void DFS (int p, int P2, int M, int sum, int t ){
Int I, j, tsum;
If (sum = 0 & t = N ){
If (CNT <20 ){
For (I = 0; I <p; I ++ ){
Printf ("% d % C", I + 1, opr [I]);
}
Printf ("% d \ n", t );
}
CNT ++;
Return;
}
If (T> = N) return;

T ++;

OPR [p] = opr2 [P2] = '+ ';
OPN [m] = T;
Tsum = sum + T;
DFS (p + 1, p2 + 1, m + 1, tsum, t );

OPR [p] = opr2 [P2] = '-';
OPN [m] = T;
Tsum = sum-T;
DFS (p + 1, p2 + 1, m + 1, tsum, t );

OPR [p] = '.';
If (T> = 10) fig [M-1] = fig [M-1] * 100 + T;
Else OPN [M-1] = OPN [M-1] * 10 + T;
J = 0; tsum = fig [0];
For (I = 1; I <= m-1; I ++ ){
If (opr2 [J] = '+ ')
Tsum + = fig [I];
Else
Tsum-= fig [I];
J ++;
}
DFS (p + 1, P2, M, tsum, t );
}

Int main (){
// Freopen ("data. In", "r", stdin );

While (~ Scanf ("% d", & N )){
Memset (OPR, 0, sizeof (OPR ));
Memset (opr2, 0, sizeof (opr2 ));
Memset (FIG, 0, sizeof (FIG ));

CNT = 0; fig [0] = 1;
DFS (0, 0, 1, 1, 1 );
Printf ("% d \ n", CNT );
}
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.