CodeForces 3C 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 × 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
 
   
题目大意是在3*3格子里。X先下,0后下,.代表没下棋的格。连成3个就赢了。但是有好多不合法的。
没下满时,下一个给X下就输出 first;下一个给0下就输出 second;X赢输出  the first player won;
0赢就输出  the second player won;平局 draw;不合法 illegal。
其实代码真的简单写,但是要注意不合法的情况,有点坑。
1,棋盘上不能出现X和0同时连成3个;
2,X连成3个时,0不能出现和X一样个数的棋子;
3,0连成3个时,X不能出现比0多一个棋子的情况。
注意这些就很简单。
代码如下:(大一学生,代码比较烂,很复杂)
#include <iostream>
#include <math.h>
#include <stdlib.h>

using namespace std;

int main()
{
    char a[3][3];
    for(int i=0; i<3; i++)
    {
        cin>>a[i];
    }
    int sum[2]={0};
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            if(a[i][j]=='X')
                sum[0]++;
            else if(a[i][j]=='0')
                sum[1]++;
        }
    }
    if(sum[0]-sum[1]>1||sum[0]-sum[1]<0)
    {
        cout<<"illegal"<<endl;
        return 0;
    }
    int f[2]= {0};
    for(int i=0; i<3; i++)
    {
        if(a[i][0]=='X'&&a[i][0]==a[i][1]&&a[i][1]==a[i][2])
            f[0]=1;
        if(a[i][0]=='0'&&a[i][0]==a[i][1]&&a[i][1]==a[i][2])
            f[1]=1;
    }
    for(int i=0; i<3; i++)
    {
        if(a[0][i]=='X'&&a[0][i]==a[1][i]&&a[1][i]==a[2][i])
            f[0]=1;
        if(a[0][i]=='0'&&a[0][i]==a[1][i]&&a[1][i]==a[2][i])
            f[1]=1;
    }
    if(a[0][2]=='X'&&a[0][2]==a[1][1]&&a[1][1]==a[2][0])
        f[0]=1;
    if(a[0][2]=='0'&&a[0][2]==a[1][1]&&a[1][1]==a[2][0])
        f[1]=1;
    if(a[2][2]=='X'&&a[2][2]==a[1][1]&&a[1][1]==a[0][0])
        f[0]=1;
    if(a[2][2]=='0'&&a[2][2]==a[1][1]&&a[1][1]==a[0][0])
        f[1]=1;
    if(f[0]==1&&f[1]==1)
    {
        cout<<"illegal"<<endl;
        return 0;
    }
    else if(f[0]==1&&f[1]!=1&&sum[0]==sum[1])
    {
        cout<<"illegal"<<endl;
        return 0;
    }
    else if(f[0]==1&&f[1]!=1&&sum[0]-sum[1]==1)
    {
        cout<<"the first player won"<<endl;
        return 0;
    }
    else if(f[1]==1&&f[0]!=1&&sum[0]-sum[1]==1)
    {
        cout<<"illegal"<<endl;
        return 0;
    }
    else if(f[1]==1&&f[0]!=1&&sum[0]-sum[1]!=1)
    {
        cout<<"the second player won"<<endl;
        return 0;
    }
    int num=sum[0]+sum[1];
    if(num==9)
    {
        cout<<"draw"<<endl;
        return 0;
    }
    else if(sum[0]==sum[1])
    {
        cout<<"first"<<endl;
        return 0;
    }
    else if(sum[0]-sum[1]==1)
    {
        cout<<"second"<<endl;
        return 0;
    }
    return 0;
}

CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值