算法競賽入門-縱橫字謎的答案(Crossword Answers)

1. 題目

 輸入一個r行c列的網格,,黑格用‘*’表示,每個白格都填有一個字母。如果一個白格的左邊相鄰位置或者上邊相鄰位置沒有白格(可能是黑格,也可能出了網格邊界),則稱這個白格是一個起始格。首先把所有的起始格從上到下,從左到右的順序編號爲1,2,3..., 


輸入:

Each puzzle solution in the input starts with a line containing two integers r and c (  xand x ), where r (the first number) is the number of rows in the puzzle and c (the second number) is the number of columns.

The r rows of input which follow each contain c characters (excluding the end-of-line) which describe the solution. Each of those ccharacters is an alphabetic character which is part of a word or the character ``*", which indicates a black square.

The end of input is indicated by a line consisting of the single number 0.


輸出:

Output for each puzzle consists of an identifier for the puzzle (puzzle #1:puzzle #2:, etc.) and the list of across words followed by the list of down words. Words in each list must be output one-per-line in increasing order of the number of their corresponding definitions.

The heading for the list of across words is ``Across". The heading for the list of down words is ``Down".

In the case where the lists are empty (all squares in the grid are black), the Across and Down headings should still appear.

Separate output for successive input puzzles by a blank line.

樣例輸入:

2 2
AT
*O
6 7
AIM*DEN
*ME*ONE
UPON*TO
SO*ERIN
*SA*OR*
IES*DEA
0

樣例輸出:

puzzle #1:
Across
  1.AT
  3.O
Down
  1.A
  2.TO

puzzle #2:
Across
  1.AIM
  4.DEN
  7.ME
  8.ONE
  9.UPON
 11.TO
 12.SO
 13.ERIN
 15.SA
 17.OR
 18.IES
 19.DEA
Down
  1.A
  2.IMPOSE
  3.MEO
  4.DO
  5.ENTIRE
  6.NEON
  9.US
 10.NE
 14.ROD
 16.AS
 18.I
 20.A

2.思路

1 輸入數據的時候判斷當前字母是否爲起始格;

2 輸出每行的字母比較容易,只需要判斷當前是否爲起始格,然後獲取當前的數字下標即可。

3 輸出每列,遍歷每行,再遍歷每列,對當前格子判斷是否爲起始格,如果是起始格則遍歷該列,如果不是,則繼續遍歷每列。


3.代碼

package basic.第三章;

import java.util.Scanner;

/**
 * 縱橫字謎的答案
 * Created by Administrator on 2018/4/8.
 * Sample Input
 * 2 2
 * AT
 * O
 * 6 7
 * AIM*DEN
 * ME*ONE
 * UPON*TO
 * SO*ERIN
 * SA*OR*
 * IES*DEA
 * 0
 * Sample Output
 * puzzle #1:
 * Across
 * 1.AT
 * 3.O
 * Down
 * 1.A
 * 2.TO
 * <p>
 * puzzle #2:
 * Across
 * 1.AIM
 * 4.DEN
 * 7.ME
 * 8.ONE
 * 9.UPON
 * 11.TO
 * 12.SO
 * 13.ERIN
 * 15.SA
 * 17.OR
 * 18.IES
 * 19.DEA
 * Down
 * 1.A
 * 2.IMPOSE
 * 3.MEO
 * 4.DO
 * 5.ENTIRE
 * 6.NEON
 * 9.US
 * 10.NE
 * 14.ROD
 * 16.AS
 * 18.I
 * 20.A
 *
 * @author 春風吹又生
 */
public class CrossedAnswers {
    static char[][] chs;
    static int[][] arrs;
    static int r;
    static int c;

    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        while (true) {
            r = read.nextInt();
            if (r == 0) break;
            c = read.nextInt();
            read.nextLine();

            chs = new char[r][c];
            arrs = new int[r][c];
            int index = 1;
            int count = 0;
            for (int i = 0; i < r; i++) {
                chs[i] = read.nextLine().toCharArray();
                for (int j = 0; j < c; j++) {
                    if (chs[i][j] == '*') continue;
                    int left = j - 1; // 左邊 不是白格
                    int up = i - 1; // 向上  不是白格
                    if (left < 0 || up < 0) {
                        arrs[i][j] = index++;
                    } else if (left >= 0 && up >= 0 && (chs[i][left] == '*' || chs[up][j] == '*')) {
                        arrs[i][j] = index++;
                    }
                }
            }

            System.out.printf("puzzle #%d:", (++count));
            System.out.println();
            printRow();
            printCol();
        }

    }

    // Down
    private static void printCol() {
        System.out.println("Down");
        int col = 0;
        int index = -1;
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                // 判斷左上
                int left = j - 1;
                int up = i - 1;
                String str = "";

                // 如果當前是* 則繼續
                if (chs[i][j] == '*') continue;

                // 如果上一個不是* 則當前不是起始格
                if (up > 0) {
                    if (chs[up][j] != '*') continue;
                }

                if (left < 0 || up < 0) { // 判斷最開始
                    get(i, j, str);

                } else if (left >= 0 && up >= 0 && chs[up][j] == '*') {
                    get(i, j, str);

                }
            }
        }
    }

    private static void get(int i, int j, String str) {
        int index;
        index = arrs[i][j];
        for (int m = i; m < r; m++) {
            if (chs[m][j] != '*')
                str += chs[m][j];//
            else {
                System.out.printf("%3d.%s", index, str);
                System.out.println();
                index = -1;
                str = "";
                break;
            }
        }
        if (!str.equals("") && index != -1) {
            System.out.printf("%3d.%s", index, str);
            System.out.println();
        }
    }

    // Across

    private static void printRow() {
        System.out.println("Across");
        for (int i = 0; i < r; i++) {
            int index = -1;
            String str = "";
            for (int j = 0; j < c; j++) {
                if (chs[i][j] != '*') {
                    if (str.length() == 0) {
                        index = arrs[i][j];
                    }
                    str += chs[i][j];
                }

                if (chs[i][j] == '*') {
                    if (str.equals("")) continue;
                    System.out.printf("%3d.%s", index, str);
                    System.out.println();
                    str = "";
                    index = -1;
                }
            }
            if (!str.equals("") && index != -1) {
                System.out.printf("%3d.%s", index, str);
                System.out.println();
            }
        }
    }

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章