Turn card Game
Time limit:9000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2624 Accepted Submission (s): 945
Problem Description There is a kind of card game, very interesting, give you n cards, a word lined up, cards have both sides, the beginning of the card may be a chaotic state (some positive, some opposite), now you need to organize these cards. But the trouble is, every time you turn a card (from a positive to a reverse, or a reverse to a positive), his or her two cards (the leftmost and rightmost cards will only affect a nearby one) must also be flipped, now giving you a messy state, asking if you can tidy them up so that each card is facing up, Minimum number of operations required.
Input has multiple case, each case entered a line of 01 symbol string (length not more than 20), 1 for the opposite side upward, 0 for the front face up.
Output for each set of case, if you can turn, outputs the minimum number of times to flip, otherwise output no.
Sample Input
01 011
Sample Output
NO 1
#include <stdio.h>
#include <string.h>
#define INF 1<<30
int a[25],cnt[25];
int Ans,temp,len;
BOOL Myok ()
{
int i;
for (i=0;i<len;i++)
if (A[i]) return false;
return true;
}
void dfs (int idx)
{
int i;
if (Myok ())//To determine whether there is a negative card, if there is the continuation of the flop, no count
{
temp=0;
for (i=0;i<len;i++)
if (cnt[i]==1) temp++;
if (Temp<ans)
ans=temp;
return;
}
if (Idx>=len) return;//The last one must be returned for
(cnt[idx]=0;cnt[idx]<2;)
{a[idx]^=1;//-
flop
if ( idx>0) a[idx-1]^=1;//Turn left
if (Idx<len) a[idx+1]^=1;//turn to the right of the
cnt[idx]++;
DFS (idx+1);
}
}
int main ()
{
int i;
Char ch[25];
while (~SCANF ("%s", ch))
{
len=strlen (CH);
for (i=0;i<len;i++)
a[i]=ch[i]-' 0 ';
Ans=inf;
memset (cnt,0,sizeof (CNT));
DFS (0);
if (ans!=inf) printf ("%d\n", ans);
else printf ("no\n");
}
return 0;
}