题目大意:
两个选手轮流在 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 ;
}