UVa 10651-pebble Solitaire

Source: Internet
Author: User

Title: There is a game like checkers, a total of 12 positions ' o ' represent the pieces '-' stands for the vacancy,

' oo-' can be transformed into '--o ', '-oo ' can be transformed into ' o--', giving you the initial state, and finally, at least a few pieces left.

Analysis: DP, memory search, bit operation. Use the search to be in the neighboring state between the DP.

The optimal solution for each state is that he can transform the optimal solution in all States.

Because, altogether has 2^12 = 4,096 states, each finds the solution namely the storage, does not repeat the computation, therefore the time aspect is not a problem.

A 12-bit integer is used to denote a state, ' o ' is denoted by 1, '-' is represented by 0, then '---oo-------' is 24 (2 binary 000000011000)

(This is from left to right for low to high, upside down)

Through observation can be found that ' oo-' (011) into '--o ' (), '-oo ' (110) converted to ' o--' (001) are corresponding to take the reverse;

The direct use of 7 (2 binary 111) for the XOR, and the corresponding amount can jump the status of 3 (2 011) and 6 (2 binary 110);

Description: Detailed reference code.

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <cstdio> #include <cmath>using namespace Std;char buf[13];int  f[4100];int dfs (int s {if (F[s] <) return F[s];int Min = 12;for (int i = 0; I < + + i) if (((s>>i) &7) = = 3 | | ((s>>i) &7) = = 6) min = min (min, Dfs (s^ (7<<i))), if (min = =) {for (int i = 0; i <; + + i) min-=! ( s& (1<<i));} return f[s] = Min;} int main () {for (int i = 0; i < 4100; + + i) f[i] = 13;int n,v;while (~scanf ("%d", &n)) for (int i = 1; I <= N; + + i) {scanf ("%s", buf), V = 0;for (int j = 0; J <; + + j) {v <<= 1;if (buf[j] = = ' O ') v + = 1;} printf ("%d\n", DF S (v));}    return 0;}


UVa 10651-pebble Solitaire

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.