首页 > 专题系列 > Java解POJ > POJ 2996 Help Me with the Game [解题报告] Java
2013
11-12

POJ 2996 Help Me with the Game [解题报告] Java

Help Me with the Game

问题描述 :

Your task is to read a picture of a chessboard position and print it in the chess notation.

输入:

The input consists of an ASCII-art picture of a chessboard with chess pieces on positions described by the input. The pieces of the white player are shown in upper-case letters, while the black player’s pieces are lower-case letters. The letters are one of “K” (King), “Q” (Queen), “R” (Rook), “B” (Bishop), “N” (Knight), or “P” (Pawn). The chessboard outline is made of plus (“+”), minus (“-”), and pipe (“|”) characters. The black fields are filled with colons (“:”), white fields with dots (“.”).

输出:

The output consists of two lines. The first line consists of the string “White: “, followed by the description of positions of the pieces of the white player. The second line consists of the string “Black: “, followed by the description of positions of the pieces of the black player.

The description of the position of the pieces is a comma-separated list of terms describing the pieces of the appropriate player. The description of a piece consists of a single upper-case letter that denotes the type of the piece (except for pawns, for that this identifier is omitted). This letter is immediatelly followed by the position of the piece in the standard chess notation — a lower-case letter between “a” and “h” that determines the column (“a” is the leftmost column in the input) and a single digit between 1 and 8 that determines the row (8 is the first row in the input).

The pieces in the description must appear in the following order: King(“K”), Queens (“Q”), Rooks (“R”), Bishops (“B”), Knights (“N”), and pawns. Note that the numbers of pieces may differ from the initial position because of capturing the pieces and the promotions of pawns. In case two pieces of the same type appear in the input, the piece with the smaller row number must be described before the other one if the pieces are white, and the one with the larger row number must be described first if the pieces are black. If two pieces of the same type appear in the same row, the one with the smaller column letter must appear first.

样例输入:

+---+---+---+---+---+---+---+---+
|.r.|:::|.b.|:q:|.k.|:::|.n.|:r:|
+---+---+---+---+---+---+---+---+
|:p:|.p.|:p:|.p.|:p:|.p.|:::|.p.|
+---+---+---+---+---+---+---+---+
|...|:::|.n.|:::|...|:::|...|:p:|
+---+---+---+---+---+---+---+---+
|:::|...|:::|...|:::|...|:::|...|
+---+---+---+---+---+---+---+---+
|...|:::|...|:::|.P.|:::|...|:::|
+---+---+---+---+---+---+---+---+
|:P:|...|:::|...|:::|...|:::|...|
+---+---+---+---+---+---+---+---+
|.P.|:::|.P.|:P:|...|:P:|.P.|:P:|
+---+---+---+---+---+---+---+---+
|:R:|.N.|:B:|.Q.|:K:|.B.|:::|.R.|
+---+---+---+---+---+---+---+---+

样例输出:

White: Ke1,Qd1,Ra1,Rh1,Bc1,Bf1,Nb1,a2,c2,d2,f2,g2,h2,a3,e4
Black: Ke8,Qd8,Ra8,Rh8,Bc8,Ng8,Nc6,a7,b7,c7,d7,e7,f7,h7,h6

解题代码:

import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;

 public class Main {

     class Compara implements Comparator< String[]> {

         public int compare(String[] o1, String[] o2) {
             if (o1[1].charAt(1) == o2[1].charAt(1)) {
                 return o1[1].charAt(0) - o2[1].charAt(0);
             } else if (Character.isUpperCase(o1[0].charAt(0))) {
                 return o1[1].charAt(1) - o2[1].charAt(1);
             } else {
                 return o2[1].charAt(1) - o1[1].charAt(1);
             }
         }
     }

     static char[] a = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };

     public static void main(String[] args) throws IOException {
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         String s;
         char c;
         String[] q;
         List< String[]> list = new ArrayList< String[]>();
         String[] t;
         for (int i = 0; i < 17; i++) {
             if (i % 2 == 0) {
                 read.readLine();
                 continue;
             }
             s = read.readLine();
             q = s.split("\\|");
             for (int j = 1; j <= 8; j++) {
                 c = q[j].charAt(1);
                 if (c == '.' || c == ':') {
                     continue;
                 }
                 t = new String[2];
                 t[0] = "" + c;
                 t[1] = "" + a[j - 1] + (8 - i / 2);
                 list.add(t);
             }
         }
         System.out.print("White: ");
         print("K", list);
         print("Q", list);
         print("R", list);
         print("B", list);
         print("N", list);
         print("P", list);
         System.out.println();
         System.out.print("Black: ");
         print("k", list);
         print("q", list);
         print("r", list);
         print("b", list);
         print("n", list);
         print("p", list);
         System.out.println();
     }

     public static void print(String key, List< String[]> list) {
         if (key.equals("k") || key.equals("K") || key.equals("q")
                 || key.equals("Q")) {
             String[] s = null;
             for (int i = 0; i < list.size(); i++) {
                 s = list.get(i);
                 if (s[0].equals(key)) {
                     break;
                 }
             }
             if (s != null) {
                 if (key.equals("k") || key.equals("K")) {
                     System.out.print(Character.toUpperCase(key.charAt(0))
                             + s[1]);
                 } else {
                     System.out.print("," + Character.toUpperCase(key.charAt(0))
                             + s[1]);
                 }
             }
         } else {
             List< String[]> temp = new ArrayList< String[]>();
             String[] s;
             for (int i = 0; i < list.size(); i++) {
                 s = list.get(i);
                 if (s[0].equals(key)) {
                     temp.add(s);
                 }
             }
             if (temp.size() > 0) {
                 Collections.sort(temp, new Main().new Compara());
                 if (key.equals("p") || key.equals("P")) {
                     for (int i = 0; i < temp.size(); i++) {
                         System.out.print("," + temp.get(i)[1]);
                     }
                 } else {
                     for (int i = 0; i < temp.size(); i++) {
                         System.out.print(","
                                 + Character.toUpperCase(key.charAt(0))
                                 + temp.get(i)[1]);
                     }
                 }
             }
         }
     } 
}

  1. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。

  2. 为什么for循环找到的i一定是素数叻,而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak,而你每次取余都用的是原来的m,也就是n

  3. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  4. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。