zoukankan      html  css  js  c++  java
  • Codeforces 3C

    C. Tic-tac-toe

    time limit per test
    1 second
    memory limit per test
    64 megabytes
    input
    standard input
    output
    standard output

    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 × 3grid (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.

    Examples
    input
    X0X
    .0.
    .X.
    output
    second

    题意:给出一个井字棋棋盘,第一个人画X,第二个人画0,“.”表示该位置还没有画。两人轮流画,谁先画出一行,一列,或一对角线就算赢。判断棋盘的情况并输出,可能出现的情况有6种,分别是:illegal(棋盘不合法),the first player won(第一个人赢),the second player won(第二个人赢),draw(棋盘画满),first(轮到第一个人下了),second(轮到第二个人下了)。

    题解:模拟。按照给出的棋盘和定义直接模拟即可。但是这题考虑的情况比较多,容易被hack,必须全面考虑,具体的参见代码最后的if判断就是此题全部的分类讨论情况。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 string s[3];
     4 int X,O;
     5 bool win1,win2;
     6 bool solve(char c)
     7 {
     8     for (int i=0;i<3;++i)
     9     {
    10         bool win=1;
    11         for (int j=0;j<3;++j)
    12         if (s[i][j]!=c)
    13         {
    14             win=0;
    15             break;
    16         }
    17         if (win) return 1;
    18     }
    19     for (int i=0;i<3;++i)
    20     {
    21         bool win=1;
    22         for (int j=0;j<3;++j)
    23         if (s[j][i]!=c)
    24         {
    25             win=0;
    26             break;
    27         }
    28         if (win) return 1;
    29     }
    30     if (s[0][0]==c&&s[1][1]==c&&s[2][2]==c) return 1;
    31     if (s[0][2]==c&&s[1][1]==c&&s[2][0]==c) return 1;
    32     return 0;
    33 }
    34 int main()
    35 {
    36     for (int i=0;i<3;++i)
    37     cin>>s[i];
    38     for (int i=0;i<3;++i)
    39     for (int j=0;j<3;++j)
    40     if (s[i][j]=='X') ++X;
    41     else if (s[i][j]=='0') ++O;
    42     win1=solve('X');
    43     win2=solve('0');
    44     if (win1&&win2) return puts("illegal"),0;
    45     if (win1&&X-O!=1) return puts("illegal"),0;
    46     if (win2&&X!=O) return puts("illegal"),0;
    47     if (X-O>1||O>X) return puts("illegal"),0;
    48     if (win1) return puts("the first player won"),0;
    49     if (win2) return puts("the second player won"),0;
    50     if (X+O==9) return puts("draw"),0;
    51     if (X==O) return puts("first"),0;
    52     if (X>O) return puts("second"),0;
    53 }
    View Code
  • 相关阅读:
    个人永久性免费-Excel催化剂功能第31波-数量金额分组凑数功能,财务表哥表姐最爱
    个人永久性免费-Excel催化剂功能第30波-工作表快捷操作(批量创建、命名、排序、工作表目录)
    个人永久性免费-Excel催化剂功能第29波-追加中国特色的中文相关自定义函数
    发现用System.Net.Mail发邮件(代码附后),附件稍微大一点就会造成程序假死. 有没有什么简单的解决办法呢? 多谢!!
    上传文件 获取文件名 --360浏览器
    js中的json对象和字符串之间的转化
    chosen.jquery.js
    select 后台获取text 并根据text 设置value选中项
    iframe中有ajax,设置iframe自适应高度
    charme浏览器 jquery1.9.1min.js 报脚本错误 无jquery.min.map 文件
  • 原文地址:https://www.cnblogs.com/zk1431043937/p/7587751.html
Copyright © 2011-2022 走看看