HDU2209 card Game "Tips"

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=2209


Main topic:

There are n cards, there is a face up, there is the opposite side up, now need to put all the cards face up, it is known that each turn a card,

a card on the left and right side of the card also flips . . Now give you a line containing only the characters ' 0 ' and ' 1 ' of the 01 string, ' 1 ' for the solitaire

reverse, ' 0 ' represents the front of the card. Now you need to change the string to all "0000 ... 00 "String, one operation can only change one

The character itself and one of its left and right sides, Q: How many operations must be done before the string becomes "0000 ... 00 ".

If you can't turn it into "0000 ... 00 ", the output is" NO ".


Ideas:

You can directly think of a wide search to find the minimum number of steps. But after observation, it can be simulated from the front to the back to get results.

Because the card is turned from left to right, for the current card, as long as the current solitaire Solitaire on the left to be ' 1 ', you don't have to flip the card if the left paper

card is ' 0 ', you must flip the card. For the first card, there is no left-hand solitaire, so the judgment starts with the 2nd card,

and The first card has two cases of flipping and non-turning, and the two cases are divided into different don't think about it. The last card is pushed to the last, at this time, in addition to the most

most after a card, the rest of the cards have become "0000 ... 0 ", just to determine whether the last card is ' 0 ', you can know is not

is a can turn into "0000 ... 00 "state. If neither of these can be turned into this state, the result is output "NO", otherwise the output can be turned out

sentiment the smallest case in the condition.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    Std;char s[22],a[22],b[22];int min,numa,numb,num,len,flag;void Solvea () {Num = 0;            for (int i = 1; i < Len; ++i) {if (a[i-1] = = ' 1 ') {a[i-1] = ' 0 ';            if (a[i] = = ' 0 ') a[i] = ' 1 ';            else a[i] = ' 0 ';                if (i! = len-1) {if (a[i+1] = = ' 0 ') a[i+1] = ' 1 ';            else a[i+1] = ' 0 ';        } num++;        }} if (a[len-1] = = ' 0 ') {Numa = Num;    flag = 1;    }}void Solveb () {Num = 0;    if (b[0] = = ' 1 ') b[0] = ' 0 ';    else b[0] = ' 1 ';    if (b[1] = = ' 1 ') b[1] = ' 0 ';    else b[1] = ' 1 ';    num++;            for (int i = 1; i < Len; ++i) {if (b[i-1] = = ' 1 ') {b[i-1] = ' 0 ';            if (b[i] = = ' 0 ') b[i] = ' 1 '; Else                B[i] = ' 0 ';                if (i! = len-1) {if (b[i+1] = = ' 0 ') b[i+1] = ' 1 ';            else b[i+1] = ' 0 ';        } num++;        }} if (b[len-1] = = ' 0 ') {Numb = Num;    flag = 1;        }}int Main () {while (CIN >> s) {len = strlen (s);        memset (A,0,sizeof (a));        Memset (b,0,sizeof (b));        for (int i = 0; i < len; ++i) a[i] = b[i] = S[i];        Flag = 0;        Numa = Numb = 0xffffff0;        Solvea ();        SOLVEB ();            if (len = = 1) {if (s[0] = = ' 1 ') cout << ' 1 ' << Endl;            else cout << ' 0 ' << Endl;        Continue        } if (flag = = 0) cout << "NO" << Endl;            else {if (Numa < Numb) cout << numa << Endl; else cout << Numb << Endl; }} return 0;}


HDU2209 card Game "Tips"

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.