Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Wednesday, February 12, 2014

TOE2 - Tic Tac Toe (II)

                                     Question link TOE2 - TIC TAC TOE (II) 

                solution of spoj TOE2 - spoj Tic - Tac - Toe (II)


#include<stdio.h>
char str[10],**temp;

int main()
{
    int i,j,no_X,no_O,flag,win_X,win_O;

    while(1)
    {
        gets(str);  no_X = no_O = flag = win_X = win_O = 0;

        if(str[0]=='e')
            break;

        for(i=0;    i<9;    i++)
        {
            if(str[i] == 'X')
                no_X++;
            else if(str[i] == 'O')
                no_O++;
        }

        if(str[4] != '.')
        {
            if(str[4] == 'O')
            {
                if((str[0] == 'O' && str[8] == 'O') || (str[1] == 'O' && str[7] == 'O') || (str[2] == 'O' && str[6] == 'O') || (str[3] == 'O' && str[5] == 'O'))
                    win_O = 1;
            }
            else
            {
                if((str[0] == 'X' && str[8] == 'X') || (str[1] == 'X' && str[7] == 'X') || (str[2] == 'X' && str[6] == 'X') || (str[3] == 'X' && str[5] == 'X'))
                    win_X = 1;
            }
        }

        if(str[1] != '.')
        {
            if(str[0] == 'O')
            {
                if((str[3] == 'O' && str[6] == 'O') || (str[1] == 'O' && str[2] == 'O'))
                    win_O =1;
            }

            else
            {
                if((str[3] == 'X' && str[6] == 'X') || (str[1] == 'X' && str[2] == 'X'))
                    win_X =1;
            }
        }

        if(str[8] != '.')
        {
            if(str[8] == 'O')
            {
                if((str[5] == 'O' && str[2] == 'O') || (str[7] == 'O' && str[6] == 'O'))
                    win_O = 1;
            }
            else
            {
                if((str[5] == 'X' && str[2] == 'X') || (str[7] == 'X' && str[6] == 'X'))
                    win_X = 1;
            }
        }

        if(win_X == 1 && win_O == 1)
            flag = 1;

        else
        {
            if(win_X == 1 && win_O == 0 && no_X != no_O + 1)
                flag = 1;

            else if(win_X == 0 && win_O == 1 && no_X != no_O)
                flag = 1;

            else if(win_X == 0 && win_O == 0 && (no_X != 5 || no_O != 4))
            {
                flag = 1;
            }
        }


        if(flag)
            printf("invalid\n");
        else
            printf("valid\n");

    }
    return 0;
}


No comments:

Post a Comment

Your comment is valuable to us