首页 > 专题系列 > Java解POJ > POJ 2361 Tic Tac Toe [解题报告] Java
2013
11-11

POJ 2361 Tic Tac Toe [解题报告] Java

Tic Tac Toe

问题描述 :

Tic Tac Toe is a child’s game played on a 3 by 3 grid. One player, X, starts by placing an X at an unoccupied grid position. Then the other player, O, places an O at an unoccupied grid position. Play alternates between X and O until the grid is filled or one player’s symbols occupy an entire line (vertical, horizontal, or diagonal) in the grid.

We will denote the initial empty Tic Tac Toe grid with nine dots. Whenever X or O plays we fill in an X or an O in the appropriate position. The example below illustrates each grid configuration from the beginning to the end of a game in which X wins.
...  X..  X.O  X.O  X.O  X.O  X.O  X.O

... ... ... ... .O. .O. OO. OO.
... ... ... ..X ..X X.X X.X XXX

Your job is to read a grid and to determine whether or not it could possibly be part of a valid Tic Tac Toe game. That is, is there a series of plays that can yield this grid somewhere between the start and end of the game?

输入:

The first line of input contains N, the number of test cases. 4N-1 lines follow, specifying N grid configurations separated by empty lines.

输出:

For each case print “yes” or “no” on a line by itself, indicating whether or not the configuration could be part of a Tic Tac Toe game.

样例输入:

2
X.O
OO.
XXX

O.X
XX.
OOO

样例输出:

yes
no

解题代码:

/* @author: */
import java.util.Scanner;
public class Main{
  public static void main(String args[])
{     
   char cc[][]=new char[3][3];
   Scanner sc=new Scanner(System.in);

   int n,i,j;
   n=sc.nextInt();
   while((n--)!=0)
   {
	for(i=0;i< 3;i++)
        cc[i]=sc.next().toCharArray();
			
	int x=0,o=0;
       boolean xw=false,ow=false;
       boolean bb=true;
 	for(i=0;i< 3;i++)
	 for(j=0;j< 3;j++)
	 {
	    if(cc[i][j]=='X')x++;
	    else if(cc[i][j]=='O')o++;
	 }
	if(x-o>1||x< o)bb=false;
	for(i=0;i< 3;i++)
	{
	  boolean tt=true;
	  for(j=0;j< 2;j++)
	  {
	    if(cc[i][j]!=cc[i][j+1])
	    {
		tt=false;
		break;
	     }
			
	   }
	   if(tt)
	   {
		if(cc[i][0]=='X')xw=true;
		else if(cc[i][0]=='O')ow=true;
	    }
	  }

	 for(j=0;j< 3;j++)
	{
	  boolean  tt=true;
	  for(i=0;i< 2;i++)
	  {
	    if(cc[i][j]!=cc[i+1][j])
	     {
		tt=false;
		break;
	     }
	   }
	   if(tt)
	    {
		if(cc[0][j]=='X')xw=true;
		else if(cc[0][j]=='O')ow=true;
	    }
	  }
	 if(cc[0][0]==cc[1][1]&&cc[1][1]==cc[2][2]&&cc[2][2]=='X')xw=true;
	 else if(cc[0][0]==cc[1][1]&&cc[1][1]==cc[2][2]&&cc[2][2]=='O')ow=true;
	 if(cc[0][2]==cc[1][1]&&cc[1][1]==cc[2][0]&&cc[2][0]=='X')xw=true;
	 else if(cc[0][2]==cc[1][1]&&cc[1][1]==cc[2][0]&&cc[2][0]=='O')ow=true;
	 if(ow&&xw) bb=false;
	 else if(ow&&x!=o)bb=false;
	 else if(xw&&x==o)bb=false;
	 if(bb) System.out.println("yes");
	 else System.out.println("no");
	}
   }
}

  1. 漂亮。佩服。
    P.S. unsigned 应该去掉。换行符是n 不是/n
    还可以稍微优化一下,
    int main() {
    int m,n,ai,aj,bi,bj,ak,bk;
    while (scanf("%d%d",&m,&n)!=EOF) {
    ai = sqrt(m-1);
    bi = sqrt(n-1);
    aj = (m-ai*ai-1)>>1;
    bj = (n-bi*bi-1)>>1;
    ak = ((ai+1)*(ai+1)-m)>>1;
    bk = ((bi+1)*(bi+1)-n)>>1;
    printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
    }
    }

  2. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.