【模拟】Tic-tac-toe  C…

来源:互联网 发布:淘宝手机营销中心 编辑:程序博客网 时间:2024/06/05 18:55
Tic-tac-toe
timelimit per test
1 second
memorylimit per test
64 megabytes
input
standard input
output
standard output

Certainly, everyone is familiar with tic-tac-toe game. The rulesare very simple indeed. Two players take turns marking the cells ina 3 × 3grid(one player always draws crosses, the other — noughts). The playerwho succeeds first in placing three of his marks in a horizontal,vertical or diagonal line wins, and the game is finished. Theplayer who draws crosses goes first. If the grid is filled, butneither Xs, nor 0s form the required line, a draw is announced.

You are given a 3 × 3 grid,each grid cell is empty, or occupied by a cross or a nought. Youhave to find the player (first or second), whose turn is next, orprint one of the verdicts below: 

  • illegal —if the given board layout can't appear during a validgame; 
  • the firstplayer won — if in the given board layoutthe first player has just won; 
  • thesecond player won — if in the given boardlayout the second player has just won; 
  • draw —if the given board layout has just let to adraw. 
Input

The input consists of three lines, each of the lines containscharacters ".","X"or "0"(a period, a capital letter X, or a digit zero).

Output

Print one of the six verdicts: firstsecondillegalthefirst player wonthesecond playerwon or draw.

Sample test(s)
input
X0X
.0.
.X.
output
second

今天算是见识到了CodeForces的数据,有个小伙伴wa在了text120....
于是写之前想了很多种情况
判断的优先级:
illegal-->first win/second win-->draw --> first/second


#include

#include

#include

using namespace std;


inta[3][3];


boolcheck(intx)

{

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

    {

       intj;

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

          if(a[i][j]!= x) break;

       if(j == 3)return1;

    }

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

    {

       inti;

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

          if(a[i][j]!= x) break;

       if(i == 3)return1;

    }

    if(a[0][0]== x && a[1][1]== x && a[2][2]== x) return1;

    if(a[0][2]== x && a[1][1]== x && a[2][0]== x) return1;

    

   return 0;

}


intmain()

{

    char c;

    intn1=0,n2 = 0;

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

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

       {

          cin>> c;

          if(c == '.')a[i][j]= 0;

          if(c == 'X')

           {

              a[i][j]= 1;

              n1++;

           }

          if(c == '0')

           {

              a[i][j]= 2;

              n2++;

           }

       }

    

    if(n1 - n2 > 1|| n2>n1)

    {

       printf("illegal\n");

       return0;

    }

    

    boolw1 = 0,w2 = 0;

    w1 = check(1);

    w2 = check(2);

    intnext;

    if(n1 > n2) next = 2;

    elsenext = 1;

    

    if(w1 && w2)

    {

       printf("illegal\n");

       return0;

    }

    

    if(w1 && next==1)

    {

       printf("illegal\n");

       return0;

    }

    

    if(w2 && next == 2)

    {

       printf("illegal\n");

       return0;

    }

    

    if(w1)

    {

      printf("thefirst player won\n");

       return0;

    }

    if(w2)

    {

      printf("thesecond player won\n");

       return0;

    }

    if(n1 + n2 == 9)

    {

       printf("draw\n");

       return0;

    }

    if(next==1)

    {

       printf("first\n");

       return0;

    }

    if(next == 2)

    {

       printf("second\n");

       return0;

    }

}

 


0 0