getting WA
1
__X
_XO
XOO
i am getting 3 for this even
Post a link to your submission.
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
1
XXO
OXX
XOO
thanks finally accepted
1 Like
How you build test so correctly amaze
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 .
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