Tic-Tac-Toe FZU - 2283 (暴力)

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!

直接暴力行列对角线,赢有两种情况,第一种:某条连线上有两个自己的棋子和一个空白,这样的情况只需要一个,第一步就可以赢。第二种:两条相交直线上都是有一个自己的棋子和两个空白,这样两步之内一定可以赢。

code:

#include 
#include 
#include 
using namespace std;
char mp[5][5];
char chess;
int judge(int x,int y){
    int flag1 = 0,flag2 = 0;//flag1代表一条连线中有两个自己的棋,那么第一步就可以赢,这样的情形只需要一次
                            //flag2代表一条连线中有一个自己的棋,两个空白,那么这样的情形需要两个才能保证两步内赢
    int n = 0,m = 0;//n记录空白个数,m记录自己棋子的个数
    for(int i = 0; i < 3; i++){
        if(mp[x][i] == '.') n++;
        else if(mp[x][i] == chess) 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] == chess) 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] == chess) 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 < 3; i++){
            if(mp[i][2-i] == '.') n++;
            else if(mp[i][2-i] == chess) 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);
    while(t--){
        getchar();
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                scanf("%c ",&mp[i][j]);
            }
        }
        scanf("%c",&chess);
        int flag = 0;
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                if(mp[i][j] == '.'){
                    if(judge(i,j)){
                        flag = 1;
                        break;
                    }
                }
            }
            if(flag) break;
        }
        if(flag) printf("Kim win!\n");
        else printf("Cannot win!\n");
    }
    return 0;
}


你可能感兴趣的:(思维技巧,暴力)