10363 - Tic Tac Toe

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Sun Jan 14, 2007 10:56 am

No problem. Can you remove your code?

vijay03
New poster
Posts: 33
Joined: Wed Sep 13, 2006 6:46 pm
Contact:

Post by vijay03 » Sun Jan 14, 2007 12:10 pm

Sorry, i forgot to do it this morning. Done now.

shaqira
New poster
Posts: 5
Joined: Tue Dec 05, 2006 10:16 am
Location: dhaka,bangladesh
Contact:

10363-wa- help anyone!!

Post by shaqira » Tue Jan 16, 2007 10:41 pm

i have solved this problem taking help from the VIJAY's code .but im gettin WA,even though my code is giving correct output. i have also checked my code with the test inpu/outout [Jemerson] and my code is givin correct output.plz some one help me find out the wrong part.here is my code:
Last edited by shaqira on Fri Jan 19, 2007 12:27 am, edited 1 time in total.
[ only a beginner.......]

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf » Tue Jan 16, 2007 11:22 pm

First off, 'main' must return 'int'. Just say no to compilers who think otherwise.

Test your code with this input:

Code: Select all

3
XXX
.X.
OOO

XXX
.X.
OOO

X.O
...
O.X
It outputs 'yesno' to the first case, and a blank line to the last one (when it's fed to it separately); and apparantly can't handle multiple cases (1st and 2nd cases are identical, and so should be the output of your program for them.)

shaqira
New poster
Posts: 5
Joined: Tue Dec 05, 2006 10:16 am
Location: dhaka,bangladesh
Contact:

10363 still wa!

Post by shaqira » Fri Jan 19, 2007 12:26 am

thnx mf, for givin me suggestion.now i hav changed my code but its still
givin wa :( .am i missin anythin?plz help
here is my modified code:



#include<stdio.h>
#include<string>

int main()
{

char board[3][3];
int test,i,r,c,co,cx,len;
bool xwin,owin;

scanf("%d\n",&test);

while(test--)
{

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

{
gets(board);
len=strlen(board[0]);

if(len==0)
{
i=-1;
continue;
}

}



cx=co=0;
owin=xwin=0;

for(r=0;r<3;r++)
for(c=0;c<3;c++)
{
if(board[r][c]=='X')
cx++;
if(board[r][c]=='O')
co++;
}

if((board[0][0]=='O')&&(board[0][1]=='O')&&(board[0][2]=='O'))
owin=1;
if((board[1][0]=='O')&&(board[1][1]=='O')&&(board[1][2]=='O'))
owin=1;
if((board[2][0]=='O')&&(board[2][1]=='O')&&(board[2][2]=='O'))
owin=1;
if((board[0][0]=='O')&&(board[1][0]=='O')&&(board[2][0]=='O'))
owin=1;
if((board[0][1]=='O')&&(board[1][1]=='O')&&(board[2][1]=='O'))
owin=1;
if((board[0][2]=='O')&&(board[1][2]=='O')&&(board[2][2]=='O'))
owin=1;
if((board[0][0]=='O')&&(board[1][1]=='O')&&(board[2][2]=='O'))
owin=1;
if((board[2][0]=='O')&&(board[1][1]=='O')&&(board[0][2]=='O'))
owin=1;

if((board[0][0]=='X')&&(board[0][1]=='X')&&(board[0][2]=='X'))
xwin=1;
if((board[1][0]=='X')&&(board[1][1]=='X')&&(board[1][2]=='X'))
xwin=1;
if((board[2][0]=='X')&&(board[2][1]=='X')&&(board[2][2]=='X'))
xwin=1;
if((board[0][0]=='X')&&(board[1][0]=='X')&&(board[2][0]=='X'))
xwin=1;
if((board[0][1]=='X')&&(board[1][1]=='X')&&(board[2][1]=='X'))
xwin=1;
if((board[0][2]=='X')&&(board[1][2]=='X')&&(board[2][2]=='X'))
xwin=1;
if((board[0][0]=='X')&&(board[1][1]=='X')&&(board[2][2]=='X'))
xwin=1;
if((board[2][0]=='X')&&(board[1][1]=='X')&&(board[0][2]=='X'))
xwin=1;

if(co>cx)
printf("no");

else if(co==cx)
{

if(xwin==1 && owin==1)
printf("no");

else if(xwin==1 && owin==0)
printf("no");

else if(xwin==0 && owin==1)
printf("yes");
else
printf("no");

}


else if(cx==co+1)
{
if(xwin==1 && owin==0)
printf("yes");

else
printf("no");

}


else
printf("no");

printf("\n");

}

return 1;
}
[ only a beginner.......]

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf » Fri Jan 19, 2007 1:13 am

am i missin anythin?
Two g's in this question. ;)

Play a tic-tac-tow with yourself, write down all the configurations you've got (or take a look at the sample game, shown in the problem statement), and test your program with them. It should output 'yes' to each of them.

vijay03
New poster
Posts: 33
Joined: Wed Sep 13, 2006 6:46 pm
Contact:

Re: 10363 still wa!

Post by vijay03 » Fri Jan 19, 2007 11:43 am

shaqira wrote:thnx mf, for givin me suggestion.now i hav changed my code but its still
givin wa :( .am i missin anythin?plz help
here is my modified code:


return 1;
}
Why are you returning a 1 in your code?

shaqira
New poster
Posts: 5
Joined: Tue Dec 05, 2006 10:16 am
Location: dhaka,bangladesh
Contact:

10363

Post by shaqira » Fri Jan 19, 2007 10:38 pm

bcoz main() itself is a function and every function got a prototype and a return type. i have written 'int main()' which means main is a function which returns any integer at the end of the function.so u can return any
int at the end of ur code.as u can also write 'return 2' or 'return 0' or 'return 1000' .

n a special thnx to 'mf' for helpin me to find out wat i missed[ 2 g indeed]

:wink:
[ only a beginner.......]

shaqira
New poster
Posts: 5
Joined: Tue Dec 05, 2006 10:16 am
Location: dhaka,bangladesh
Contact:

thnx mf

Post by shaqira » Fri Jan 19, 2007 11:21 pm

thnx mf, atlast my code got accepted. :D

how come i missed the simplest input... :oops:
thnx again [ u must b wonderin y im thankin u so much ..coz u r da first
person to reply my post :D , i didnt get any reply for my previous 2 posts :cry: ]
[ only a beginner.......]

vijay03
New poster
Posts: 33
Joined: Wed Sep 13, 2006 6:46 pm
Contact:

Post by vijay03 » Sat Jan 20, 2007 9:06 am

I thought unless u returned a zero, the judge assumed your program was not running properly. I didnt know we could even return 1000 if we wanted!

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf » Sat Jan 20, 2007 11:45 am

vijay03 wrote:I thought unless u returned a zero, the judge assumed your program was not running properly. I didnt know we could even return 1000 if we wanted!
UVa's judge doesn't care about main()'s return value. But some other judges (like, spoj.pl), do and expect main to return 0.

Edit: this is no longer true, new UVa's judge expects a zero exit code :)

Ghaverves
New poster
Posts: 4
Joined: Mon Dec 13, 2010 2:49 pm

Re: 10363 - Tic Tac Toe

Post by Ghaverves » Mon Dec 27, 2010 4:11 pm

Can't seem to get the logic right.

If O wins,
number of X == number of O and X does not win.

If X wins,
number of X == number of O + 1 and O does not win.

Otherwise,
as long as number of X >= number of O, it is correct. <<--- this is wrong

Code: Select all

removed after ac...

khobis
New poster
Posts: 1
Joined: Fri Jan 07, 2011 2:53 pm

10363 - Tic Tac Toe

Post by khobis » Mon Jan 10, 2011 7:46 pm

Can anyone please help me with my code ?
M getting W/A :(
m i missing any important part?
thanks in advance

Code: Select all

#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string.h>
using namespace std;
#define fo(i,start,end) for(int i=start;i<end;i++)
#define szv(vector) vector.size()
#define sz(string) string.size();
#define rv(string) reverse(string.begin(),string.end())
typedef map<char,int> mp;
mp v_wins(char a[3][3])
{
    map <char,int> c;
    map <char,int> rslt;
    c['X']=0;
    c['O']=0;
    rslt['X']=0;
    rslt['O']=0;
    int i,j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<2;j++)
        {
            if(a[i][j]==a[i][j+1]) {c[a[i][j]]++;}
            else c[a[i][j]]=0;
        }
        if(c[a[i][j]]==2&&rslt[a[i][j]]!=1) {rslt[a[i][j]]=1;c[a[i][j]]=0;}
    }
    return rslt;
}
mp wins(char a[3][3])
{
    map <char,int> count;
    map <char,int> result;
    int i,j;

    //   else
       {
           count['X']=0;
            count['O']=0;
            result['X']=0;
            result['O']=0;

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

               for(i=0;i<2;i++)
               {
                   if(a[i][j]==a[i+1][j]) {count[a[i][j]]++;}
                else{count[a[i][j]]=0;}


               }

               if((count[a[i][j]]==2)&&(result[a[i][j]]!=1)) {result[a[i][j]]=1;count[a[i][j]]=0;}
           }
          // result['P']=0;
           return result;
       }



}
int main()
{
    char grid[3][3];
    memset(grid,'.',sizeof(grid));
    map <char,int> num;

    int kase;
    bool yes=true;
    //freopen("tic.txt","r",stdin);
    cin>>kase;
    while(kase>0)
    {   yes=true;
        num['X']=0;
        num['O']=0;

        fo(i,0,3)
        {
            fo(j,0,3)
         cin>>grid[i][j];

        }
        fo(i,0,3)
        {
            fo(j,0,3)
            {
                num[grid[i][j]]++;

            }
        }
        map <char,int> digo;
        digo['X']=0;
        digo['O']=0;
        if((grid[0][0]==grid[1][1])&&(grid[1][1]==grid[2][2])) digo[grid[0][0]]=1;
        else if((grid[0][2]==grid[1][1])&&(grid[1][1]==grid[2][0])) digo[grid[0][2]]=1;
        //cout<<"NUmber of X: "<<num['X']<<endl;
        //cout<<"NUmber of O: "<<num['O']<<endl;

          map <char,int> won;
          map <char,int> v_w;
          won=wins(grid);
           // cout<<endl;
        // cout<<"wX: "<<won['X']<<endl;
         //cout<<"wO: "<<won['O']<<endl;
         if((won['X']!=1)&&(won['O']!=1)) {v_w=v_wins(grid);} //cout<<v_w['X']<<endl;cout<<v_w['O']<<endl;
         //if(won['X']==1&&won['O']==1) cout<<"NO"<<endl;
         //else if(v_w['X']==1&&v_w['O']==1) cout<<"NO"<<endl;
         //else cout<<"YES"<<endl;
         bool x_wins=false;
         bool o_wins=false;
         if(won['X']==1||v_w['X']==1||digo['X']==1) x_wins=true;
         if(won['O']==1||v_w['O']==1||digo['O']==1) o_wins=true;
        // cout<<"x_wins: "<<x_wins<<endl;
         //cout<<"o_wins: "<<o_wins<<endl;

            if((num['X']-num['O'])>1) {yes=false;} //cout<<"1"<<endl;}
            else if(num['X']<num['O']) {yes=false;}//cout<<"x less"<<endl;}
            else if(x_wins==true&&o_wins==true) {yes=false;}// cout<<"both wins"<<endl;}
            else if(x_wins==true&&(num['X']-num['O'])>1) {yes=false;}// cout<<"x wins but diff"<<endl;}
            else if(o_wins==true&&(num['X']!=num['O'])) {yes=false;}// cout<<"o wins"<<endl;}


            if(yes==false) cout<<"no"<<endl;
         else if(yes==true) cout<<"yes"<<endl;
         num.clear();
         yes=true;
         if(kase>1)
         {
             cout<<endl;
         }





        kase--;
    }
    return 0;
}


Eather
New poster
Posts: 28
Joined: Thu Jan 28, 2010 2:23 pm

Re: 10363 - Tic Tac Toe

Post by Eather » Mon Apr 04, 2011 9:02 am

Getting WA... but why??

Code: Select all

                           /*in the name of Allah */


# include <list>
# include <deque>
# include <bitset>
# include <algorithm>
# include <functional>
# include <numeric>
# include <utility>
# include <sstream>
# include <iostream>
# include <iomanip>
# include <cstdio>
# include <cmath>
# include <cstdlib>
# include <ctime>
# include <set>
# include <map>
# include <cmath>
# include <queue>
# include <limits>
# include <stack>
# include <vector>
# include <cstring>
# include <cstdio>
using namespace std;

# define MEM(array,w)   memset(array,w,sizeof array)
# define fr(i,a,b) for(int (i) = a ; i < b ; i++)
# define SET set<int>::iterator it = s.begin(); it != s.end();it++
# define ULL unsigned long long
# define eps 1e-9
# define SS stringstream
# define PR pair<int , int>
# define all(c) (c).begin(), (c).end()
# define maxint 1 << 31 - 1
# define FOR(i, a, b) for (int i=a; i<b; i++)
# define REP(i, a) FOR(i, 0, a)
# define rive(s) reverse(s.begin(),s.end())
# define OK(R,C) if(i>=0 && j>=0 && j<=C && i<=R)

template<class T> string toString(T n){ostringstream ost;ost<<n;ost.flush();return ost.str();}

int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}

bool isprime(int n){if( n<2) return 0;for( int i=2; i*i<=n ; i++)if(n%i==0)return 0; return 1;return 0;}

int pel(string s){string t;t=s;reverse(t.begin(),t.end());if(s==t)return 1;return 0;}
char tic[3][3];
int x,o;
int xx,oo;

void tictoc()
{
  x=o=0;
  if(tic[0][0]==tic[0][1] && tic[0][1]==tic[0][2])if(tic[0][0]=='X')x++;else if(tic[0][0]=='O')o++;//"""
  if(tic[1][0]==tic[1][1] && tic[1][1]==tic[1][2])if(tic[1][0]=='X')x++;else if(tic[1][0]=='O')o++;//"""---
  if(tic[2][0]==tic[2][1] && tic[2][1]==tic[2][2])if(tic[2][0]=='X')x++;else if(tic[2][0]=='O')o++;//"""---___
  if(tic[0][0]==tic[1][0] && tic[1][0]==tic[2][0])if(tic[0][0]=='X')x++;else if(tic[0][0]=='O')o++;//|
  if(tic[0][1]==tic[1][1] && tic[1][1]==tic[2][1])if(tic[0][1]=='X')x++;else if(tic[0][1]=='O')o++;// |
  if(tic[0][2]==tic[1][2] && tic[1][2]==tic[2][2])if(tic[0][2]=='X')x++;else if(tic[0][2]=='O')o++;//  |
  if(tic[0][0]==tic[1][1] && tic[1][1]==tic[2][2])if(tic[2][2]=='X')x++;else if(tic[2][2]=='O')o++;//"-_
  if(tic[2][0]==tic[1][1] && tic[1][1]==tic[0][2])if(tic[2][0]=='X')x++;else if(tic[2][0]=='O')o++;//_-"
}

int main()
{
  int n,i,j,k;
  scanf("%d\n",&n);
  bool s=0;
  bool f;
  for( k=0;k<n;k++){
   // if(s==1)cout<<endl;s=1;
    f=false;xx=0,oo=0;

    for(i=0;i<3;i++)
    {
      for( j=0;j<3;j++)
      {
        cin>>tic[i][j];
        if(tic[i][j]=='X')xx++;
        else if(tic[i][j]=='O')oo++;
      }
      getchar();
    }

    tictoc();
    //cout<<x<<" "<<o<<" "<<xx<<" "<<oo<<endl;
    if(x==1 && o==0 && xx==(oo+1))f=true;
    else if(o==1 && x==0 && xx==oo)f=true;
    else if(x==0 && o==0 && xx==(oo+1))f=true;

    if(f)cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }

return 0;

}
please help me

User avatar
plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

Re: 10363 - Tic Tac Toe

Post by plamplam » Thu Sep 01, 2011 7:07 pm

Code: Select all

34
XXX
XOO
XOO

OXX
XOO
XXO

X..
X..
X..

X..
X..
O..

X.X
...
.X.

O.O
...
.O.

X.O
XO.
XOX

X.O
XOO
XXX

OXO
XXX
OXO

XXX
.O.
.O.

OOO
.X.
.X.

XOX
OXO
XOX

XXX
OOO
X.O

X.O
..X
X..

X.X
.OO
XOO

X.X
OO.
X.X

X.O
O.X
X.O

X.O
OO.
XXX

X.O
O.X
OOX

X.O
O..
XXO

X.O
O.O
X..

...
...
...

...
.X.
...

...
.O.
...

OO.
XXO
X.O

XXX
XXX
XXX

OOO
OOO
OOO

.O.
XXX
.O.

.X.
OOO
.X.

X.O
.X.
O.X

OXO
OXX
X.O

XXX
.OO
OX.

OOO
.XX
X.X

XO.
XO.
XOO

Code: Select all

yes
no
no
yes
no
no
yes
no
yes
yes
no
yes
no
no
no
no
yes
yes
no
yes
no
yes
yes
no
no
no
no
yes
no
yes
yes
yes
no
no
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

Post Reply

Return to “Volume 103 (10300-10399)”