Given a string colors, where each character is either white or black, Wendy and Bob play a game to manipulate this string as follows:
• They perform moves alternatively in turns and Wendy makes the first move.
• In a single move, Wendy can remove from the string any white character that has exactly 2 white neighbors.
• Similarly, in a single move, Bob can remove from string any black character that has exactly 2 black neighbors.
• When a character is removed, the strings shrink itself, so if a character Y had neighbors X and Z on its left and right respectively before the move, after the move is made, X and Z become each other's neighbors.
• The first player who cannot perform a move loses the game. For example, if the colors string is wwwbb, with the first move Wendy will change it to wwbb, and Bob can no longer perform a move. Determine who has a winning strategy assuming that both Wendy and Bob play optimally
ANY BETTER OPTIMISED APPROACH THAN MY APPROACH PLEASE SUGGEST
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
using namespace std;
string s="wwwbb";
int main() {
// your code goes here
int cnt=0;
restart:
//int precnt;
for(int i=1;i<s.size()-1;i++)
{
// precnt=cnt;
char b;
if(cnt==0)
b='w';
else
b='b';
//cout<<b;
if(s[i]==b && s[i-1]==b && s[i+1]==b)
{//cout<<"i "<<i<<endl;
//cout<<i<<"bbbbbb"
cnt=!cnt;
//cout<<s<<endl;
//cout<<"i"<<i<<endl;
//cout<<s[i]<<endl;
s.erase(i,1);
//cout<<s;
// i=1;
goto restart;
}
else if(s.size()==2)
{
//cout<<!cnt;
if(!cnt==0)
cout<<"wendy wins"<<endl;
else
cout<<"bob wins"<<endl;
}
else if(i==s.size()-2)
{//cout<<s<<endl;
//cout<<!cnt;
if(!cnt==0)
cout<<"wendy wins"<<endl;
else
cout<<"bob wins"<<endl;
break;
}
}
return 0;
}
|