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