zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #3 C. Tic-tac-toe 模拟题

    C. Tic-tac-toe

    题目连接:

    http://www.codeforces.com/contest/3/problem/C

    Description

    Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always draws crosses, the other — noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is announced.

    You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below:

    illegal — if the given board layout can't appear during a valid game;
    the first player won — if in the given board layout the first player has just won;
    the second player won — if in the given board layout the second player has just won;
    draw — if the given board layout has just let to a draw.

    Input

    The input consists of three lines, each of the lines contains characters ".", "X" or "0" (a period, a capital letter X, or a digit zero).

    Output

    Print one of the six verdicts: first, second, illegal, the first player won, the second player won or draw.

    Sample Input

    X0X
    .0.
    .X.

    Sample Output

    second

    Hint

    题意

    就那个XO游戏,谁先画三个X或者三个O在一条直线就赢了。

    现在给你一个局面,让你判断这个局面是否合法,谁胜利了。

    如果没人胜利,输出下一步谁走。

    X先走。

    题解:

    模拟题,没啥好说的。

    就判断一下谁胜利就好了,然后剩下的瞎判一下……

    代码

    #include<bits/stdc++.h>
    using namespace std;
    string s[3];
    
    int check(char c)
    {
        for(int i=0;i<3;i++)
        {
            int win = 1;
            for(int j=0;j<3;j++)
                if(s[i][j]!=c)
                    win=0;
            if(win==1)return win;
        }
        for(int i=0;i<3;i++)
        {
            int win=1;
            for(int j=0;j<3;j++)
                if(s[j][i]!=c)
                    win=0;
            if(win==1)return win;
        }
        if(s[0][0]==c&&s[1][1]==c&&s[2][2]==c)return 1;
        if(s[0][2]==c&&s[1][1]==c&&s[2][0]==c)return 1;
        return 0;
    }
    int main()
    {
        for(int i=0;i<3;i++)
            cin>>s[i];
        int X=0,O=0;
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
                if(s[i][j]=='X')X++;
                else if(s[i][j]=='0')O++;
        int Win1 = check('X');
        int Win2 = check('0');
        if(Win1&&Win2)return puts("illegal"),0;
        if(Win1&&X-O!=1)return puts("illegal"),0;
        if(Win2&&X!=O)return puts("illegal"),0;
        if(X-O>1||O>X)return puts("illegal"),0;
        if(Win1)return puts("the first player won"),0;
        if(Win2)return puts("the second player won"),0;
        if(X+O==9)return puts("draw"),0;
        if(X==O)return puts("first"),0;
        if(O==X-1)return puts("second"),0;
    
    }
  • 相关阅读:
    extern “C”的作用详解
    const和typedef的常见用法详解
    虚函数、纯虚函数、虚函数与析构函数
    C++中四种类型转换方式
    面经中高频知识点归纳(一)
    leetcode Database4
    32位机和64位机下面各类型sizeof的大小
    Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作
    字符串变量的定义与引用
    使用字符数组及相关函数,求3个国家名中字母顺序排在最前面的国家。
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5266662.html
Copyright © 2011-2022 走看看