ZCMU-1201 Flip Card Game

topic

1201: Card Flip Game
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 168 Solved: 51
[Submit][Status][Web Board]
Description
There is a kind of card game, which is very interesting. We will give you N cards in a row. , The cards have front and back sides. The starting cards may be in a chaotic state (some face up, some face upside down). Now you need to sort these cards. But the trouble is that whenever you turn a card (from front to back, or from back to front), he has two cards on the left and right (the leftmost and rightmost cards will only affect the nearby one). You must follow the flip, and now give you a chaotic state, and ask if you can arrange them so that each card is face up. If so, how many operations are required at least.

Input
has multiple cases. For each case, enter a line of 01 symbol string (the length does not exceed 1000), 1 means the reverse side is facing up, and 0 means the front face is facing up.


For each case of Output , if it can be flipped, output the minimum number of flips, otherwise output NO.

Sample Input

01
011
1111

Sample Output
NO

1
2

HINT
is unable to complete the first set of test data no matter what the operation is.

For the second set of test data, you only need to reverse the rightmost card once.

For the third set of test data, the first and last cards need to be flipped

***Please use scanf("%s",s) to input, you may encounter trouble when using gets()


idea

For this problem, first simulate several findings. If you want to achieve all 0s, then make sure that the previous ones are all 0, but the previous one is 1, so that when i is flipped, i-1 i i+1 (if there is any) ) Can all become 0. In other words, judging whether it can be flipped depends on whether the previous one is 1.
Then it should be noted that whether the first flop needs to be discussed in different categories

AC code

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pre(i,a,b) for(int i=a;i>=b;--i)
#define m(x) memset(x,0,sizeof x)
const double PI = acos(-1);
const int maxn = 110;
const int mod = 1000000007;
typedef long long ll;
char s[1010];
int turn[1010],Nturn[1010];
int cnt1,cnt2;
//第一张就动
void first_move(int n){
    
    
    cnt1 = 1;
    //动就动1,2张
    turn[0] = -turn[0];
    turn[1] = -turn[1];
    for(int i=1;i<n;++i)
    {
    
    
        if(turn[i-1]==1){
    
    
            cnt1 ++;
            turn[i-1] = -turn[i-1];
            turn[i] = -turn[i];
            if(i!=n-1)turn[i+1] = - turn[i+1];
        }
    }
    if(turn[n-1]==1)cnt1 = -1;
}

//第一张不动
void first_notmove(int n){
    
    
    
    for(int i=1;i<n;++i)
    {
    
    
        if(Nturn[i-1]==1){
    
    
            cnt2 ++;
            Nturn[i-1] = -Nturn[i-1];
            Nturn[i] = -Nturn[i];
            if(i!=n-1)Nturn[i+1] = -Nturn[i+1];
        }
    }
    if(Nturn[n-1]==1)cnt2 = -1;
}

int main()
{
    
    
    while(~scanf("%s",s))
    {
    
    
        cnt1 = cnt2 = 0;
        int len = (int)strlen(s);
        //特别判断len=1
        if(len==1){
    
    
            if(s[0]=='1')printf("1\n");
            else printf("0\n");
        }else
        {
    
    
            for(int i=0;i<len;++i){
    
    
                if(s[i]=='0')turn[i] = -1;
                else turn[i] = 1;
                Nturn[i] = turn[i];
            }
            first_move(len);
            first_notmove(len);
           // cout << cnt1 << " " << cnt2 << endl;
            if(cnt1==-1&&cnt2!=-1){
    
    
                printf("%d\n",cnt2);
                continue;
            }
            if(cnt2==-1&&cnt1!=-1){
    
    
                printf("%d\n",cnt1);
                continue;
            }
            if(cnt1==-1&&cnt2==-1){
    
    
                printf("NO\n");
                continue;
            }
            printf("%d\n",min(cnt1,cnt2));
        }
        memset(turn, 0, sizeof turn);
        memset(Nturn, 0, sizeof Nturn);
        memset(s, 0, sizeof s);
    }
    return 0;
}




to sum up

Take time to write more code, hey...

Guess you like

Origin blog.csdn.net/DAVID3A/article/details/115220958