C. Tic-tac-toe【模拟】

C. Tic-tac-toe
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

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: firstsecondillegalthe first player wonthe second player won or draw.

Examples
input
X0X
.0.
.X.
output
second


一道小小的模拟题,错了不知道多少遍,自己思维不够缜密,一直有漏洞,情况比较多,需要逐一进行判断


/*
http://blog.csdn.net/liuke19950717
*/
#include
#include
using namespace std;
char m[5][5];
int judge(int x)
{
	if(m[0][0]==x&&m[0][0]==m[1][1]&&m[1][1]==m[2][2])
	{
		return 1;
	}
	for(int i=0;i<3;++i)
	{
		if(m[0][i]==x&&m[0][i]==m[1][i]&&m[1][i]==m[2][i])
		{
			return 1;
		}
		if(m[i][0]==x&&m[i][0]==m[i][1]&&m[i][1]==m[i][2])
		{
			return 1;
		}
	}
	if(m[0][2]==x&&m[0][2]==m[1][1]&&m[1][1]==m[2][0])
	{
		return 1;
	}
	return 0;
}
void slove()
{
	int a=0,b=0;
	for(int i=0;i<3;++i)
	{
		for(int j=0;j<3;++j)
		{
			if(m[i][j]=='X')
			{
				++a;
			} 
			if(m[i][j]=='0')
			{
				++b;
			}
		}
	}
	if(a>b+1||b>a)//最多多出一个 
	{
		printf("illegal\n");
		return;
	}
	int ka=judge('X'),kb=judge('0');//是否完成
	if(a==b+1&&ka&&!kb)//先下的完成,后下的没完成 
	{
		printf("the first player won\n");
		return;
	}
	if(a==b&&!ka&&kb)//后下的完成,先下的未完成 
	{
		printf("the second player won\n");
		return;
	}
	if(!ka&&!kb)//都没完成,继续走 
	{
		if(a+b==9)
		{
			printf("draw\n");
			return;
		}
		if(a==b)
		{
			printf("first\n");
			return;
		}
		if(a==b+1)
		{
			printf("second\n");
			return;
		}
	}
	printf("illegal\n");//其他情况非法 
}
int main()
{
	scanf("%s%s%s",m[0],m[1],m[2]);
	slove(); 
	return 0;
}


你可能感兴趣的:(C. Tic-tac-toe【模拟】)