Tic-Tac-Toe FZU - 2283(思维+暴力)

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!

 
题意:给一个3*3的棋盘,'o'和'x'代表双方的在棋盘上的棋,'.'代表棋盘上无任何棋,问在两步之内所给的玩家能否赢棋
思路:只有自己的棋在一条线上才算赢棋(所在的横轴,所在的数轴,对角线上)
          要想在两步之内赢棋,则当前自己走,下一步对手走,再下一步自己走,赢棋!!分两种赢棋方式
         1.一步赢棋:存在一条直线满足,有一个是'.'(无棋),两个是我方棋子,则赢棋
         2.两步赢棋:存在一点是'.'(无棋),与这点向连的直线满足另外两点里,一点是我方棋子,第三点事无棋,满足这
                              一条件的直线至少2条
                            放棋步骤:我方放在符合条件的位置
                                             对方放在满足条件的任意一条边
                                             我方放在另一条边,赢棋
代码:
#include<stdio.h>
#include<string.h>
using namespace std;
char mp[3][3],str[3];
void getMap(){  //读入当前棋盘 
    for(int i=0;i<3;i++)
    for(int j=0;j<3;j++){
        scanf("%c",&mp[i][j]);
        getchar();
    }
    scanf("%s",str);
    getchar();
}
int Find(int x,int y){ //找满足与当前空地连的边 
    int flag1=0,flag2=0;
    int n=0,m=0;
    for(int i=0;i<3;i++){//所在的横轴 
        if(mp[x][i]=='.') n++;//记录'.' 数量 
        else if(mp[x][i]==str[0]) m++;//我方棋子的数量 
    }
     if(n==1&&m==2) flag1++;
     else if(n==2&&m==1) flag2++;

     n=m=0;
     for(int i=0;i<3;i++){//所在纵轴 
        if(mp[i][y]=='.') n++;
        else if(mp[i][y]==str[0]) m++;
    }
    if(n==1&&m==2) flag1++;
    else if(n==2&&m==1) flag2++;


    if(x==y){//主对角线 
       n=m=0;
       for(int i=0;i<3;i++){
         if(mp[i][i]=='.') n++;
         else if(mp[i][i]==str[0]) m++;
       }
       if(n==1&&m==2) flag1++;
       else if(n==2&&m==1) flag2++;
    }

    if(x+y==2){//副对角线 
        n=m=0;
        for(int i=0;i<2;i++){
            if(mp[i][2-i]=='.') n++;
            else if(mp[i][2-i]==str[0]) m++;
        }
       if(n==1&&m==2) flag1++;
       else if(n==2&&m==1) flag2++;
    }
    return flag1>0||flag2>=2;
}
int main(){
    int T;
    scanf("%d",&T);
    getchar();
    while(T--){
        getMap();
        int flag=1;
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){//遍历所有可以放棋子的位置 
                if(mp[i][j]=='.'&&Find(i,j)){
                    flag=0;
                    break;
                }
            }
            if(!flag) break;
        }
        if(flag) printf("Cannot win!\n");
        else printf("Kim win!\n");
    }
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值