2017福建省赛 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!

题意:下九宫棋,Kim先手,问Kim两步之内是否可以获胜
分析:1.枚举每个Kim可以下棋的地方
   2.首先看Kim下了这部棋后是否阻住了已经有两颗棋的对方
   3.然后再看Kim下了这部棋后是否可以获胜,获胜的状态有两种,一种是三棋相连直接获胜,一种是这部棋后我有两个地方可以下棋子构成三子相连
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 10;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const ll inf = 1e9;
const double pi = acos(-1.0);
char mp[maxn][maxn];
bool check( char c ) {
    if(mp[1][1]==c&&mp[1][1]==mp[1][2]&&mp[1][1]==mp[1][3]) return true;
    if(mp[2][1]==c&&mp[2][1]==mp[2][2]&&mp[2][1]==mp[2][3]) return true;
    if(mp[3][1]==c&&mp[3][1]==mp[3][2]&&mp[3][1]==mp[3][3]) return true;
    if(mp[1][1]==c&&mp[1][1]==mp[2][1]&&mp[1][1]==mp[3][1]) return true;
    if(mp[1][2]==c&&mp[1][2]==mp[2][2]&&mp[1][2]==mp[3][2]) return true;
    if(mp[1][3]==c&&mp[1][3]==mp[2][3]&&mp[1][3]==mp[3][3]) return true;
    if(mp[1][1]==c&&mp[1][1]==mp[2][2]&&mp[1][1]==mp[3][3]) return true;
    if(mp[1][3]==c&&mp[1][3]==mp[2][2]&&mp[1][3]==mp[3][1]) return true;
    return false;
}
bool ok( char c ) {
    if( check(c) ) { //是否构成三子相连
        return true;
    }
    ll cnt = 0;
    //是否有两个地方可以再下一颗棋子构成三子相连
    for( ll i = 1; i <= 3; i ++ ) { 
        for( ll j = 1; j <= 3; j ++ ) {
            if( mp[i][j] == '.' ) {
                mp[i][j] = c;
                if( check(c) ) {
                    cnt ++;
                }
                mp[i][j] = '.';
            }
        }
    }
    if( cnt >= 2 ) {
        return true;
    }
    return false;
}
int main() {
    ll T;
    cin >> T;
    while( T -- ) {
        for( ll i = 1; i <= 3; i ++ ) {
            for( ll j = 1; j <= 3; j ++ ) {
                cin >> mp[i][j];
            }
        }
        char c1, c2;
        cin >> c1;
        if( c1 == 'x' ) {
            c2 = 'o';
        } else {
            c2 = 'x';
        }
        bool flag = false;
        for( ll i = 1; i <= 3; i ++ ) {
            for( ll j = 1; j <= 3; j ++ ) {
                if( mp[i][j] == '.' ) {
                    mp[i][j] = c1;
                    if( !ok(c2) && ok(c1) ) {
                        flag = true;
                    }
                    mp[i][j] = '.';
                }
            }
        }
        if( flag ) {
            cout << "Kim win!" << endl;
        } else {
            cout << "Cannot win!" << endl;
        }
    }
    return 0;
}

  

posted on 2018-08-27 20:12  九月旧约  阅读(211)  评论(0编辑  收藏  举报

导航