0
点赞
收藏
分享

微信扫一扫

Codeforces Beta Round #3 / 3C Tic-tac-toe (超级模拟)



C. Tic-tac-toe



http://codeforces.com/problemset/problem/3/C



time limit per test



memory limit per test



input



output


3 × 3grid (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.

3 × 3

  • illegal
  • the first player won
  • the second player won
  • draw


Input



.", "X" or "0" (a period, a capital letter X, or a digit zero).


Output



first, second, illegal, the first player won, the second player won or draw.


Sample test(s)



input



X0X .0. .X.



output



second



一道我WA了n遍的题目(可能是晚上状态不好)


完整代码:

/*30ms,0KB*/

#include<cstdio>

char grid[3][3];

int main(void)
{
	int fcount = 0, scount = 0;
	bool fwin = false, swin = false; ///局部变量一定要赋值后才能用
	for (int i = 0; i < 3; ++i)
	{
		for (int j = 0; j < 3; j++)
		{
			grid[i][j] = getchar();
			if (grid[i][j] == 'X')
				++fcount;
			else if (grid[i][j] == '0')
				++scount;
		}
		getchar();
	}
	if (fcount - scount > 1 || fcount - scount < 0) ///永远是X(first)先走
		printf("illegal");
	else
	{
		///行
		for (int i = 0; i < 3; ++i)
		{
			if (grid[i][0] == grid[i][1] && grid[i][0] == grid[i][2] && grid[i][0] != '.')
			{
				if (grid[i][0] == 'X')
					fwin = true;
				else
					swin = true;
			}
		}
		if (fwin && swin)
		{
			printf("illegal");
			return 0;
		}
		else if (fwin || swin)
		{
			if (fwin && fcount == scount || swin && fcount > scount)
			{
				printf("illegal");
				return 0;
			}
			printf(fwin ? "the first player won" : "the second player won");
			return 0;
		}
		///列
		for (int i = 0; i < 3; ++i)
		{
			if (grid[0][i] == grid[1][i] && grid[0][i] == grid[2][i] && grid[0][i] != '.')
			{
				if (grid[0][i] == 'X')
					fwin = true;
				else
					swin = true;
			}
		}
		if (fwin && swin)
		{
			printf("illegal");
			return 0;
		}
		else if (fwin || swin)
		{
			if (fwin && fcount == scount || swin && fcount > scount)
			{
				printf("illegal");
				return 0;
			}
			printf(fwin ? "the first player won" : "the second player won");
			return 0;
		}
		///斜
		if (grid[1][1] != '.' && (grid[0][0] == grid[1][1] && grid[2][2] == grid[1][1] || grid[0][2] == grid[1][1] && grid[2][0] == grid[1][1]))
		{
			if (grid[1][1] == 'X')
				fwin = true;
			else
				swin = true;
		}
		if (fwin || swin)
		{
			if (fwin && fcount == scount || swin && fcount > scount)
			{
				printf("illegal");
				return 0;
			}
			printf(fwin ? "the first player won" : "the second player won");
			return 0;
		}
		printf(fcount + scount == 9 ? "draw" : fcount > scount ? "second" : "first");
	}
	return 0;
}




举报

相关推荐

0 条评论