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. tenda t402 家庭版 有线路由器

    使用快速向导: adsl(拨号)+用户名+密码 路由器后DMZ主机设置简单图解:http://wenku.baidu.com/view/94b9f0768e9951e79b8927ce.html  可 ...

  2. 《Java编程思想》阅读笔记一

    Java编程思想 这是一个通过对<Java编程思想>(Think in java)第四版进行阅读同时对java内容查漏补缺的系列.一些基础的知识不会被罗列出来,这里只会列出一些程序员经常会 ...

  3. CF1064 E - Dwarves, Hats and Extrasensory Abilities

    题意 交互题, 本来应该是在平面上进行的. 实际上换成一条直线就可以, 其实换成在平面上更复杂一些. Solution 假设\(l\)点是黑点, \(r\)处是白点, 那么就把下一个点的位置放置在\( ...

  4. python 作业

    Linux day01 计算机硬件知识整理 作业要求:整理博客,内容如下 编程语言的作用及与操作系统和硬件的关系 应用程序->操作系统->硬件 cpu->内存->磁盘 cpu与 ...

  5. windows下github 出现Permission denied (publickey)

    github教科书传送门:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 再学习到 ...

  6. Bootstrap框架的简介

    一.Bootstrap介绍 Bootstrap是Twitter开源的基于HTML.CSS.JavaScript的前端框架. 它是为实现快速开发Web应用程序而设计的一套前端工具包. 它支持响应式布局, ...

  7. Python3判断自身脚本是不是在运行

    采用Socket方式,启动时占用一个端口,转化问题为判断端口是不是存在? import sys import locale import http.server import socketserver ...

  8. jstree无限级菜单ajax按需动态加载子节点

    业余时间研究了一下jstree,更新非常快已经是3.0了,首先看一下效果截图: 1.页面引入样式和脚本(注意路径根据实际情况) <link href="~/Scripts/vakata ...

  9. 是时候升级你的Js工具了-分页【基于JQ】

    好久没有来逛园子,也好久没有更新博客,就像沉睡已久的人忽然被叫醒,忽然就被园友的回复惊醒了.园友提出了关于我之前一篇文章的疑问——可那已经是半年以前的博客了,加上我一直觉得分享给大家的应该是我最新的思 ...

  10. OpenStack 存储服务 Cinder存储节点部署LVM (十四)

    部署在block(10.0.0.103)主机 一)配置lvm 1.安装lvm2软件包 yum install lvm2 -y 2.启动LVM的metadata服务并且设置该服务随系统启动 system ...