【FZU - 2283 Tic-Tac-Toe】 模擬

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!

題意:給出一個3x3的棋盤,兩人用'x'和'o'下棋,三個棋子連成一條線就算贏,現在給出Kim的棋子樣式,問Kim能否在兩步之內取得勝利(若沒有一步取勝,對手也會再下一步)。


分析:我們可以遍歷棋盤上的點,若點爲'.'則將其變爲Kim的棋子進行一次判斷,是否存在三子連珠,若存在則說明Kim可以贏得比賽。如果改變點後未形成三子連珠,則再次進行遍歷,將'.'變爲Kim的棋子,判斷是否有兩個以上的點符合三子連珠,若存在則Kim可以贏得比賽。

因爲一步之後對手會下一步棋,如果只存在一種滿足的情況,那麼對手會將路堵死,所以要有兩點以上滿足條件。


代碼如下:

#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

#define LL long long
#define INF 0x3f3f3f3f

using namespace std;
const int MX = 5;
char mp[MX][MX];
char op[5];

char read(){
    char c = getchar();
    if(c == ' ' || c == '\n'){
        c = getchar();
    }
    return c;
}

int check(){
    for(int i = 1; i <= 3; i++){
        for(int j = 1; j <= 3; j++){
            if(mp[i][j] == op[0]){
                if(i+2 <= 3 && mp[i+1][j] == op[0] && mp[i+2][j] == op[0])  return 1;
                if(j+2 <= 3 && mp[i][j+1] == op[0] && mp[i][j+2] == op[0])  return 1;
                if(i+2 <= 3 && j+2 <= 3 && mp[i+1][j+1] == op[0] && mp[i+2][j+2] == op[0])  return 1;
                if(i+2 <= 3 && j-3 >= 0 && mp[i+1][j-i] == op[0] && mp[i+2][j-2] == op[0])  return 1;
            }
        }
    }
    return 0;
}

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        for(int i = 1; i <= 3; i++){
            for(int j = 1; j <= 3; j++){
                mp[i][j] = read();
            }
        }
        scanf("%s", op);
        int flag = 0;
        for(int i = 1; i <= 3; i++){
            for(int j = 1; j <= 3; j++){
                if(mp[i][j] == '.'){
                    int cnt = 0;
                    mp[i][j] = op[0];
                    if(check()){
                        flag = 1;
                        break;
                    }
                    else{
                        for(int p = 1; p <= 3; p++){
                            for(int q = 1; q <= 3; q++){
                                if(mp[p][q] == '.'){
                                    mp[p][q] = op[0];
                                    if(check()){
                                        cnt++;
                                    }
                                    mp[p][q] = '.';
                                }
                            }
                        }
                        if(cnt >= 2){
                            flag = 1;
                            break;
                        }
                    }
                    mp[i][j] = '.';
                }
            }
        }
        if(flag)    puts("Kim win!");
        else puts("Cannot win!");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章