算法竞赛入门-纵横字谜的答案(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
 * 

* 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(); } } } }




你可能感兴趣的:(Java,算法,算法竞赛入门)