CF3C 暴力

原创 2015年07月10日 11:59:48

http://codeforces.com/problemset/problem/3/C

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 a3 × 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: firstsecondillegalthe first player wonthe second player won or draw.

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

/**
CF3C 暴力
题目大意:给定一个3*3的棋盘,判断现在的情况下是属于什么状态(状态有6种)
解题思路:判断非法的时候要注意如果已经出现赢的情况了,棋盘就必须结束。其余就是简单暴力
*/
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
char a[5][5];
int cnt0,cntx;
bool win(char x)
{
    if(a[0][0]==x&&a[0][1]==x&&a[0][2]==x)return true;
    if(a[1][0]==x&&a[1][1]==x&&a[1][2]==x)return true;
    if(a[2][0]==x&&a[2][1]==x&&a[2][2]==x)return true;
    if(a[0][0]==x&&a[1][0]==x&&a[2][0]==x)return true;
    if(a[0][1]==x&&a[1][1]==x&&a[2][1]==x)return true;
    if(a[0][2]==x&&a[1][2]==x&&a[2][2]==x)return true;
    if(a[2][0]==x&&a[1][1]==x&&a[0][2]==x)return true;
    if(a[0][0]==x&&a[1][1]==x&&a[2][2]==x)return true;
    return false;
}
bool judge()
{
    if(win('X')&&cntx==cnt0)return true;
    if(win('0')&&cntx==cnt0+1)return true;
    if(cntx>cnt0+1||cntx<cnt0)return true;
    return false;
}
int main()
{
    for(int i=0;i<3;i++)
    {
        scanf("%s",a[i]);
    }
    cnt0=0,cntx=0;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(a[i][j]=='0')
            {
                cnt0++;
            }
            if(a[i][j]=='X')
            {
                cntx++;
            }
        }
    }
    if(judge())
    {
        printf("illegal\n");
    }
    else if(win('0'))
    {
        printf("the second player won\n");
    }
    else if(win('X'))
    {
        printf("the first player won\n");
    }
    else if(cnt0+cntx==9)
    {
        printf("draw\n");
    }
    else if(cnt0==cntx)
    {
       printf("first\n");
    }
    else
    {
       printf("second\n");
    }
    return 0;
}
/**
X0.
...
...

X0X
0X0
0X0

XXX
XXX
XXX

000
X..
...

0X.
X0.
...

X0X
0X0
X0X
*/


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

相关文章推荐

CF 558 C. Amr and Chemistry 暴力+二进制

题意: 给你若干个数。 对每个数都可以进行除或者乘的操作。 3/2=1 问 最少 多少步操作,可以让所有数字都相等。 做法: 首先如果都变成一个数字,那这个数字肯定是小于等于这些数字里的最大的那个...

CF 474C Captain Marmot[暴力枚举 || bfs状态压缩 ]

题目链接:http://codeforces.com/problemset/problem/474/C 问题的意思还是比较简单的,...

s3c6410_CF_SD源代码

  • 2011年08月06日 19:26
  • 7.5MB
  • 下载

CF-A7-MX C.A780T D3 V19

  • 2017年09月30日 17:17
  • 1024KB
  • 下载

CF#278 (Div. 2) A.(暴力枚举)

A. Giga Tower time limit per test 1 second memory limit per test 256 megabytes inpu...

CF - 813B. The Golden Age - 数学+暴力+二分

1.题目描述: B. The Golden Age time limit per test 1 second memory limit per test 256 me...

cf 251 B Playing with Permutations 暴力 分类讨论

题意: 题意是,给q数列,和s数列。然后p数列初始为1-n。然后通过p[q[i]]=p[i],或者p[i]=p[q[i]]这两种变换,问有没有可能在k次变换后刚刚p数列为s数列。并且在这k次变换过程中...

CF - 812B. Sagheer, the Hausmeister - 暴力+dp

1.题目描述: B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 meg...

CF - 797E. Array Queries - dp+有选择地暴力

题目描述: E. Array Queries time limit per test 2 seconds memory limit per test ...

CF - 757B. Bash's Big Day - 贪心+暴力

1.题目描述: B. Bash's Big Day time limit per test 2 seconds memory limit per test 512 m...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CF3C 暴力
举报原因:
原因补充:

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