题目大意:

两个选手轮流在 3*3的矩阵上作标记,一个选手总是画一个‘X’, 另一个选手总是画一个‘0’,谁先在水平线上或者垂直线上,或者对角线上,先完成三个点连在一块,谁就赢。画‘×’的选手是第一个画,如果画满了还没分出胜负,那么就是平局。每个单元格是空的‘.’, 或者是‘0’, 或者是‘X。我们需要找到下一步是轮到谁了?
规则如下:
1.如果这个局面是不可能出现的就是“illegal”
2.如果给的这个局面就是第一个选手赢就输出“the first player won"
3.如果给的这个局面就是第二个选手赢就输出”the second player won“
4.如果给的这个局面就是平局就输出”draw “。
5.如果下一步该画‘X’的下了,输出‘first’。
6.如果下一步该‘0’的下了,输出‘second’。
====================================================================
解析:
有几种不合法的情况:
1.X的数量比0的数量大于1
2.0的数量比X的数量大于0
3.X和0同时胜利了
4.X胜利的时候,X比0多的数量不是 1
5.0胜利的时候,X比0多的数量不是 0
======================================================================================

#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
using namespace std;
typedef __int64 LL;
const LL INF = 0xffffff;
const int maxn = ;
const LL MOD = 1e9+;
#define Illegal 0 ///判断这个局面不合法
#define First 1 ///该第一个人了
#define Second 2 ///该第二个人了
#define Draw 3 ///平局
#define Tfpw 4 ///这个局面刚出来F就赢了
#define Tspw 5 ///这个局面刚出来S就赢了
char maps[][]; bool Ok(int x,int y)
{
return x>= && x< && y>= && y < ;
} bool Win(char ch)
{
for(int i=; i<; i++)
{
if(maps[][i] == ch && maps[][i] == ch && maps[][i] == ch)
return true;
if(maps[i][] == ch && maps[i][] == ch && maps[i][] == ch)
return true;
}
if(maps[][] == ch && maps[][] == ch && maps[][] == ch)
return true;
if(maps[][] == ch && maps[][] == ch && maps[][] == ch)
return true;
return false;
} int solve()
{
int numX = , num0 = ;
for(int i=; i<; i++)
for(int j=; j<; j++)
{
if(maps[i][j] == 'X')
numX ++;
if(maps[i][j] == '')
num0 ++;
}
if( !(numX - num0 == || numX - num0 == ) || (Win('X') && Win('') ) || (Win('X') && numX - num0 != ) || (Win('') && numX - num0 != ))
return Illegal;
if(Win('X'))
return Tfpw;
if(Win('') && numX - num0 == )
return Tspw;
if(num0 == && numX == )
return Draw;
if(numX - num0 == )
return First;
if(numX - num0 == )
return Second; return ;
} int main()
{
for(int i=; i<; i++)
scanf("%s", maps[i]); int ans = solve(); if(ans == Illegal)
puts("illegal");
else if(ans == First)
puts("first");
else if(ans == Draw)
puts("draw");
else if(ans == Second)
puts("second");
else if(ans == Tfpw)
puts("the first player won");
else if(ans == Tspw)
puts("the second player won"); return ;
}
 
 

3C Tic-tac-toe的更多相关文章

  1. 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 ...

  2. POJ 2361 Tic Tac Toe

    题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...

  3. 【leetcode】1275. Find Winner on a Tic Tac Toe Game

    题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-To ...

  4. 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

    题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...

  5. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  6. Epic - Tic Tac Toe

    N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...

  7. python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  8. ACM-Team Tic Tac Toe

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...

  9. LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

    地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...

  10. 【Python五篇慢慢弹】数据结构看python

    数据结构看python 作者:白宁超 2016年10月9日14:04:47 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给出的pythondoc ...

随机推荐

  1. Java基础知识强化之集合框架笔记32:集合之可变参数的概述和使用

    1. 可变参数的概述和使用: (1)可变参数:定义方法的时候不知道该定义多少个参数(2)格式:     修饰符  返回值类型  方法名(数据类型… 变量名){   }  注意: 这里的变量其实是一个数 ...

  2. asp.net对word文档进行修改 对于使用word文档做模板编辑比较适用

    最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...

  3. CentOS 5.4下的Memcache安装步骤(Linux+Nginx+PHP+Memcached)

    原文链接:http://www.jb51.net/article/29668.htm

  4. struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解

    http://www.cnblogs.com/langtianya/archive/2013/04/10/3012205.html

  5. 数据库导出导入操作(expdp,impdp)

    EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用. 命令行: sqlplus/nolog connect username/password as sysd ...

  6. 解决NSAttributedString与UILabel高度自适应计算问题

    两个类扩展方法: /** *  修改富文本的颜色 * *  @param str   要改变的string *  @param color 设置颜色 *  @param range 设置颜色的文字范围 ...

  7. Objective-C中的分类与协议

    分类 在谈分类之前,我们可以先探究下,OC中为什么出现分类这种机制,有什么好处? 假设你接到一个大项目:计算两个整数的和,差.接到任务的你马上动手.编写代码如下: #import <Founda ...

  8. 安卓开发之viewpager学习(头条显示)

    activity_luancher.xml代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res ...

  9. cmd 命令行下复制、粘贴的快捷键

    1.单击左下角“开始”菜单,选择“运行”,输入“cmd”. 2.在弹出的cmd窗口的标题栏上点击“右键”,选择“属性”. 3.在弹出的对话框中选择“选项”这个选项卡,在“编辑选项”区域中勾选“快速编辑 ...

  10. php学习小技巧

    1.print_r可打印数组 <?php echo '<p class="ajax">This paragraph was loaded with AJAX.&l ...