Topic Link: Click to open the link
Test instructions: A given n ticks. Here are the n ticks.
To construct a ruler, the n scales above can be directly calculated.
And to meet the ruler on the minimum number of tick marks, the least the case of the shortest ruler.
The first scale is always 0
The problem guarantee always exists the solution of <7 tick line.
Ideas:
BFS, each time the enumeration of new plus a scale, which can be directly calculated, in binary notation, and then violent search
Pruning:
1, if the addition of a new scale can not be more than the measurement of a given n ticks then the added scale is invalid (that is, before and after joining the same state)
2, ruler length if greater than the maximum scale is invalid
Import Java.io.printwriter;import java.util.arraylist;import Java.util.arrays;import Java.util.Collections;import Java.util.comparator;import Java.util.iterator;import Java.util.linkedlist;import Java.util.PriorityQueue;import Java.util.scanner;import Java.util.treeset;import Java.util.queue;public class Main {static int N = 55;int N, max;int[] A = new int[n];int[] id = new Int[1000005];class node{treeset<integer> g;int state; Node () {state = 0; G = new TreeSet ();} Public Node Clone () {node hehe = new Node (), hehe.state = this.state;iterator<integer> it = this. G.iterator (); while (It.hasnext ()) hehe. G.add (It.next ()); return hehe;} void put () {System.out.print (state+ ":"); Iterator it = G.iterator (); while (It.hasnext ()) System.out.print (It.next () + "" ); System.out.println ();}} Node p; treeset<integer> ans = new TreeSet (); queue<node> Q = new linkedlist<node> (); void BFS () {q.clear ();p = new Node ();p. G.add (0); Q.add (P); while (Q.size () >0) {p = Q.poll (); if (p.state = = (1<<n)-1) {if (Ans.size () < P.g.size ()) continue;if (ans.size () = = P.g.size () && ans.last () <=p.g.last ()) Continue ;iterator<integer> it = P.g.iterator (); Ans.clear (); while (It.hasnext ()) Ans.add (It.next ()); continue;} if (p.g.size () = = 7) continue;iterator<integer> it = P.g.iterator (); while (It.hasnext ()) {int num = It.next (); Node next;int i;for (i=0; i<n; i++) {if (p.state& (1<<i)) >0) continue; The scale has been selected int v = num+a[i];if (P.g.contains (v)) continue;//Scale is already able to indicate if (V>max) continue;//exceeds the maximum scale and does not require next=p.clone (); next. G.add (v);iterator<integer> it2 = P.g.iterator (); while (It2.hasnext ()) {int x=abs (V-it2.next ()); if (id[x]!=-1) Next.state|= (1<<id[x]);} if (next.state!=p.state) Q.add (Next.clone ());}}} void Init () {ans.clear (); for (int i = 1; I <= n; i++) Ans.add (Cin.nextint ()), max = Ans.last (); for (int i = 0; I <= Max; i++) Id[i] = -1;iterator<integer> it = Ans.iterator (); n = 0;while (It.hasnext ()) {a[n++] = It.next (); id[a[n-1]] = n-1; }}void put () {Out.println (Ans.size ());iterator<integer> it = Ans.iterator (); while (It.hasnext ()) {Out.print (It.next () + "");} Out.println ();} void work () {int Cas = 1;while (true) {n = cin.nextint (), if (n = = 0) break;out.println ("Case" + (cas++) + ":"); init (); BFS ();p ut ();}} Main () {cin = new Scanner (system.in); out = new PrintWriter (System.out);} public static void Main (string[] args) {main E = new main (); E.work (); Out.close ();} Public Scanner cin;public static PrintWriter out;int upper_bound (int[] A, int l, int r, int val) {//upper_bound (a+l,a+r,val )-a;int pos = r;r--; while (L <= r) {int mid = (l+r) >>1;if (a[mid]<=val) {L = mid+1;} else {pos = Mid;r = Mid-1;}} return POS;} /*class Queue {int[] queue = new Int[n+10];int front, rear;//front <= rearqueue () {//queue = new int[x];} void Clear () {front = rear = 1;} Boolean empty () {return front = = rear;} int size () {return rear-front;} int Front () {return queue[front];} int rear () {return queue[rear-1];} void push_rear (int x) {queue[rear++] = x;} void Pop_front () {front++;} voidPop_rear () {rear--;}} /**/int max (int x, int y) {return x > y x:y;} int min (int x, int y) {return x < y x:y;} Double Max (double x, double y) {return x > y x:y;} Double min (double x, double y) {return x < y x:y;} static double EPS = 1e-8;int abs (int x) {return x > 0 ×:-X;} Double abs (double x) {return x > 0 ×:-X;} Boolean zero (double x) {return abs (x) < EPS;}}
UVA1377 Ruler bfs+-shaped pressure search