[Ultraviolet A] 10317-equating equations (DFS + pruning)

Source: Internet
Author: User

I am so depressed that I will use the DP Method for a common search question, and the results will always be done by TLE and Wa.

If the sum of all numbers is an odd number, there is certainly no positive solution.

14133454 10317 Equating Equations Accepted C ++ 0.102 2014-09-02 09:01:23

# Include <iostream> # include <cstdlib> # include <cstdio> # include <string> # include <cstring> # include <cmath> # include <vector> # include <queue> # include <set> # include <map> # include <stack> # include <algorithm> using namespace STD; const double EPS = 1e-10; # define maxd 110 # DEFINE _ pI ACOs (-1.0) int array [maxd]; int _ ansx [maxd], _ ansy [maxd]; int _ x, _ y; int size, add; int aim; int vis [maxd]; int _ operator [maxd]; Bo Ol DFS (INT ans, int cur, int POS) {If (cur = add) {If (ANS = aim) return true; else return false ;} for (INT I = Pos; I <size; I ++) if (! Vis [I]) {vis [I] = 1; if (ANS + array [I] <= aim & DFS (ANS + array [I], cur + 1, I + 1) return true; vis [I] = 0;} return false;} void Init () {for (INT I = 0; I <size; I ++) {If (vis [I]) {_ ansx [_ x ++] = array [I];} else {_ ansy [_ y ++] = array [I] ;}} int main () {char character [maxd]; char m; while (scanf ("% d", & array [0])! = EOF) {_ operator [0] = 1; memset (VIS, 0, sizeof (VIS); size = 1; _ x = 0; _ y = 0; add = 1; int p = 0; int POs, sum = array [0]; while (M = getchar () {If (M = '\ n ') break; scanf ("% s", expiration); If (else [0] = '+') {If (! P) {Add ++; _ operator [size] = 1 ;}else {_ operator [size] =-1 ;}} if (else [0] = '-') {If (! P) {_ operator [size] =-1 ;}else {_ operator [size] = 1; add ++ ;}} if (Values [0] = ') {p = 1; Pos = size; _ operator [size] =-1;} scanf ("% d ", & array [size ++]); sum + = array [size-1];} p = 0; Aim = sum/2; if (sum % 2 = 0 & DFS (0, 0) {Init (); For (INT I = 0, P1 = 0, Q1 = 0; I <size; I ++) {if (I = 0) printf ("% d", _ ansx [P1 ++]); else if (I = POS) {p = 1; printf ("= % d", _ ansy [Q1 ++]);} else if (_ OP Erator [I] = 1) {If (! P) printf ("+ % d", _ ansx [P1 ++]); else printf ("-% d", _ ansx [P1 ++]);} else if (_ operator [I] =-1) {If (! P) printf ("-% d", _ ansy [Q1 ++]); else printf ("+ % d", _ ansy [Q1 ++]);} printf ("\ n");} else printf ("no solution \ n");} return 0 ;}

[Ultraviolet A] 10317-equating equations (DFS + pruning)

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.