FZU-2283 Tic-Toc-Tac(模拟)

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!

题意:

问当前局势下一步能否出现必胜局。有两种情况:
1:直接一步就连成三个。

2:通过一步可以形成两个可连的三线

思路:

参见n皇后的储存思想。
用四个数组存横、竖、左斜、右斜。每次判断的时候看各个数组的储存情况。

代码:

#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
int a1[5], b1[5], c1[5], d1[5];     ///o的横、竖、左斜、右斜
int a2[5], b2[5], c2[5], d2[5];     ///.的横、竖、左斜、右斜
char a[5][5];
int n = 3;    ///3*3的方格
bool judge1(){  ///判断是否一步就win
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            if(a[i][j] == '.'){
                if(a1[i]==2|| b1[j]==2 || c1[i+j]==2 || d1[i-j+n]==2)
                    return true;
            }
        }
    }
    return false;
}
bool judge2(){    ///判断是否第二步可以必胜
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            int num = 0;
            if(a[i][j] == '.'){
                if((a1[i]==1)&&a2[i]==2) num++;
                if(b1[j]==1&&b2[j]==2) num ++;
                if(c1[i+j]==1&&c2[i+j]==2) num ++;
                if(d1[i-j+n]==1&&d2[i-j+n]==2) num ++;
            }
            if(num >= 2) return true;
        }
    }
    return false;
}
bool judge3(){
    if(judge1() || judge2()) return true;
    return false;
}
void init(){
    memset(a1, 0, sizeof(a1));
    memset(b1, 0, sizeof(b1));
    memset(c1, 0, sizeof(c1));
    memset(d1, 0, sizeof(d1));
    memset(a2, 0, sizeof(a2));
    memset(b2, 0, sizeof(b2));
    memset(c2, 0, sizeof(c2));
    memset(d2, 0, sizeof(d2));
}
int main()
{
    int t;
    char ch;
    cin >> t;
    getchar();
    while(t--)
    {
        init();
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                scanf("%c", &a[i][j]);
                getchar();
            }
        }
        getchar();
        scanf("%c", &ch);
        if(ch == 'x'){
            for(int i=1; i<=n; i++){
                for(int j=1; j<=n; j++){
                if(a[i][j] == 'x')
                    a[i][j] = 'o';
                else if(a[i][j] == 'o')
                    a[i][j] = 'x';
                }
            }
        }
        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                if(a[i][j] == 'o'){
                    a1[i] ++;
                    b1[j] ++;
                    c1[i+j] ++;
                    d1[i-j+n] ++;
                }else if(a[i][j] == '.'){
                    a2[i] ++;
                    b2[j] ++;
                    c2[i+j] ++;
                    d2[i-j+n] ++;
                }
            }
        }
        if(judge3()) cout << "Kim win!" << endl;
        else cout << "Cannot win!" << endl;
    }
}

猜你喜欢

转载自blog.csdn.net/a_thinking_reed_/article/details/80370411
今日推荐