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; }

Codeforces Beta Round #3 C. Tic-tac-toe 模拟题的更多相关文章

  1. Codeforces Beta Round #14 (Div. 2) A. Letter 水题

    A. Letter 题目连接: http://www.codeforces.com/contest/14/problem/A Description A boy Bob likes to draw. ...

  2. Codeforces Beta Round #97 (Div. 1) A. Replacement 水题

    A. Replacement 题目连接: http://codeforces.com/contest/135/problem/A Description Little Petya very much ...

  3. Codeforces Beta Round #10 A. Power Consumption Calculation 水题

    A. Power Consumption Calculation 题目连接: http://www.codeforces.com/contest/10/problem/A Description To ...

  4. Codeforces Beta Round #7 A. Kalevitch and Chess 水题

    A. Kalevitch and Chess 题目连接: http://www.codeforces.com/contest/7/problem/A Description A famous Berl ...

  5. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  6. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  7. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  8. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  9. Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

    1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...

  10. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

随机推荐

  1. 一个基于时间注入的perl小脚本

    use strict; use warnings; use LWP::Simple; my %table_and_leng; ;;$count++){ #print "Test Table: ...

  2. 【LA3882】And then there was one

    做sb题也是一种乐趣,是吧…… #include<bits/stdc++.h> #define N 10005 using namespace std; int f[N],m,n,k; i ...

  3. 图论-最近公共祖先-离线Tarjan算法

    有关概念: 最近公共祖先(LCA,Lowest Common Ancestors):对于有根树T的两个结点u.v,最近公共祖先表示u和v的深度最大的共同祖先. Tarjan是求LCA的离线算法(先存储 ...

  4. 2017多校第8场 HDU 6143 Killer Names 容斥,组合计数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:m种颜色需要为两段长度为n的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况. ...

  5. 4.Python3标准库--算法

    (一)functools:管理函数的工具 import functools ''' functools模块提供了一些工具来管理或扩展和其他callable对象,从而不必完全重写 ''' 1.修饰符 f ...

  6. linux磁盘占用跟每个文件夹大小总和不符

    1.一种情况是删除了大文件但是没有释放出来,因为有进程还在调用使用 最简单的方法是reboot下服务器再对比下: 2.查看服务器空间使用情况 df -h cd / du -sh *

  7. P1968

    题目背景 此处省略maxint+1个数 题目描述 在以后的若干天里戴维将学习美元与德国马克的汇率.编写程序帮助戴维何时应买或卖马克或美元,使他从100美元开始,最后能获得最高可能的价值. 输入输出格式 ...

  8. 《深入浅出MyBatis技术原理与实战》——3. 配置

    要注意的是上面那些层次是不能够颠倒顺序的,否则MyBatis在解析文件的时候就会出现异常. 3.1 properties元素 properties是一个属性配置元素,让我们能在配置文件的上下文中使用它 ...

  9. C语言实现二叉排序树

    程序以'#'结尾的二叉排序树. /*(双重指针 BSTree *T)问:数据结构中 二叉树建立结点为什么用 双重指针?详细解释下双重指针 答:指针的指针.因为树的结点要用指针描述.如果只用指针,作形参 ...

  10. ffmepg的基本使用

    基本使用命令 ffmpeg -i input.mp4 output.avi //视频格式转换 ffmepg -i input.mp4 -r fps image%3d.jpg //视频转成图片 //fp ...