首页 > 专题系列 > Java解POJ > POJ 1244 Slots of Fun [解题报告] Java
2013
11-09

POJ 1244 Slots of Fun [解题报告] Java

Slots of Fun

问题描述 :

The International Betting Machine company has just issued a new type of slot machine. The machine display consists of a set of identical circles placed in a triangular shape. An example with four rows is shown below. When the player pulls the lever, the machine places a random letter in the center of each circle. The machine pays off whenever any set of identical letters form the vertices of an equilateral triangle. In the example below, the letters ‘a’ and ‘c’ satisfy this condition.



In order to prevent too many payoffs, the electronics in the machine ensures that no more than 3 of any letter will appear in any display configuration.

IBM is manufacturing several models of this machine, with varying number of rows in the display, and they are having trouble writing code to identify winning configurations. Your job is to write that code.

输入:

Input will consist of multiple problem instances. Each instance will start with an integer n indicating the number of rows in the display. The next line will contain n(n + 1)/2 letters of the alphabet (all lowercase) which are to be stored in the display row-wise, starting from the top. For example, the display above would be specified as

4

abccddadca

The value of n will be between 1 and 12, inclusive. A line with a single 0 will terminate input.

输出:

For each problem instance, output all letters which form equilateral triangles on a single line, in alphabetical order. If no such letters exist, output “LOOOOOOOOSER!”.

样例输入:

4
abccddadca
6
azdefccrhijrrmznzocpq
2
abc
0

样例输出:

ac
crz
LOOOOOOOOSER!

解题代码:

//* @author popop0p0popo
import java.util.*;
import java.io.*;

public class Main{
 public static void main(String[] args) throws Exception{
  Scanner scanner=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n,flag;
		String line,r;
		char c;
		int[] idx;
		while (true){
			n=scanner.nextInt();
			if (n==0){
				break;
			}
			line=scanner.next();
			r="";
			for (int i=0;i< 26 ;i++ ){
				c=(char) ('a'+i);
				idx=new int[n];
				flag=0;
				while (line.indexOf(c)!=-1){
					idx[flag]=line.indexOf(c);
					flag++;
					line=line.replaceFirst(""+c,"#");
				}
				if (flag>=3){
					if (isD(idx,flag)){
						r=r+c;
					}
				}
			}
			if (r.equals("")){
				System.out.println("LOOOOOOOOSER!");
			}
			else{
				System.out.println(r);
			}
		}
	}

	public static boolean isD(int[] idx,int flag){
		int a,b,c;
		int aa,bb,cc;
		int[] iidx=new int[3];
		boolean f,ff1,ff2;
		for (int i=0;i< flag-2 ;i++ ){
			for (int j=i+1;j< flag-1 ;j++ ){
				for (int k=j+1;k< flag ;k++ ){
					a=getP(idx[i])-1;
					b=getP(idx[j])-1;
					c=getP(idx[k])-1;
					aa=getL(idx[i]);
					bb=getL(idx[j]);
					cc=getL(idx[k]);
   if (3*(a-b)*(a-b)+(aa-bb)*(aa-bb)==3*(a-c)*(a-c)+(aa-cc)*(aa-cc)&&
      3*(c-b)*(c-b)+(cc-bb)*(cc-bb)==3*(a-c)*(a-c)+(aa-cc)*(aa-cc)){
						return true;
					}
				}
			}
		}
		return false;
	}

	public static int getP(int n){
		int flag=1;
		while (true){
			if (n<=(flag)*(flag+1)/2-1){
				return flag;
			}
			flag++;
		}
	}

	public static int getL(int n){
		int p=getP(n);
		int a=n-p*(p-1)/2;
		if (p%2==0){
			return 2*a-p+1;
		}
		else{
			return 2*(a-p/2);
		}
	}
}