首页 > 专题系列 > Java解POJ > POJ 1970 The Game [解题报告] Java
2013
11-10

POJ 1970 The Game [解题报告] Java

The Game

问题描述 :

A game of Renju is played on a 19*19 board by two players. One player uses black stones and the other uses white stones. The game begins in an empty board and two players alternate in placing black stones and white stones. Black always goes first. There are 19 horizontal lines and 19 vertical lines in the board and the stones are placed on the intersections of the lines.

Horizontal lines are marked 1, 2, …, 19 from up to down and vertical lines are marked 1, 2, …, 19 from left to right.



The objective of this game is to put five stones of the same color consecutively along a horizontal, vertical, or diagonal line. So, black wins in the above figure. But, a player does not win the game if more than five stones of the same color were put consecutively.

Given a configuration of the game, write a program to determine whether white has won or black has won or nobody has won yet. There will be no input data where the black and the white both win at the same time. Also there will be no input data where the white or the black wins in more than one place.

输入:

The first line of the input contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. Each test case consists of 19 lines, each having 19 numbers. A black stone is denoted by 1, a white stone is denoted by 2, and 0 denotes no stone.

输出:

There should be one or two line(s) per test case. In the first line of the test case output, you should print 1 if black wins, 2 if white wins, and 0 if nobody wins yet. If black or white won, print in the second line the horizontal line number and the vertical line number of the left-most stone among the five consecutive stones. (Select the upper-most stone if the five consecutive stones are located vertically.)

样例输入:

1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 0 0 2 2 2 1 0 0 0 0 0 0 0 0 0 0
0 0 1 2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 2 2 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

样例输出:

1
3 2

解题代码:

/* @author: */
import java.util.Scanner;
public class Main{
 static int p[][]=new int[21][21];
 public static void  main(String args[])
{
  Scanner sc=new Scanner(System.in);
  int tt,i,j,k,x=0,y=0;
  tt=sc.nextInt();
  while((tt--)!=0)
  {
	
   for(i=1;i< 20;i++)
    for(j=1;j< 20;j++)
	p[i][j]=sc.nextInt();
   int tag=0;
   for(i=1;i< 20;i++)
   {
    for(j=1;j< 16;j++)
    {
	if(p[i][j]==p[i][j-1]) continue;
	if(p[i][j]!=p[i][j+1]) continue;
	if(p[i][j]!=p[i][j+2]) continue;
	if(p[i][j]!=p[i][j+3]) continue;
	if(p[i][j]!=p[i][j+4]) continue;
	if(p[i][j]==p[i][j+5]) continue;
	tag=p[i][j];
	x=i;
	y=j;
	break;
    }
    if(tag!=0) break;
   }

   for(j=1;j< 20;j++)
   {
    if(tag!=0) break;
	for(i=1;i< 16;i++)
	{
	  if(p[i][j]==p[i-1][j]) continue;
	  if(p[i][j]!=p[i+1][j]) continue;
	  if(p[i][j]!=p[i+2][j]) continue;
	  if(p[i][j]!=p[i+3][j]) continue;
	  if(p[i][j]!=p[i+4][j]) continue;
	  if(p[i][j]==p[i+5][j]) continue;
	  tag=p[i][j];
	  x=i;
	  y=j;
	  break;
	}
    }
   for(i=1;i< 16;i++)
   {
    if(tag!=0) break;
    for(j=1;j< 16;j++)
    {
	if(p[i][j]==p[i-1][j-1]) continue;
	if(p[i][j]!=p[i+1][j+1]) continue;
	if(p[i][j]!=p[i+2][j+2]) continue;
	if(p[i][j]!=p[i+3][j+3]) continue;
	if(p[i][j]!=p[i+4][j+4]) continue;
	if(p[i][j]==p[i+5][j+5]) continue;
	tag=p[i][j];
	x=i;
	y=j;
	break;
    }
   }
  for(i=19;i>4;i--)
  {
   if(tag!=0) break;
   for(j=1;j< 16;j++)
   {
	if(p[i][j]==p[i+1][j-1]) continue;
	if(p[i][j]!=p[i-1][j+1]) continue;
	if(p[i][j]!=p[i-2][j+2]) continue;
	if(p[i][j]!=p[i-3][j+3]) continue;
	if(p[i][j]!=p[i-4][j+4]) continue;
	if(p[i][j]==p[i-5][j+5]) continue;
	tag=p[i][j];
	x=i;
	y=j;
	break;
   }
 }
 if(tag==0) System.out.println("0");
 else System.out.printf("%d\n%d %d\n",tag,x,y);
 }
 }
}

  1. 代码是给出了,但是解析的也太不清晰了吧!如 13 abejkcfghid jkebfghicda
    第一步拆分为 三部分 (bejk, cfghi, d) * C(13,3),为什么要这样拆分,原则是什么?