UVA-1374 Power calculus iterative Deep Search

Source: Internet
Author: User

Question: Ask at least how many steps to change from 1 to N. The transformation rules are as follows
1. Intermediate results in the process of change can be used arbitrarily
2. Only two numbers can be selected for addition and subtraction at a time

How to solve the problem: first count the minimum number of steps, and then if the current number of steps can not reach the results, the current number of steps plus 1, continue to search down

#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;#define MAXN 1010intNUM[MAXN], CNT, N, min_depth;BOOLDfsintCurintDepth) {if(Depth >= min_depth) {if(cur = = N)return true;return false; }intMAX =0; for(inti =0; I < CNT; i++) max = max (max,num[i]);if((cur + MAX) << (Min_depth-depth-1)) < N)return false; for(inti = cnt-1; I >=0; i--) {num[cnt++] = cur + num[i];if(DFS (cur + num[i], depth+1))return true;        cnt--; num[cnt++] = Cur-num[i];if(Dfs (cur-num[i],depth+1))return true;    cnt--; }return false;}intMain () { while(scanf("%d", &n) = =1&& N) {min_depth =0;intt =1; while(T < N)            {T + = t;        min_depth++; } while(1) {num[0] = CNT =1;if(Dfs (1,0)) Break;        min_depth++; }printf("%d\n", min_depth); }return 0;}

UVA-1374 Power calculus iterative Deep Search

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.