[Ultraviolet A] 10651-Pebble solitaire (direct recursion or memory)

Source: Internet
Author: User

I don't know what the data for this question is. I handed it in two methods. The first direct recursion, the second memory pruning, and the time turned out to be the same !?

Directly depressed, simple binary represents the State and binary operations.

14145176 10651 Pebble Solitaire Accepted C ++ 0.009 2014-09-04 09:18:21

# Include <cstdio> # include <algorithm> # include <string> # include <cstring> # include <map> # include <iostream> using namespace STD; # define maxd 20 const int max_size = 1 <13; const int INF = 20; int DP [max_size]; int dp (INT state) {If (DP [State]! =-1) return DP [State]; DP [State] = 0; For (INT I = 0; I <12; I ++) if (State & (1 <I) DP [State] ++; For (INT I = 0; I <12; I ++) {if (I> 1) {If (State & (1 <I) & (State & (1 <(I-1 )))&&! (State & (1 <(I-2) {int u = State; // Changes the I-2 digit from 0 to 1, change the I-1 and I bit from 1 to 0 U = u &(~ (1 <I); U = u &(~ (1 <(I-1); U = u | (1 <(I-2); DP [State] = min (DP [State], DP (u) ;}}if (I <10) {If (State & (1 <I )) & (State & (1 <(I + 1 )))&&! (State & (1 <(I + 2) {int u = State; // Changes the I-2 digit from 0 to 1, change the I-1 and I bit from 1 to 0 U = u &(~ (1 <I); U = u &(~ (1 <(I + 1); U = u | (1 <(I + 2); DP [State] = min (DP [State], DP (u) ;}}return DP [State];} int main () {int t; int N; memset (DP,-1, sizeof (DP )); scanf ("% d", & T); While (t --) {char STR [maxd]; scanf ("% s", STR); n = 0; for (INT I = 0; I <strlen (STR); I ++) {If (STR [I] = 'O ') N = n | (1 <I);} int ans = dp (n); printf ("% d \ n", ANS);} return 0 ;}

[Ultraviolet A] 10651-Pebble solitaire (direct recursion or memory)

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.