侧边栏壁纸
博主头像
王小木人

这是很长,很好的一生

  • 累计撰写 141 篇文章
  • 累计创建 43 个标签
  • 累计收到 7 条评论

目 录CONTENT

文章目录

填字母游戏

王小木人
2021-05-22 / 0 评论 / 0 点赞 / 844 阅读 / 1,260 字

K大师在纸上画了一行n个格子,要小明和他交替往其中填入字母。

  1. 轮到某人填的时候,只能在某个空格中填入L或O
  2. 谁先让字母组成了“LOL”的字样,谁获胜。
  3. 如果所有格子都填满了,仍无法组成LOL,则平局。

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

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

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

例如,
输入:
4


LL
L
LL
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;
}
}
0

评论区