zoukankan      html  css  js  c++  java
  • 3C Tic-tac-toe

    题目大意:

    两个选手轮流在 3*3的矩阵上作标记,一个选手总是画一个‘X’, 另一个选手总是画一个‘0’,谁先在水平线上或者垂直线上,或者对角线上,先完成三个点连在一块,谁就赢。画‘×’的选手是第一个画,如果画满了还没分出胜负,那么就是平局。每个单元格是空的‘.’, 或者是‘0’, 或者是‘X。我们需要找到下一步是轮到谁了?
    规则如下:
    1.如果这个局面是不可能出现的就是“illegal”
    2.如果给的这个局面就是第一个选手赢就输出“the first player won"
    3.如果给的这个局面就是第二个选手赢就输出”the second player won“
    4.如果给的这个局面就是平局就输出”draw “。
    5.如果下一步该画‘X’的下了,输出‘first’。
    6.如果下一步该‘0’的下了,输出‘second’。
    ====================================================================
    解析:
    有几种不合法的情况:
    1.X的数量比0的数量大于1
    2.0的数量比X的数量大于0
    3.X和0同时胜利了
    4.X胜利的时候,X比0多的数量不是 1
    5.0胜利的时候,X比0多的数量不是 0
    ======================================================================================
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include <string>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <cstdlib>
    using namespace std;
    typedef __int64 LL;
    const LL INF = 0xffffff;
    const int maxn = 100005;
    const LL MOD = 1e9+7;
    #define Illegal 0 ///判断这个局面不合法
    #define First 1  ///该第一个人了
    #define Second 2  ///该第二个人了
    #define Draw 3   ///平局
    #define Tfpw 4   ///这个局面刚出来F就赢了
    #define Tspw 5  ///这个局面刚出来S就赢了
    char maps[5][5];
    
    bool Ok(int x,int y)
    {
        return x>=0 && x<3 && y>=0 && y < 3;
    }
    
    bool Win(char ch)
    {
        for(int i=0; i<3; i++)
        {
            if(maps[0][i] == ch && maps[1][i] == ch && maps[2][i] == ch)
                return true;
            if(maps[i][0] == ch && maps[i][1] == ch && maps[i][2] == ch)
                return true;
        }
        if(maps[0][0] == ch && maps[1][1] == ch && maps[2][2] == ch)
            return true;
        if(maps[0][2] == ch && maps[1][1] == ch && maps[2][0] == ch)
            return true;
        return false;
    }
    
    int solve()
    {
        int numX = 0, num0 = 0;
        for(int i=0; i<3; i++)
        for(int j=0; j<3; j++)
        {
            if(maps[i][j] == 'X')
                numX ++;
            if(maps[i][j] == '0')
                num0 ++;
        }
        if( !(numX - num0 == 0 || numX - num0 == 1) || (Win('X') && Win('0') ) || (Win('X') && numX - num0 != 1) || (Win('0') && numX - num0 != 0))
            return Illegal;
        if(Win('X'))
            return Tfpw;
        if(Win('0') && numX - num0 == 0)
            return Tspw;
        if(num0 == 4 && numX == 5)
            return Draw;
        if(numX - num0 == 0)
            return First;
        if(numX - num0 == 1)
            return Second;
    
        return 0;
    }
    
    int main()
    {
        for(int i=0; i<3; i++)
            scanf("%s", maps[i]);
    
        int ans = solve();
    
        if(ans == Illegal)
            puts("illegal");
        else if(ans == First)
            puts("first");
        else if(ans == Draw)
            puts("draw");
        else if(ans == Second)
            puts("second");
        else if(ans == Tfpw)
            puts("the first player won");
        else if(ans == Tspw)
            puts("the second player won");
    
        return 0;
    }
     
     
  • 相关阅读:
    会计期间勿关闭后台打开
    动态创建VIEW
    约束变量
    JS 图片懒加载
    如何在Javascript中对数组的遍历使用异步函数
    JavaScript之闭包
    面试:数组去重你会几种呀?
    JavaScript重构技巧-降低函数复杂度
    JavaScript的内置对象
    js/jQuery获取data-*属性值
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4845058.html
Copyright © 2011-2022 走看看