填字母游戏

原文地址

K大师在纸上画了一行n个格子,要小明和他交替往其中填入字母。
1. 轮到某人填的时候,只能在某个空格中填入L或O
2. 谁先让字母组成了“LOL”的字样,谁获胜。
3. 如果所有格子都填满了,仍无法组成LOL,则平局。


小明试验了几次都输了,他很惭愧,希望你能用计算机帮他解开这个谜。


本题的输入格式为:
第一行,数字n(n<10),表示下面有n个初始局面。
接下来,n行,每行一个串,表示开始的局面。
  比如:“******”, 表示有6个空格。“L****”,   表示左边是一个字母L,它的右边是4个空格。


要求输出n个数字,表示对每个局面,如果小明先填,当K大师总是用最强着法的时候,小明的最好结果。
1 表示能赢
-1 表示必输
0 表示可以逼平


例如,
输入:
4
***
L**L
L**L***L
L*****L


则程序应该输出:
0
-1
1
1




#include<iostream>
#include<cmath>
using namespace std;
int main()
{
   int game(string);
   cout<<game("***")<<'\n';
   cout<<game("L**L")<<'\n';
   cout<<game("L**L***L")<<'\n';
   cout<<game("L*****L")<<'\n';
   
   return 0;
}
int game(string str)
{
bool contains(string,string);
if(contains(str,"LOL")) return -1;
if(!contains(str,"*")) return 0;
bool ping =false;
for(int i=0;i<str.length();i++)
{
if(str[i]=='*')
{

       
str[i]='L';
int result=game(str);
str[i]='*';
switch(result)
{

case -1:return 1;
case 0:ping=true;
                 
  }
str[i]='O';
int result2=game(str);
str[i]='*';
switch(result2)
{
case -1:return 1;
case 0:ping=true;
                   
}

     

}
}
if(ping) return 0;
return -1;
}
bool contains(string strp,string strs)
{
string::size_type idx=strp.find(strs);
if(idx==string::npos)
{
return false;
}else
{
return true;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值