pku(2361)Tic Tac Toe

作者在 2008-03-29 10:39:33 发布以下内容

给提示 

 

XXX              XXX       OOO   

XOO            OO.        X.X

XOO            O. .        X..

yes              no        yes

就这么点要注意的:

 

#include <iostream>
char winner[9];
char toe[3][3];
int X,O;
int legal;
using namespace std;
bool islegall()
{
  legal=0;
  if((X!=O+1)&&(X!=O))
       return false;
 for(int i=0;i<3;i++)
 {
  if(toe[i][0]==toe[i][1]&&toe[i][0]==toe[i][2]&&(toe[i][0]=='X'||toe[i][0]=='O'))
  {
   if(toe[i][0]=='X')
    winner[legal++]='X';
   else
    winner[legal++]='O';
  }
  if(toe[0][i]==toe[1][i]&&toe[0][i]==toe[2][i]&&(toe[0][i]=='X'||toe[0][i]=='O'))
  {
     if(toe[0][i]=='X')
    winner[legal++]='X';
   else
    winner[legal++]='O';
  }
 }
 if(toe[0][0]==toe[1][1]&&toe[0][0]==toe[2][2]&&(toe[0][0]=='X'||toe[0][0]=='O'))
  {
    if(toe[0][0]=='X')
    winner[legal++]='X';
   else
    winner[legal++]='O';
  }
 if(toe[0][2]==toe[1][1]&&toe[0][2]==toe[2][0]&&(toe[0][2]=='X'||toe[0][2]=='O'))
  {
    if(toe[2][0]=='X')
    winner[legal++]='X';
   else
    winner[legal++]='O';
  }
 if(legal==1||legal==0||(legal==2&&winner[0]=='X'&&winner[1]=='X'))
  return true;
 else
  return false;
}
bool real()
{
 if(!islegall())
  return false;
 else if(legal==1&&(X==O+1))
 {
  if(winner[0]=='X')
   return true;
  else
   return false;
 }
 else if(legal==1&&(X==O))
 {
  if(winner[0]=='O')
   return true;
  else
   return false;
 }
 else
  return true;
}

 

int main()
{
 int n;
 cin >>n;
 while(n--)
 {
    X=0;
    O=0;
   for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
      {
       cin >>toe[i][j];
       if(toe[i][j]=='X')
        X++;
       else if(toe[i][j]=='O')
        O++;
      }


      if(real())
       cout<<"yes"<<endl;
      else
       cout<<"no"<<endl;
      memset(winner,'.',sizeof(winner));
 }
      return 0;

}

代码写得很wa…………-_-!

acm | 阅读 1843 次
文章评论,共0条
游客请输入验证码
浏览260759次