HackerEarth: Batman and Tick-tack-toe

This is a basic implementaation problem but need to think critically and I used editorial + previous solved code to understand the logic

code:

#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        char s[4][4];
        int i,j;
        bool ans=false;
        for(i=0; i<4; i++)
        {
            for(j=0; j<4; j++)
            {
                cin>>s[i][j];
            }
        }
        //for column match
        for(i=0;i<2;i++)
        {
            for(j=0;j<4;j++)
            {
                if(s[i][j]=='x'&&s[i+1][j]=='x'&&s[i+2][j]=='.')
                    ans=true;
                if(s[i][j]=='x'&&s[i+1][j]=='.'&&s[i+2][j]=='x')
                    ans=true;
                if(s[i][j]=='.'&&s[i+1][j]=='x'&&s[i+2][j]=='x')
                    ans=true;
            }
        }
        //for row match
        for(i=0;i<4;i++)
        {
            for(j=0;j<2;j++)
            {
                if(s[i][j+1]=='x'&&s[i][j]=='x'&&s[i][j+2]=='.')
                    ans=true;
                if(s[i][j+1]=='x'&&s[i][j]=='.'&&s[i][j+2]=='x')
                    ans=true;
                if(s[i][j+1]=='.'&&s[i][j]=='x'&&s[i][j+2]=='x')
                    ans=true;
            }
        }
        //for diagonal match
        for(i=0;i<2;i++){
            for(j=0;j<2;j++){
                if(s[i][j]=='x'&&s[i+1][j+1]=='x'&&s[i+2][j+2]=='.')
                    ans=true;
                if(s[i][j]=='x'&&s[i+1][j+1]=='.'&&s[i+2][j+2]=='x')
                    ans=true;
                if(s[i][j]=='.'&&s[i+1][j+1]=='x'&&s[i+2][j+2]=='x')
                    ans=true;
            }
        }
        //reverse diagonal match
    for(i=0;i<2;i++)
    {
        for(j=2;j<4;j++)
        {
            if(s[i][j]=='x' && s[i+1][j-1]=='x'&&s[i+2][j-2]=='.')
                ans=true;
            if(s[i][j]=='x' && s[i+1][j-1]=='.'&&s[i+2][j-2]=='x')
                ans=true;
            if(s[i][j]=='.'&&s[i+1][j-1]=='x'&&s[i+2][j-2]=='x')
                ans=true;
        }
    }
    if(ans)
       cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    }
    return 0;
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *