• 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;
    }
    

      

  • 相关阅读:
    原生js ajax与jquery ajax的区别
    ajax的五大步骤
    js中setTimeout()时间参数设置为0的探讨
    js数组与字符串的相互转换方法
    javascript的三个组成部分
    linq 获取不重复数据,重复数据 var unique = arr.GroupBy(o => o).Where(g => g.Count() == 1) .Select(g => g.ElementAt(0));
    C# 随机 抽奖 50个随机码 不重复
    聚集索引和非聚集索引 聚集索引的叶节点就是最终的数据节点,而非聚集索引的叶节仍然是索引节点,但它有一个指向最终数据的指针。
    WPF ControlTemplate,DataTemplate
    C# 实现 奇数偶数排序,奇数在前,偶数在后
  • 原文地址:https://www.cnblogs.com/l609929321/p/9544473.html
Copyright © 2020-2023  润新知