ACM Learning process-hdu1584 Spider Card (Dynamic planning && State compression)

Source: Internet
Author: User

Description

Spider card is the Windows XP operating system comes with a card game, the rules of the game is this: can only be dragged to the card than her freshman card (a minimum, K max), if you drag the cards in order to arrange the cards, then these cards are also moving together, The purpose of the game is to all the cards in the same suit from small to large row, for the sake of simplicity, our game only the same suit of 10 cards, from a to 10, and randomly spread on a line, numbering from 1 to 10, the number I on the card on the first J plate, moving distance of ABS (I-J), Now all you have to do is find the minimum moving distance to complete the game.

Input

The first input data is T, which represents the number of groups of data.
Each group of data has a row, 10 input data, the range of data is [1,10], respectively, a to 10, we guarantee that each group of data is legal.

Output

Corresponds to the minimum moving distance for each set of data output.

Sample Input

11 2 3 4 5 6 7 8 9 10

Sample Output

9

The title requirement is to say 10 cards merged into a pile, then the small cards placed in the size of +1 on the big card.

The key is only 10 cards, then each card in its own position, or in other locations, then set a one for 1 cards in their own position, otherwise 0, so that the state can be compressed.

The bits of p[state],state is the state of each card.

The second is to consider if 2 is not in their own position, 3 is not in their own position, and 4 in their own position, then 234 must be in the position of 4, because the most is the first 3 to 4, then 2 to 3, or 2 to 3, then 2, 31 to 4 (1 regardless of the situation)

In addition, a card with a value of K can be placed in the first non-0 position in the k+1,k+2,k+3.

This status update is

P[State^ (1<<i)] = mymin(P[State^ (1<<i)], p [State]+ABS(i-j));(where I is a non-0 bit of state and J is the first non-0 bit above i)

< Span class= "Sh-symbol" > mymin is a custom minimum value, the first parameter is 1 of the case of a special sentence, there are many ways to deal with.

And then this equation I'm not going to do with for. With a SPFA thought. The previous status update will cause later status updates.

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<Set>#include<map>#include<queue>#include<string>#include<algorithm>#defineLL Long Longusing namespacestd;inta[ the], p[4100], from;inthash[ the];BOOLvis[4100];intMymin (intXinty) {    if(x = =-1)        returny; returnmin (x, y);}voidinput () {memset (P,-1,sizeof(p)); memset (Vis,false,sizeof(VIS));  from=0;  for(inti =1; I <=Ten; ++i) {scanf ("%d", &A[i]); Hash[a[i]]=i;  from|= (1<<i); } p[ from] =0;}voidSPFA () {intK, V, pre; Queue<int>Q; Q.push ( from); vis[ from] =true;  while(!Q.empty ()) {k=Q.front ();        Q.pop (); VIS[K]=false;  for(inti =1; I <=Ten; ++i) {if(K & (1<<i)) && a[i]! =Ten) {v= a[i]+1;  while(! (k& (1<<Hash[v]))) V++; Pre= p[k^ (1<<i)]; P[k^(1<<i)] = Mymin (p[k^ (1<<i)], p[k]+abs (i-Hash[v])); if(p[k^ (1&LT;&LT;I)]! =Pre &&!vis[k^ (1<<i)]) {Q.push (k^(1<<i)); Vis[k^(1<<i)] =true; }            }        }    }}voidWork () {SPFA (); printf ("%d\n", p[1<Ten]]);}intMain () {//freopen ("test.in", "R", stdin);    intT; scanf ("%d", &T);  for(intTimes =0; Times < T; ++Times )        {input ();    Work (); }    return 0;}

ACM Learning process-hdu1584 Spider Card (Dynamic planning && State compression)

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.