CodeForces 3C---Tic-tac-toe--思维题

原创 2015年07月09日 11:23:26
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: firstsecondillegalthe first player wonthe second player won or draw.

Sample test(s)
input
X0X
.0.
.X.
output
second

此题需考虑全面。

首先考虑不合法的情况,有如下4种:

1、X的数量少于0的数量。

2、X的数量-0的数量>1。

3、X数量等于0的数量,而此时存在三个X相连(即先手获胜)。这是不可能的,因为先手下子之后,X的数量必然多于0的数量,不可能相等。

4、X的数量大于0的数量,而此时存在三个0相连(即后手获胜)。这是不可能的,因为后手下子之后X的数量和0的数量应当相等。

注:当X为五个且有两个三连重叠时为第一个人赢,因为可能最后一步放在交叉点,--,一开始以为是illegal;

#include <iostream>

using namespace std;
char a[4][4];
bool sh(char c)
{
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(a[i][j]!=c)
                break;
            if(j==2)
                return 1;
        }
    }
    return 0;
}
bool sl(char c)
{
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(a[j][i]!=c)
                break;
            if(j==2)
                return 1;
        }
    }
    return 0;
}
bool sxl(char c)
{
    for(int i=0;i<3;i++)
    {
        if(a[i][i]!=c)
            break;
        if(i==2)
            return 1;
    }
    return 0;
}
bool sxr(char c)
{
    for(int i=0;i<3;i++)
    {
        if(a[i][2-i]!=c)
            break;
        if(i==2)
            return 1;
    }
    return 0;
}
int main()
{
    while(cin>>a[0])
    {   int sum1=0,sum2=0;
        cin>>a[1]>>a[2];
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                if(a[i][j]=='X')
                    sum1++;
                if(a[i][j]=='0')
                    sum2++;
            }
        }
        int flag=0;
        if(sum1<sum2||sum1>sum2+1)
            flag=1;
        else if((sh('X')==1||sl('X')==1||sxl('X')==1||sxr('X')==1)&&sum1==sum2)
                flag=1;
        else if((sh('0')==1||sl('0')==1||sxl('0')==1||sxr('0')==1)&&sum1>sum2)
            flag=1;
        //else if((sh('X')==1&&sl('X')==1)||(sh('X')==1&&sxl('X')==1)||(sh('X')==1&&sxr('X')==1)||(sl('X')==1&&sxl('X')==1)||(sl('X')==1&&sxr('X')==1)||(sxr('X')==1&&sxl('X')==1))
            // flag=1;
        else if(sh('X')==1||sl('X')==1||sxl('X')==1||sxr('X')==1)
            flag=2;
        else if(sh('0')==1||sl('0')==1||sxl('0')==1||sxr('0')==1)
            flag=3;
        else if(sum1==sum2)
            flag=4;
        else if(sum1>sum2&&(sum1+sum2)!=9)
            flag=5;
        else if(sum1>sum2&&(sum1+sum2)==9)
            flag=6;
            if(flag==1)
                cout<<"illegal"<<endl;
            if(flag==2)
                cout<<"the first player won"<<endl;
            if(flag==3)
                cout<<"the second player won"<<endl;
            if(flag==4)
                cout<<"first"<<endl;
            if(flag==5)
                cout<<"second"<<endl;
            if(flag==6)
                cout<<"draw"<<endl;
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces Beta Round #3-C. Tic-tac-toe

题意: 就是给你一个井字棋的棋面,然后让你判断当前属于哪种情况。 思路: 首先就是判断不合法的情况没有考虑清楚。 最重要的一点是我们应该先判断X是否赢,因为它是先手,然后再判断0是否赢。 其...

CodeForces 3C Tic-tac-toe 井字棋盘游戏

CodeForces 3C Tic-tac-toe 井字棋盘游戏

codeforces 3C Tic-tac-toe

C. Tic-tac-toe time limit per test 1 second memory limit per test 64 megabytes inpu...
  • ahoLic
  • ahoLic
  • 2014-04-03 13:00
  • 2258

Codeforces 3C Tic-tac-toe

EnglishDescCertainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. ...

Codeforces Beta Round #3 C. Tic-tac-toe

井字棋 ,这个模拟也是醉了 题目大意: 给出一个井字棋棋局,判断他是不是合法的。若合法且出现某人赢是谁赢了,没出现某人赢是下一步该谁下,还是平局。 解题思路: 分类讨论...

Codeforces 3C. Tic-tac-toe

C. Tic-tac-toe time limit per test 1 second memory limit per test 64 megabytes inpu...

FZU2283+Tic-Tac-Toe+【思维+暴力枚举】+ 第八届福建省大学生程序设计竞赛

Problem 2283 Tic-Tac-Toe Accept: 35    Submit: 46 Time Limit: 1000 mSec    Memory Limit : 262144...

Codeforces Round #390 (Div. 2)-B Ilya and tic-tac-toe game(模拟)

题目链接题目大意: 有一个4*4的图,先手画’x’,后手画’o’,谁先满足在对角线或者横线或竖线上有三个连续的标记,则这个人获胜。 问在这个图上画一个’x’标记,是否可以获胜。直接暴力模拟这个过程...

Codeforces Round #390 (Div. 2)B Ilya and tic-tac-toe game

题目大意:       两个人走三子棋问下一步是否能赢。 题目解法:       枚举所有情况就行了,反正在边上的就8种,在中间的8种。 代码: #include "iostream&quo...

Codeforces Round #390(Div. 2)B. Ilya and tic-tac-toe game【模拟】

B. Ilya and tic-tac-toe game time limit per test 2 seconds memory limit per test 256 megabyt...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)