TCTCTOE - Editorial

1
XXO
OXX
XOO

thanks finally accepted

1 Like

How you build test so correctly amaze

I generated a testsuite of all 3^9 possible boards + answers :wink:

If you make test cases in order of blank spaces , you can neatly prepare a solution without having to consider all edge and special cases . worked out for me tho it was a bit long .

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

I am getting WA in this code . Can any body help.

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;


int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
	

	int t;cin >>t;
	while(t--){

		string s[3];
		for(int i=0;i<3;i++){
			cin >> s[i];
		}
		string s_t[3]={"","",""};
		for(int i=0;i<3;i++){
			s_t[i]+=s[i][0]+s[i][1]+s[i][2];
		}
		int x_cnt=0,o_cnt=0;
		for(auto row : s){
			x_cnt+= count(row.begin(), row.end(), 'X');
			o_cnt+= count(row.begin(), row.end(), 'O');
		}

		bool x_win= false;
		for(string row : s) x_win |= count(row.begin(), row.end(), 'X')==3;
		for(string row : s_t)x_win|= count(row.begin(), row.end(), 'X')==3;

		x_win |= (s[0][0]=='X' && s[1][1]=='X' && s[2][2]=='X');
		x_win |= (s[0][2]=='X' && s[1][1]=='X' && s[2][0]=='X');

		bool o_win= false;
		for(string row : s) o_win |= count(row.begin(), row.end(), 'O')==3;
		for(string row : s_t)o_win|= count(row.begin(), row.end(), 'O')==3;

		o_win |= (s[0][0]=='O' && s[1][1]=='O' && s[2][2]=='O');
		o_win |= (s[0][2]=='O' && s[1][1]=='O' && s[2][0]=='O');
		

		int ans=2;
		if((x_win && o_win)|| !(x_cnt==o_cnt || x_cnt==o_cnt+1) 
				|| (x_win && x_cnt!=o_cnt+1) || (o_win && o_cnt!=x_cnt))
			{
				ans=3;
			}
		else if((x_cnt==o_cnt || x_cnt == o_cnt+1) 
			&& ((x_win+o_win==1) || (x_cnt+o_cnt==9)))
		{
			ans=1;
		}
		cout << ans << "\n";

		
	}

return 0;
	}

I really am not getting this solution is there anyone who can help with their code or some articles/topics I need to learn before solving this problem?

how it will be 3 it must be 1

Show us how a game of Tic-Tac-Toe reaches that state - list the moves :slight_smile:

Got is :sweat_smile: Thank You

1 Like

A cool looking solution that isn’t eyes blinding.

#include <iostream>
#include <vector>
#include <algorithm>


void validateBoard(char** board, int xCount, int oCount, int _Count) {
    std::vector<char> winners;
    for (int i = 0; i < 3; i++) {
        bool success = true;
        char last;
        for (int j = 1; j < 3; j++) {
            if (board[i][j] != board[i][j - 1]) {
                success = false;
            }
            last = board[i][j];
        }
        if (success && last != '_') {
            if (std::find(winners.begin(), winners.end(), last) == winners.end()) {
                winners.emplace_back(last);
            }
        }
    }
    for (int i = 0; i < 3; i++) {
        bool success = true;
        char last;
        for (int j = 1; j < 3; j++) {
            if (board[j][i] != board[j - 1][i]) {
                success = false;
            }
            last = board[j][i];
        }
        if (success && last != '_') {
            if (std::find(winners.begin(), winners.end(), last) == winners.end()) {
                winners.emplace_back(last);
            }
        }
    }
    bool success = true;
    char last;
    for (int i = 1; i < 3; i++) {
        if (board[i][i] != board[i - 1][i - 1]) {
            success = false;
        }
        last = board[i][i];
    }
    if (success && last != '_') {
        if (std::find(winners.begin(), winners.end(), last) == winners.end()) {
            winners.emplace_back(last);
        }
    }
    success = true;
    for (int i = 1; i < 3; i++) {
        if (board[2 - i][i] != board[2 - i + 1][i - 1]) {
            success = false;
        }
        last = board[2 - i][i];
    }
    if (success && last != '_') {
        if (std::find(winners.begin(), winners.end(), last) == winners.end()) {
            winners.emplace_back(last);
        }
    }
    if (!(xCount == oCount || xCount == oCount + 1)) {
        std::cout << "3\n";
        return;
    }
    if (winners.size() > 1) std::cout << "3\n";
    else if (winners.empty()) {
        if (_Count > 0) std::cout << "2\n";
        else std::cout << "1\n";
    }
    else if (winners.size() == 1) {
        if (winners[0] == 'X') {
            if (xCount == oCount + 1) std::cout << "1\n";
            else std::cout << "3\n";
        }
        else {
            if (xCount == oCount) std::cout << "1\n";
            else std::cout << "3\n";
        }
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    char* board[3];
    for (int i = 0; i < 3; i++) board[i] = new char[3];
    int tests;
    std::cin >> tests;
    while (tests--) {
        int x, y; x = y = 3;
        int xCount = 0;
        int oCount = 0;
        int _Count = 0;
        while (x--) {
            while (y--) {
                char state;
                std::cin >> state;
                if (state == 'X') xCount++;
                if (state == 'O') oCount++;
                if (state == '_') _Count++;
                board[2 - x][2 - y] = state;
            }
            y = 3;
        }
        validateBoard(board, xCount, oCount, _Count);
    }
    return 0;
}

can anyone tell me the test case i might be missing
#include
using namespace std;
int main() {
int t;
cin>>t;
while(t–)
{
string l1,l2,l3;
int winx=0,wino=0,available=0,X=0,O=0;
cin>>l1;
cin>>l2;
cin>>l3;
for(int i=0;i<3;i++)
{
if (l1[i]==‘X’)
X++;
else if(l1[i]==‘O’)
O++;

   }
   for(int i=0;i<3;i++)
   {
       if (l2[i]=='X')
            X++;
       else if(l2[i]=='O')
            O++;
       
   }
   for(int i=0;i<3;i++)
   {
       if (l3[i]=='X')
            X++;
       else if(l3[i]=='O')
            O++;
       
   }
   //cout<<l1<<" "<<l2<<" "<<l3<<endl;
   if(l1[0]==l1[1] && l1[1]==l1[2])
   {
       if(l1[0]=='X')
        winx=winx+1;
       else if(l1[0]=='O')
        wino=wino+1;
   }
   if(l2[0]==l2[1] && l2[1]==l2[2])
   {
       if(l2[0]=='X')
        winx=winx+1;
       else if(l2[0]=='O')
        wino=wino+1;
   }
   if(l3[0]==l3[1] && l3[1]==l3[2])
   {
       if(l3[0]=='X')
        winx=winx+1;
       else if(l3[0]=='O')
        wino=wino+1;
   }
   
   if(l1[0]==l2[0] && l2[0]==l3[0])
   {
       if(l3[0]=='X')
        winx=winx+1;
       else if(l3[0]=='O')
        wino=wino+1;
   }
   if(l1[1]==l2[1]  && l2[1]==l3[1])
   {
      if(l3[1]=='X')
        winx=winx+1;
       else if(l3[1]=='O')
        wino=wino+1;
   }
   if(l1[2]==l2[2]  &&l2[2]==l3[2])
   {
      if(l3[2]=='X')
        winx=winx+1;
       else if(l3[2]=='O')
        wino=wino+1;
   }
   if(l1[0]==l2[1]  && l2[1]==l3[2])
   {
       if(l3[2]=='X')
        winx=winx+1;
       else if(l3[2]=='O')
        wino=wino+1;
   }
   if(l1[2]==l2[1]  && l2[1]==l3[0])
   {
       if(l3[0]=='X')
        winx=winx+1;
       else if(l3[0]=='O')
        wino=wino+1;
   }
   if(l1[0]=='_' || l1[1]=='_' || l1[2]=='_' || l2[0]=='_' || l2[1]=='_' || l2[2]=='_' || l3[0]=='_' || l3[1]=='_' || l3[1]=='_')
   {
       available=available+1;
   }

if(X==O || X-1==O)
{
   if(winx<=2 && winx>0 && wino==0)
   {
       if(X-1==O)
        cout<<"1"<<endl;
       else
        cout<<"3"<<endl;
   }
   else if(wino==1 && winx==0)
   {
       if(X==O)
        cout<<"1"<<endl;
       else
        cout<<"3"<<endl;
   }
   else if(winx==0 && wino==0)
   {
       if(available==0)
       {
        if(X-1==O)
            cout<<"1"<<endl;
        else
            cout<<"3"<<endl;
           
       }
       if(available==1)
       {
        if(X-1==O)
            cout<<"2"<<endl;
        else if(X==O)
            cout<<"2"<<endl;
        else
            cout<<"3"<<endl;
           
       }
   }
   else
   {
       cout<<"3"<<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:

https://www.codechef.com/viewsolution/55817608
link to my solution

1 Like

please help me find the test case that i might be missing

Consider the test input:

1
XXO
XXO
OO_

answer should be 2 right

Yep :slight_smile:

cannot believe it was a typo …thanks

1 Like