UVa 10651 Pebble Solitaire:dp&bitset

Source: Internet
Author: User
Tags bitset int size min reset time limit

10651-pebble Solitaire

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1592

into the binary for state transfer, see Code.

Complete code:

/*0.016s*/#include <bits/stdc++.h> using namespace std;  
const int Size = 12;  
Char Str[size];  
int dp[4100];  
bitset<size> tmp;  
    int f (unsigned long N) {if (n = = 0 | | dp[n]) return dp[n];  
    Bitset<size> b (n); int minn = B.count ();///in the case of not card time, I use Bitset is because this function is very convenient, although I write a few lines///with Bitset can also avoid the trouble caused by operator precedence (!). ~ higher than << >> above = = above & above ^ above | Above && above | |  
            for (int i = 0; i < Size; ++i) {if (!b.test (i))///equivalent to if ((n>>i&1) ==0) { if (i < Size-2 && B.test (i + 1) && b.test (i + 2)) {TMP =  
                Tmp.set (i), tmp.reset (i + 1), Tmp.reset (i + 2),///equivalent to n|1u<<i,n&~ (1u<< (i+1)), n&~ (1u<< (i+2))  
            Minn = MIN (Minn, F (Tmp.to_ulong ()));  
      } if (i > 1 && b.test (i-1) && b.test (i-2)) {          TMP = b;  
                Tmp.set (i), Tmp.reset (i-1), Tmp.reset (i-2);  
            Minn = MIN (Minn, F (Tmp.to_ulong ()));  
}} return Dp[n] = Minn;  
    int main () {int T, I;  
    scanf ("%d", &t);  
        while (t--) {GetChar (); for (i = 0; i < Size; ++i) str[i] = (GetChar () = = '-'?  
        ' 0 ': ' 1 ');  
    printf ("%d\n", F (Strtoul (str, NULL, 2));  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.