# TCTCTOE - Editorial

but the X is winning two time 1 col and diagonal

I know Try and figure out how that state is reachable.

yeah i got that thankyou

1 Like
``````#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) or (x_win and !(x==o+1)) or (o_win and  !(x==o))) 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;
}
``````

Modified solution

https://www.codechef.com/viewsolution/46677665
i correct that condition even now i am getting WA

``````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

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

Got is 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!