Codeforces Beta Round #3 C. Tic-tac-toe 模拟题
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 模拟题的更多相关文章
- 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. ...
- Codeforces Beta Round #97 (Div. 1) A. Replacement 水题
A. Replacement 题目连接: http://codeforces.com/contest/135/problem/A Description Little Petya very much ...
- Codeforces Beta Round #10 A. Power Consumption Calculation 水题
A. Power Consumption Calculation 题目连接: http://www.codeforces.com/contest/10/problem/A Description To ...
- Codeforces Beta Round #7 A. Kalevitch and Chess 水题
A. Kalevitch and Chess 题目连接: http://www.codeforces.com/contest/7/problem/A Description A famous Berl ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- 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]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- 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 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
随机推荐
- 概率DP入门学习QAQ
emmmm博客很多都烂尾了...但是没空写..先写一下正在学的东西好了 概率DP这东西每次考到都不会..听题解也是一脸懵逼..所以决定学习一下这个东东..毕竟NOIP考过...比什么平衡树实在多了QA ...
- JS中的日期内置函数
用JS中的日期内置函数实现在页面显示:“今天是:2013年9月26日14:32:45”. var date=new Date(Date.parse('9/26/2013 14:32:45')); ...
- python基础===函数的几个要点
函数 可接受任意数量参数的函数 位置参数 和 关键字参数 为了能让一个函数接受任意数量的位置参数,可以使用一个*参数. def avg(first, *r): return (first + s ...
- 【Android开发日记】之基础篇(二)——Android的动画效果
什么是动画,动画的本质是通过连续不断地显示若干图像来产生“动”起来的效果.比如说一个移动的动画,就是在一定的时间段内,以恰当的速率(起码要12帧/秒以上,才会让人产生动起来的错觉)每隔若干 ...
- hadoop 分布式环境安装
centos 多台机器免密登录 hadoop学习笔记(五)--全分布模式下SSH免密码登陆的实现 参考安装教程 Hadoop-2.7.4 集群快速搭建 启动hadoop cd /opt/soft/ha ...
- AC日记——双栈排序 洛谷 P1155
双栈排序 思路: 二分图染+模拟: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1005 #define ...
- LoadRunner11破解方法
前期准备:LoadRunner11 下载LoadRunner破解文件 下载LoadRunner注册表清理工具 下载 LoadRunner11破解方法:一.覆盖破解文件首先请下载LoadRunner破解 ...
- MyBatis 返回(批量)新增数据的自增id
<insert id="save" parameterType="Vote" useGeneratedKeys="true" keyP ...
- react native 问题点
问题点一:安装了react-native-vector-icons后,编译出错 版本: "react": "16.2.0", "react-nativ ...
- 【ASP.NET MVC】 路由机制:命名路由
首先看一下命名路由和没有命名的差别: 命名路由: routes.MapRoute( name: "Test", // Route name url: "code/p/{a ...