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

本文介绍了一个井字游戏(Tic-tac-toe)胜负判断的算法实现,通过分析游戏盘面的状态来判断游戏的胜负情况或是平局,并确定下一步由哪位玩家行动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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.

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值