TCTCTOE - Editorial

The answer to

1
__X
_XO
XOO

is 3, yes :slight_smile:

please check it where i am wrong

That still doesn’t compile.

I’m going to assume it’s this submission, in which case it gets the wrong answer for the testcase directly above your last post.

I am getting the same outputs but still compiler is still telling WA.

Post a link to your submission :slight_smile:

Can you counter any test case which fails to pass in this?
https://www.codechef.com/viewsolution/46669468

1
__X
__X
OOX

Thanks man,it was a typo in my code i was struggling with this very badly now worked!

1 Like

can anyone tell me which case I am missing

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int t;

    cin >> t;

    while (t--)

    {

        int nu = 0, nx = 0, no = 0;

        char a[3][3];

        for (int i = 0; i < 3; i++)

        {

            for (int j = 0; j < 3; j++)

            {

                cin >> a[i][j];

                if (a[i][j] == '_')

                    nu++;

                if (a[i][j] == 'X')

                    nx++;

                if (a[i][j] == 'O')

                    no++;

            }

        }

        int xwin = 0, owin = 0;

        if ((a[0][0] == a[0][1]) && (a[0][0] == a[0][2]))

        {

            if (a[0][0] == 'O')

            {

                owin++;

            }

            else if (a[0][0] == 'X')

            {

                xwin++;

            }

        }

        if ((a[1][0] == a[1][1]) && (a[1][0] == a[1][2]))

        {

            if (a[1][0] == 'O')

            {

                owin++;

            }

            else if (a[1][0] == 'X')

            {

                xwin++;

            }

        }

        if ((a[2][0] == a[2][1]) && (a[2][0] == a[2][2]))

        {

            if (a[2][0] == 'O')

            {

                owin++;

            }

            else if (a[2][0] == 'X')

            {

                xwin++;

            }

        }

        if ((a[0][0] == a[1][0]) && (a[0][0] == a[2][0]))

        {

            if (a[0][0] == 'O')

            {

                owin++;

            }

            else if (a[0][0] == 'X')

            {

                xwin++;

            }

        }

        if ((a[0][1] == a[1][1]) && (a[0][1] == a[2][1]))

        {

            if (a[0][1] == 'O')

            {

                owin++;

            }

            else if (a[0][1] == 'X')

            {

                xwin++;

            }

        }

        if ((a[0][2] == a[1][2]) && (a[0][2] == a[2][2]))

        {

            if (a[0][2] == 'O')

            {

                owin++;

            }

            else if (a[0][2] == 'X')

            {

                xwin++;

            }

        }

        if ((a[0][0] == a[1][1]) && (a[0][0] == a[2][2]))

        {

            if (a[0][0] == 'O')

            {

                owin++;

            }

            else if (a[0][0] == 'X')

            {

                xwin++;

            }

        }

        if ((a[2][0] == a[1][1]) && (a[2][0] == a[0][2]))

        {

            if (a[2][0] == 'O')

            {

                owin++;

            }

            else if (a[2][0] == 'X')

            {

                xwin++;

            }

        }

        if (nu == 0)

        {

            if (nx == 5 && no == 4)

                cout << "1" << endl;

            else

                cout << "3" << endl;

        }

        else if (nu != 0)

        {

            if ((xwin == 1 && owin == 0) && (nx - no == 1))

                cout << "1" << endl;

            else if ((xwin == 0 && owin == 1) && (nx == no))

                cout << "1" << endl;

            else if ((xwin == 0 && owin == 0) && (nx - no == 1 || nx - no == 0))

                cout << "2" << endl;

            else

                cout << "3" << endl;

        }

        

    }

    return 0;

}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

ok

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int t;

    cin >> t;

    while (t--)

    {

        int nu = 0, nx = 0, no = 0;

        char a[3][3];

        for (int i = 0; i < 3; i++)

        {

            for (int j = 0; j < 3; j++)

            {

                cin >> a[i][j];

                if (a[i][j] == '_')

                    nu++;

                if (a[i][j] == 'X')

                    nx++;

                if (a[i][j] == 'O')

                    no++;

            }

        }

        int xwin = 0, owin = 0;

        if ((a[0][0] == a[0][1]) && (a[0][0] == a[0][2]))

        {

            if (a[0][0] == 'O')

            {

                owin++;

            }

            else if (a[0][0] == 'X')

            {

                xwin++;

            }

        }

        if ((a[1][0] == a[1][1]) && (a[1][0] == a[1][2]))

        {

            if (a[1][0] == 'O')

            {

                owin++;

            }

            else if (a[1][0] == 'X')

            {

                xwin++;

            }

        }

        if ((a[2][0] == a[2][1]) && (a[2][0] == a[2][2]))

        {

            if (a[2][0] == 'O')

            {

                owin++;

            }

            else if (a[2][0] == 'X')

            {

                xwin++;

            }

        }

        if ((a[0][0] == a[1][0]) && (a[0][0] == a[2][0]))

        {

            if (a[0][0] == 'O')

            {

                owin++;

            }

            else if (a[0][0] == 'X')

            {

                xwin++;

            }

        }

        if ((a[0][1] == a[1][1]) && (a[0][1] == a[2][1]))

        {

            if (a[0][1] == 'O')

            {

                owin++;

            }

            else if (a[0][1] == 'X')

            {

                xwin++;

            }

        }

        if ((a[0][2] == a[1][2]) && (a[0][2] == a[2][2]))

        {

            if (a[0][2] == 'O')

            {

                owin++;

            }

            else if (a[0][2] == 'X')

            {

                xwin++;

            }

        }

        if ((a[0][0] == a[1][1]) && (a[0][0] == a[2][2]))

        {

            if (a[0][0] == 'O')

            {

                owin++;

            }

            else if (a[0][0] == 'X')

            {

                xwin++;

            }

        }

        if ((a[2][0] == a[1][1]) && (a[2][0] == a[0][2]))

        {

            if (a[2][0] == 'O')

            {

                owin++;

            }

            else if (a[2][0] == 'X')

            {

                xwin++;

            }

        }

        if (nu == 0)

        {

            if (nx == 5 && no == 4)

                cout << "1" << endl;

            else

                cout << "3" << endl;

        }

        else if (nu != 0)

        {

            if ((xwin == 1 && owin == 0) && (nx - no == 1))

                cout << "1" << endl;

            else if ((xwin == 0 && owin == 1) && (nx == no))

                cout << "1" << endl;

            else if ((xwin == 0 && owin == 0) && (nx - no == 1 || nx - no == 0))

                cout << "2" << endl;

            else

                cout << "3" << endl;

        }

        

    }

    return 0;

} 

Thanks :slight_smile:

Consider the test input:

1
XXX
XXO
OOO

Thank you so much :grin:

1 Like

Can some one help me resolving the bug? I am getting WA
#include <bits/stdc++.h>
using namespace std;

bool check_win(vector<vector<char>>& v, char ch)
{
    for(int i=0;i<v.size();i++)
    {
        bool found = true;
        for(int j=0;j<3;j++)
        {
            if(v[i][j]!=ch)
            {
                found = false;
                break;
            }
        }
        if(found)
        {
            return true;
        }
    }
    for(int i=0;i<v.size();i++)
    {
        bool found = true;
        for(int j=0;j<3;j++)
        {
            if(v[j][i]!=ch)
            {
                found = false;
                break;
            }
        }
        if(found)
        {
            return true;
        }
    }
    if(v[0][0] == ch and v[1][1] == ch and v[2][2] == ch) return true;
    if(v[0][2] == ch and v[1][1] == ch and v[2][0] == ch) return true;
    
    return false;
}

int solve(string a, string b, string c)
{
    vector<vector<char>> v;
    v.push_back(vector<char>(a.begin(),a.end()));
    v.push_back(vector<char>(b.begin(),b.end()));
    v.push_back( vector<char>(c.begin(),c.end()));
    int x=0, o=0;
    for(auto p:v)
    {
        for(auto q:p)
        {
            if(q == 'X') x++;
            if(q == 'O') o++;
        }
    }
    if(x-1 == o || x == o)
    {
        bool x_win = false, o_win = false;
        x_win = check_win(v,'X');
        o_win = check_win(v,'O');
        if (x_win and o_win) return 3;
        
        if (x_win or o_win) return 1;
        if(x==5 and o==4) return 1;
        return 2;
        
    }
    else
    {
        return 3;
    }
}

int main() {
	// your code goes here
	int t;
	cin>>t;
    while(t--)
    {
    	string a,b,c;
    	cin>>a>>b>>c;
        cout<<solve(a,b,c)<<endl;
    	
    }
	return 0;
}

Never mind. Got the case where it is failing.
XO_
XO_
X_O

Thanks @ssjgz. Got it.

1 Like

getting WA
1
__X
_XO
XOO
i am getting 3 for this even

Post a link to your submission.

https://www.codechef.com/viewsolution/46677410