Codeforces Ilya and tic-tac-toe game

/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.

*/

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back

typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef unsigned long long ull;

const int mod = 1000007;
const int high = 4;

char ch[5][5];

bool horizontal()
{
    int i , j;

    bool decision=false;

    for(i=0; i<high; i++)
    {
        for(j=0; j<high; j++)
        {
            if(ch[i][j] == 'x')
            {
                if(ch[i][j+1]=='x' and ch[i][j+2]=='.')
                {
                    decision = true;
                    break;
                }

                else if(ch[i][j+1]=='.' and ch[i][j+2]=='x')
                {
                    decision = true;
                    break;
                }
            }

            else if(ch[i][j] == '.')
            {
                if(ch[i][j+1]=='x' and ch[i][j+2]=='x')
                {
                    decision = true;
                    break;
                }
            }
        }

        if(decision) break;
    }

    if(decision) return true;
    else return false;
}

bool vertical()
{
    int i , j;

    bool decision = false;

    for(j=0; j<high; j++)
    {
        for(i=0; i<high; i++)
        {
            if(ch[i][j] == 'x')
            {
                if(ch[i+1][j] == 'x' and ch[i+2][j] == '.')
                {
                    decision = true;
                    break;
                }

                else if(ch[i+1][j] == '.' and ch[i+2][j] == 'x')
                {
                    decision = true;
                    break;
                }
            }

            else if(ch[i][j] == '.')
            {
                if(ch[i+1][j]=='x' and ch[i+2][j] == 'x')
                {
                    decision = true;
                    break;
                }
            }
        }

        if(decision) break;
    }

    if(decision) return true;
    else return false;
}

bool diagonal_one()
{
    int i , j;

    bool decision = false;

    for(i=0 , j=2; i<3 and j>=0; i++ , j--)
    {
        if(ch[i][j] == 'x')
        {
            if(ch[i+1][j-1]=='x' and ch[i+2][j-2]=='.')
            {
                decision = true;
                break;
            }

            else if(ch[i+1][j-1]=='.' and ch[i+2][j-2]=='x')
            {
                decision = true;
                break;
            }
        }

        //else if(ch[i][j] == 'o') x=0;

        else if(ch[i][j] == '.')
        {
            if(ch[i+1][j-1]=='x' and ch[i+2][j-2]=='x')
            {
                decision = true;
                break;
            }
        }
    }

    if(decision) return true;

    else
    {
        decision = false;

        for(i=1 , j=high-1; i<4 and j>0; i++, j--)
        {
            if(ch[i][j] == 'x')
            {
                if(ch[i+1][j-1]=='x' and ch[i+2][j-2]=='.')
                {
                    decision = true;
                    break;
                }

                else if(ch[i+1][j-1]=='.' and ch[i+2][j-2]=='x')
                {
                    decision = true;
                    break;
                }
            }


            //else if(ch[i][j] == 'o') x=0;
            else if(ch[i][j] == '.')
            {
                if(ch[i+1][j-1]=='x' and ch[i+2][j-2]=='x')
                {
                    decision = true;
                    break;
                }
            }
        }

    if(decision) return true;

    else
    {
        for(i=0 , j=high-1; i<high and j>=0; i++, j--)
        {
            if(ch[i][j] == 'x')
            {
                if(ch[i+1][j-1]=='x' and ch[i+2][j-2]=='.')
                {
                    decision = true;
                    break;
                }

                else if(ch[i+1][j-1]=='.' and ch[i+2][j-2]=='x')
                {
                    decision = true;
                    break;
                }
            }
            //else if(ch[i][j] == 'o') x=0;
            else if(ch[i][j] == '.')
            {
                if(ch[i+1][j-1]=='x' and ch[i+2][j-2]=='x')
                {
                    decision = true;
                    break;
                }
            }
        }

        if(decision) return true;
        else return false;
    }
  }
}

bool diagonal_two()
{
    int i , j;
    bool decision = false;
    //cout << "jjjjj";

    for(i=1 , j=0; i<4 and j<3; i++, j++)
    {

        if(ch[i][j] == 'x')
        {
            if(ch[i+1][j+1]=='x' and ch[i+2][j+2]=='.')
            {
                decision = true;
                break;
            }

            else if(ch[i+1][j+1]=='.' and ch[i+2][j+2]=='x')
            {
                decision = true;
                break;
            }
        }

            //else if(ch[i][j] == 'o') x=0;
            else if(ch[i][j] == '.')
            {
                if(ch[i+1][j+1]=='x' and ch[i+2][j+2]=='x')
                {
                    decision = true;
                    break;
                }
            }
    }

    if(decision)
    {
        //outn(1);
        return true;
    }
    else
    {
        decision = false;

        for(i=0, j=0; i<high and j<high; i++, j++)
        {
            if(ch[i][j] == 'x')
            {
                if(ch[i+1][j+1]=='x' and ch[i+2][j+2]=='.')
                {
                    decision = true;
                    break;
                }

                else if(ch[i+1][j+1]=='.' and ch[i+2][j+2]=='x')
                {
                    decision = true;
                    break;
                }
            }
            //else if(ch[i][j] == 'o') x=0;
            else if(ch[i][j] == '.')
            {
                if(ch[i+1][j+1]=='x' and ch[i+2][j+2]=='x')
                {
                    decision = true;
                    break;
                }
            }
        }

        if(decision)
        {
            //outn(2);
                return true;
        }
        else
        {
            decision = false;

            for(i=0, j=1; i<3 and j<high; i++, j++)
            {

                if(ch[i][j] == 'x')
                {
                    if(ch[i+1][j+1]=='x' and ch[i+2][j+2]=='.')
                    {
                        decision = true;
                        break;
                    }

                    else if(ch[i+1][j+1]=='.' and ch[i+2][j+2]=='x')
                    {
                        decision = true;
                        break;
                    }
                }

            //else if(ch[i][j] == 'o') x=0;
                else if(ch[i][j] == '.')
                {
                    if(ch[i+1][j+1]=='x' and ch[i+2][j+2]=='x')
                    {
                        decision = true;
                        break;
                    }
                }

            }

            if(decision)
            {
                //outn(3);
                return true;
            }

            else return false;
        }
    }

    //cout << "hfff";
}

int main()
{
    fast;
    int i , j;
    bool decision=false;

    for(i=0; i<high; i++)
    {
        for(j=0; j<high; j++)
        {
            cin >> ch[i][j];
        }
    }

//    bool t = diagonal_two();
//    cout << t << " ";

    if(horizontal()) outn("YES");
    else if(vertical()) outn("YES");
    else if(diagonal_one()) outn("YES");
    else if(diagonal_two()) outn("YES");
    else outn("NO");

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number