L - Tic-Tac-Toe

Kim likes to play Tic-Tac-Toe.

Given a current state, and now Kim is going to take his next move. Please tell Kim if he can win the game in next 2 moves if both player are clever enough.

Here “next 2 moves” means Kim’s 2 move. (Kim move,opponent move, Kim move, stop).

Game rules:

Tic-tac-toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.

Input

First line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.

For each test case: Each test case contains three lines, each line three string(“o” or “x” or “.”)(All lower case letters.)

x means here is a x

o means here is a o

. means here is a blank place.

Next line a string (“o” or “x”) means Kim is (“o” or “x”) and he is going to take his next move.

Output

For each test case:

If Kim can win in 2 steps, output “Kim win!”

Otherwise output “Cannot win!”

Sample Input

3
. . .
. . .
. . .
o
o x o
o . x
x x o
x
o x .
. o .
. . x
o

Sample Output

Cannot win!
Kim win!
Kim win!

 

#include<iostream>
using namespace std;
char MAP[5][5];
bool judge(char c)//判断c是否已经成功
{
    if(MAP[1][1]==c&&MAP[1][2]==c&&MAP[1][3]==c) return 1;
    if(MAP[2][1]==c&&MAP[2][2]==c&&MAP[2][3]==c) return 1;
    if(MAP[3][1]==c&&MAP[3][2]==c&&MAP[3][3]==c) return 1;
    if(MAP[1][1]==c&&MAP[2][1]==c&&MAP[3][1]==c) return 1;
    if(MAP[1][2]==c&&MAP[2][2]==c&&MAP[3][2]==c) return 1;
    if(MAP[1][3]==c&&MAP[2][3]==c&&MAP[3][3]==c) return 1;
    if(MAP[1][1]==c&&MAP[2][2]==c&&MAP[3][3]==c) return 1;
    if(MAP[1][3]==c&&MAP[2][2]==c&&MAP[3][1]==c) return 1;
    return 0;
}
bool win(char c)//Kim是否能有两个以上赢面
{
    if(judge(c))
        return 1;
    int num=0;
    for(int i=1; i<=3; i++)
    {
        for(int j=1; j<=3; j++)
        {
            if(MAP[i][j]=='.')
            {
                MAP[i][j]=c;
                if(judge(c))
                    num++;
                MAP[i][j]='.';
            }
        }
    }
    if(num>=2)
        return 1;
    return 0;
}
bool win1(char c)//对方是否能1步走赢
{
    if(judge(c))
        return 1;
    int num=0;
    for(int i=1; i<=3; i++)
    {
        for(int j=1; j<=3; j++)
        {
            if(MAP[i][j]=='.')
            {
                MAP[i][j]=c;
                if(judge(c))
                    num++;
                MAP[i][j]='.';
            }
        }
    }
    if(num)
        return 1;
    return 0;
}
int main()
{
    std::ios::sync_with_stdio(0);
    int t;
    char c1,c2;
    cin>>t;
    while(t--)
    {
        for(int i=1; i<=3; i++)
        {
            for(int j=1; j<=3; j++)
            {
                cin>>MAP[i][j];
            }
        }
        cin>>c1;
        if(c1=='x')
            c2='o';
        else
            c2='x';
        int flag=0;
        for(int i=1; i<=3; i++)
        {
            for(int j=1; j<=3; j++)
            {
                if(MAP[i][j]=='.')//枚举所有Kim可以放的点
                {
                    MAP[i][j]=c1;//走该步的话,现在该c2走了
                    if(win(c1)&&!win1(c2))//一定要c2一步赢不了的情况下c1一定要有至少2个赢面才算真正的赢
                    {
                        flag=1;
                        goto label;
                    }
                    MAP[i][j]='.';//恢复原状
                }
            }
        }
        label:
        if(flag)
            cout<<"Kim win!"<<endl;
        else
            cout<<"Cannot win!"<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值