其他分享
首页 > 其他分享> > 【CF3C Tic-tac-toe】题解

【CF3C Tic-tac-toe】题解

作者:互联网

题目链接

题目

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:

当然,大家都熟悉井字棋游戏。 这些规则确实很简单。 两个玩家A、B轮流在3x3的方格的单元格中画符号(玩家A总是画“X”,玩家B总是画“0”)。 首先在水平、垂直或对角线上出现3个相同的符号的玩家获胜,比赛结束。 玩家A总是为先手。 如果3x3的方格被填满,但是在所有水平、垂直或对角线上均未出现3个相同的符号,宣布平局。

给你一个3*3的方格,每个单元格是“.”(空白),“X”或“0”。 你必须输出以下内容之一:

玩家A走棋(输出“first”)——如果出现给定的状态后,下一步为玩家A走棋。

玩家B走棋(输出“second”)——如果出现给定的状态后,下一步为玩家B走棋。

非法(输出“illegal”)——如果给定的状态无法在正常游戏过程中出现。

玩家A胜利(输出“the first player won”)——如果给定的状态中玩家A胜利。

玩家B胜利(输出“the second player won”)——如果给定的状态中玩家B胜利。

思路

一道语法题。

这道题难就不难,但很细节。

我的思路如此:

  1. 如果先手和后手棋子数不等或不是相差1,判无解。
  2. 接下来判断先手与后手是否赢了。
  3. 如果先手赢,但后手还在下,无解。
  4. 如果后手赢,但先手还在下,无解。
  5. 如果棋盘被填满,平局
  6. 如果先手数量大于后手,轮到后手
  7. 如果先手数量等于先手,轮到先手

考虑好这些题目就不难了。
平局(输出“draw”)——如果给定的状态是平局。

总结

对于此类题,明显要考验码力的,在oi赛制中,要考虑清楚每一种情况,自己手动出几组数据,同时更重要的是理解清题目。

做之前,最好先写一写优先级。

Code


// Problem: CF3C Tic-tac-toe
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF3C
// Memory Limit: 62 MB
// Time Limit: 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
//#define N
int n, m, i, j, k; 
char s[5][5]; 

signed main()
{
//	freopen("tiaoshi.in", "r", stdin); 
//	freopen("tiaoshi.out", "w", stdout); 
	scanf("%s%s%s", s[1]+1, s[2]+1, s[3]+1); 
	for(i=1; i<=3; ++i) for(j=1; j<=3; ++j) if(s[i][j]!='.')
		if(s[i][j]=='X') ++n; else ++m; 
	if(n-m!=0&&n-m!=1) return printf("illegal "), 0; 
	if(s[1][1]==s[1][2]&&s[1][2]==s[1][3]&&s[1][1]!='.')  s[1][1]=='X' ? i=1 : j=1; 
	if(s[2][1]==s[2][2]&&s[2][2]==s[2][3]&&s[2][1]!='.')  s[2][1]=='X' ? i=1 : j=1; 
	if(s[3][1]==s[3][2]&&s[3][2]==s[3][3]&&s[3][1]!='.')  s[3][1]=='X' ? i=1 : j=1; 
	if(s[1][1]==s[2][1]&&s[2][1]==s[3][1]&&s[1][1]!='.')  s[1][1]=='X' ? i=1 : j=1; 
	if(s[1][2]==s[2][2]&&s[2][2]==s[3][2]&&s[1][2]!='.')  s[1][2]=='X' ? i=1 : j=1; 
	if(s[1][3]==s[2][3]&&s[2][3]==s[3][3]&&s[1][3]!='.')  s[1][3]=='X' ? i=1 : j=1; 
	if(s[1][1]==s[2][2]&&s[2][2]==s[3][3]&&s[1][1]!='.')  s[1][1]=='X' ? i=1 : j=1; 
	if(s[1][3]==s[2][2]&&s[2][2]==s[3][1]&&s[1][3]!='.')  s[1][3]=='X' ? i=1 : j=1; 
	if(n>m&&j) return printf("illegal"), 0; 
	if(n==m&&i) return printf("illegal"), 0; 
	if(i|j) return printf("%s", i ? "the first player won" : "the second player won"), 0;
	if(n+m==9) return printf("draw"), 0; 
	printf("%s", n==m ? "first" : "second"); 
	return 0; 
}

标签:return,题解,玩家,player,second,won,tac,toe,first
来源: https://www.cnblogs.com/zhangtingxi/p/15724793.html