cf 3C Tic-tac-toe(模拟)

C. Tic-tac-toe

题目链接:Problem - 3C


题目大意:

(井字游戏)6种规则:

first(下一步第一个人走)

second(下一步第二个人走)

illegal(棋盘不可能出现次情况)

the first player won(第一个玩家赢了)

the second player won(第二个玩家赢了)

draw(棋局结束但胜负未分)


题目分析:

多种情况的模拟,一一枚举,注意各种情况的边界。


思路分析:

1)先存图。'X'存1,'0'存0,'.'存10;

2)通过判断棋局上1,0的个数先判是否合法。

3)通过函数判断是否一方获胜(注意一方获胜时棋局也会有不合法情况)。

4)最后判断棋局是否完整,完整则输出draw,不完整判断下一步谁先下。


贴AC代码:

#include <stdio.h> //定义输入/输出函数
#include <limits.h> //定义各种数据类型最值常量
#include <math.h> //定义数学函数
#include <stdlib.h> //定义杂项函数及内存分配函数
#include <string.h> //字符串处理
#include <algorithm>//算法
#include <queue>//队列
#include <stack>//栈
#include <vector>
using namespace std;
int map[3][3];
char input;
int couts1, couts0, couts_1;
int panduan()
{
    int p1, p0;
    p1 = p0 = 0;
    for (int i = 0; i < 3; i ++){
        if (map[i][1] + map[i][2] + map[i][0] == 3) p1 = 1;
        if (map[i][1] + map[i][2] + map[i][0] == 0) p0 = 1;
    }
    //行判断
    for (int i = 0; i < 3; i ++){
        if (map[0][i] + map[1][i] + map[2][i] == 3) p1 = 1;
        if (map[0][i] + map[1][i] + map[2][i] == 0) p0 = 1;
    }
    //列判断
    if (map[0][0] + map[1][1] + map[2][2] == 3) p1 = 1;
    if (map[0][2] + map[1][1] + map[2][0] == 3) p1 = 1;
    if (map[0][0] + map[1][1] + map[2][2] == 0) p0 = 1;
    if (map[0][2] + map[1][1] + map[2][0] == 0) p0 = 1;
    if (p1 == 1 && p0 != 1) return 1;
    else if (p1 != 1 && p0 == 1) return 0;
    else if (p1 == 0 && p0 == 0) return 2;
    else return -1;
}
int main()
{
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            scanf("%c", &input);
            if (input == 'X'){
                map[i][j] = 1;
                couts1++;
            }
            if (input == '.'){
                map[i][j] = 10;
                couts_1++;
            }
            if (input == '0'){
                map[i][j] = 0;
                couts0++;
            }
        }
        getchar();
    }
    if(couts1 - couts0 > 1 || couts0 - couts1 > 0) printf("illegal\n");
    else{
        int flag = panduan();
        if (flag == 1 && couts1 - couts0 == 1) printf("the first player won\n");
        else if (flag == 0 && couts1 == couts0) printf("the second player won\n");
        else if (flag == 2){
            if (couts_1 == 0) printf("draw\n");
            else {
                if (couts1 > couts0 ) printf("second\n");
                else printf("first\n");
            }
        }
        else printf("illegal\n");
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值