TCTCTOE - Editorial

hi,can you find the wrong case in my code?
the answers here are right in my code

Already have, unless you’ve updated your code since :slight_smile:

The same testcase as this one:

1 Like

can anyone tell. me why i am getting WA

#include <iostream>
using namespace std;
int wins(char board[][3],char ch)
{ int res=0;
if(board[0][0]==ch&&board[0][0]==board[0][1]&&board[0][1]==board[0][2])
res++;
if(board[0][0]==ch&&board[0][0]==board[1][0]&&board[1][0]==board[2][0])
res++;
if(board[0][0]==ch&&board[0][0]==board[1][1]&&board[1][1]==board[2][2])
res++;
if(board[0][2]==ch&&board[0][2]==board[1][1]&&board[1][1]==board[2][0])
res++;
if(board[1][0]==ch&&board[1][0]==board[1][1]&&board[1][1]==board[1][2])
res++;
if(board[2][0]==ch&&board[2][0]==board[2][1]&&board[2][1]==board[2][2])
res++;
if(board[0][1]==ch&&board[0][1]==board[1][1]&&board[1][1]==board[2][1])
res++;
if(board[0][2]==ch&&board[0][2]==board[1][2]&&board[1][2]==board[2][2])
res++;
if(res!=0)
return res;
else
return 0;
}
int main()
{
int t;
cin>>t;
while(t--)
{
char mat[3][3];
int x=0,o=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin>>mat[i][j];
if(mat[i][j]=='X')
x++;
else if(mat[i][j]=='O')
o++;
}
}
if(x==o||x==o+1)
{
    int xx=wins(mat,'X');
int oo=wins(mat,'O');

        if(xx>=2||oo>=2)
        cout<<"3"<<endl;
   
        else if(xx&&oo)
        {
            cout<<"3"<<endl;
        }
            
        else if(xx)
        {      if(x==o+1)
            cout<<"1"<<endl;
            else
            cout<<"3"<<endl;
        }
        else if(oo)
        {
            if(x==o)
            cout<<"1"<<endl;
            else
            cout<<"3"<<endl;
        }
        else
        {      if(xx+oo==9)
            cout<<"1"<<endl;
            else
            cout<<"2"<<endl;
        }

    }
    else
    {
        cout<<"3"<<endl;
    }
}
}

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

Should’nt it be 3? As count(‘X’)!=count(‘O’)+1 as first symbol starts with ‘X’

1 Like

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;
}