FZU - 2283 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!

题意:输入一个棋盘,判断两步之内能不能赢,每行每列或者对角线有三个子就算赢

为数不多的没看题解就自己写出来的题,有点小激动诶!!!咳咳......下面说下我的思路:

其实说是判断两步之内能不能赢,但是只要下一步就能判断出来能不能赢,举个栗子说:下五子棋的时候,如果有四个子连成一条线,并且两边上没有阻碍的话,这局棋胜负已经决定了,就无需再下了

就是说如果只有一个位置下完之后两个子,对方不想让你赢,肯定会去堵你,这样你就赢不了,但是如果有两个位置已经是两个子了,无论对面堵那个,你都会赢

例:x . x 无论怎么下都是死棋了,话不多说,上代码!!!

. . .

x . .

#include<iostream>
using namespace std;
char a[4][4];
char s;
int dfs(int x,int y,char w)             //并不是dfs,只是顺手起的名
{
    int mark=0;                       //标记有几个点快要完成
    for(int i=1;i<=3;i++)             //判断每行是否有符合要求的
    {
       int w1=0,w1s=0;
       for(int j=1;j<=3;j++)
       {
            if(a[i][j]==w) w1++;
            else if(a[i][j]=='.') w1s++;
            if(w1==3) return 1;            //如果已经有三个点直接退出
            else if(w1==2&&w1s==1) mark++;    //两个点和一个'.'符合要求  
       }
    }
    for(int i=1;i<=3;i++)              //判断每列是否有符合要求的
    {
        int w2=0,w2s=0;
        for(int j=1;j<=3;j++)
        {
            if(a[j][i]==w) w2++;
            else if(a[j][i]=='.')  w2s++;
            if(w2==3) return 1;
            else if(w2==2&&w2s==1) mark++;
        }
    }
    for(int i=1;i<=3;i++)                  //判断主对角线
    {
        int w3=0,w3s=0; 
            if(a[i][i]==w) w3++;
            else if(a[i][i]=='.') w3s++;
            if(w3==3) return 1;
            else if(w3==2&&w3s==1)  mark++;
    }
    for(int i=1;i<=3;i++)                  //判断副对角线
    {
        int w4=0,w4s=0;
            if(a[i][4-i]==w) w4++;
            else if(a[i][4-i]=='.') w4s++;
            if(w4==3) return 1;
            else if(w4==2&&w4s==1)  mark++;
    }
    if(mark>=2)                         //超过两处直接退出
        return 1;
    return 0;
}
int main()
{
    int t;
    while(cin>>t)
    {
        while(t--)
        {
            for(int i=1;i<=3;i++)
            {
                for(int j=1;j<=3;j++)
                {
                    cin>>a[i][j];
                }
            }
            cin>>s;
            int ok;
            for(int i=1;i<=3;i++)
            {
                for(int j=1;j<=3;j++)
                {
                    ok=0;
                    if(a[i][j]=='.')
                    {
                        a[i][j]=s;             //假设将棋下在此处
                   if(dfs(i,j,s)==1)
                        {
                            ok=1;
                            break;
                        }
                        a[i][j]='.';          //如果下在此处不符合要求,将标记删除!!!
                    }
                }
                if(ok==1)
                    break;
            }
            if(ok==1)
                cout<<"Kim win!"<<endl;
            else
            cout<<"Cannot win!"<<endl;
        }
    }
}
/**
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       每次AC
**/



猜你喜欢

转载自blog.csdn.net/asd1637579311/article/details/80158793
今日推荐