VJ新生题B - Tic Tac Toe

今天继续发一道这几天做的题,题目不是很难,但是思路要很清晰。
Tic Tac Toe is a child’s game played on a 3 by 3 grid. One player, X, starts by placing an X at an unoccupied grid position. Then the other player, O, places an O at an unoccupied grid position. Play alternates between X and O until the grid is filled or one player’s symbols occupy an entire line (vertical, horizontal, or diagonal) in the grid.
We will denote the initial empty Tic Tac Toe grid with nine dots. Whenever X or O plays we fill in an X or an O in the appropriate position. The example below illustrates each grid configuration from the beginning to the end of a game in which X wins.
… X… X.O X.O X.O X.O X.O X.O

… … … … .O. .O. OO. OO.

… … … …X …X X.X X.X XXX

Your job is to read a grid and to determine whether or not it could possibly be part of a valid Tic Tac Toe game. That is, is there a series of plays that can yield this grid somewhere between the start and end of the game?
Input
The first line of input contains N, the number of test cases. 4N-1 lines follow, specifying N grid configurations separated by empty lines.
Output
For each case print “yes” or “no” on a line by itself, indicating whether or not the configuration could be part of a Tic Tac Toe game.
Sample Input
2
X.O
OO.
XXX

O.X
XX.
OOO
Sample Output
yes
no
题目的内容大体是,给你一个3x3的一个字符表格然后来代表井字棋的现状,X,O分别代表两个玩家下的两种棋子而“ . ”代表的是还没有下的空位,然后由你来判断这种情况能不能存在。
(1)依据题目要求和所给例子来看,是X先下,所以说X一定等于O或等于O+1。
(2)如果一个玩家赢了,那么这个玩家下的一定是最后一步棋,所以说X三连时X棋的数量一定是O的数量+1
(3)如果时O三连时O的数量一定等于X的数量。
(4)如果X和O同时出现三连的情况,那么肯定是不成立的。
然后接下来就附上代码,供各位参考

include<stdio.h>
int main()
{
    int a,b,c,d,e,f,g,h,i;//  本人写代码时喜欢先定义很多变量,然后最后写完时不用的再删掉,这样不用边写边定义
    char n[3][3],k;          //                                              (这次懒得删了)
    int j;
    b=0;
    scanf("%d",&a);
    while(b<a){
        j=-1;
        scanf("%c",&k);
        g=0;
        c=0;
        e=0;
        f=0;
        i=0;
        h=0;
        while(c<3){
            d=0;
            while(d<3){
                scanf("%c",&n[c][d]);
                if(n[c][d]=='X'){
                    e=e+1;
                }
                if(n[c][d]=='O'){
                    f=f+1;
                }
                d=d+1;
            }
            scanf("%c",&k);
            c=c+1;
        }
        if(n[0][0]=='X'&&n[0][1]=='X'&&n[0][2]=='X'){//判断X是否三连(我写的比较冗杂,但是能够一眼看懂)。
            h=2;
        }
        if(n[1][0]=='X'&&n[1][1]=='X'&&n[1][2]=='X'){
            h=2;
       }
        if(n[2][0]=='X'&&n[2][1]=='X'&&n[2][2]=='X'){
            h=2;
        }
        if(n[0][0]=='X'&&n[1][0]=='X'&&n[2][0]=='X'){
            h=2;
        }
        if(n[0][1]=='X'&&n[1][1]=='X'&&n[2][1]=='X'){
            h=2;
        }
        if(n[0][2]=='X'&&n[1][2]=='X'&&n[2][2]=='X'){
            h=2;
        }
        if(n[0][0]=='X'&&n[1][1]=='X'&&n[2][2]=='X'){
            h=2;
        }
        if(n[0][2]=='X'&&n[1][1]=='X'&&n[2][0]=='X'){
            h=2;
        }
        if(n[0][0]=='O'&&n[0][1]=='O'&&n[0][2]=='O'){//判断O是否三连。
            i=2;
        }
        if(n[1][0]=='O'&&n[1][1]=='O'&&n[1][2]=='O'){
            i=2;
       }
        if(n[2][0]=='O'&&n[2][1]=='O'&&n[2][2]=='O'){
            i=2;
        }
        if(n[0][0]=='O'&&n[1][0]=='O'&&n[2][0]=='O'){
            i=2;
        }
        if(n[0][1]=='O'&&n[1][1]=='O'&&n[2][1]=='O'){
            i=2;
        }
        if(n[0][2]=='O'&&n[1][2]=='O'&&n[2][2]=='O'){
            i=2;
        }
        if(n[0][0]=='O'&&n[1][1]=='O'&&n[2][2]=='O'){
            i=2;
        }
        if(n[0][2]=='O'&&n[1][1]=='O'&&n[2][0]=='O'){
            i=2;
       }
        if(h==2&&i!=2&&e==f){//条件一
            j=0;
        }
        else if(h!=2&&i==2&&e!=f){//条件二
            j=0;
        }
        else if(h==2&&i==2){//条件三
            j=0;
        }
        else if(e>=f+2||e<f){//条件四
            j=0;
        }
        else if(e==0&&f==0){//这是我写的时候脑袋热加上去的,不加应该也没事(我没试过)
            j=1;
        }
        else{
            j=1;
        }
        if(j==1){
            printf("yes\n");
        }
        if(j==0){
            printf("no\n");
        }
        b=b+1;
    }
    b=0;
    return 0;
}

之后的题会依次放出来,每天的题不用多,两三道即可,有的时候一天一道做不出来也没办法。
还有就是初用VJ实在有点不舒服,过几天会写一点自己用VJ的出错总结,方便大家借鉴。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值