Time limit: 3.000 seconds
Crossword Answers |
A crossword puzzle consists of a rectangular grid of black and white squares and two lists of definitions (or descriptions).
One list of definitions is for ``words" to be written left to right across white squares in the rows and theother list is for words to be written down white squares in the columns. (A word is a sequence of alphabetic characters.)
To solve a crossword puzzle, one writes the words corresponding to the definitions on the white squares of the grid.
The definitions correspond to the rectangular grid by means of sequential integers on ``eligible" white squares. White squares with black squaresimmediately to the left or above them are ``eligible." White squares with nosquares either immediately to the left or above are also ``eligible." No othersquares are numbered. All of the squares on the first row are numbered.
The numbering starts with 1 and continues consecutively across white squares of the first row, then across the eligible white squares of the second row, then across the eligible white squares of the third row and so on across all of the rest of the rows of the puzzle. The picture below illustrates a rectangular crossword puzzle grid with appropriate numbering.
An ``across" word for a definition is written on a sequence of white squares ina row starting on a numbered square that does not follow another white square in the same row.
The sequence of white squares for that word goes across the row of the numbered square, ending immediately before the next black square in the row or in the rightmost square of the row.
A ``down" word for a definition is written on a sequence of white squares in acolumn starting on a numbered square that does not follow another white square in the same column.
The sequence of white squares for that word goes down the column of the numbered square, ending immediately before the next black square in the column or in the bottom square of the column.
Every white square in a correctly solved puzzle contains a letter.
You must write a program that takes several solved crossword puzzles as input and outputs the lists of across and down words which constitute the solutions.
Each puzzle solution in the input starts with a line containing two integers rand c ( and ), where r (the first number) is the number ofrows in the puzzle and c (the second number) is the number of columns.
The rrows of input which follow each contain c characters (excluding the end-of-line) which describe the solution. Each of those c characters is an alphabeticcharacter which is part of a word or the character ``*", which indicates ablack 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 downwords. 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 puzzlesby 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【分析】
(分析过程附加在程序注释中)
用java语言编写程序,代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//puzzle的序号
int puzzleCase = 0;
while(input.hasNext()) {
int r = input.nextInt();
if(r == 0) break;
int c = input.nextInt();
char[][] arr = new char[r][c];//存储网格字符
int[][] orderArr = new int[r][c];
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
orderArr[i][j] = 0;
//给起始格从左到右,从上到下标号
int order = 1;
for(int i = 0; i < r; i++) {
String str = input.next();
for(int j = 0; j < c; j++) {
arr[i][j] = str.charAt(j);
if(arr[i][j] != '*') {
//对起始格进行标号
if(i == 0 || j == 0)
orderArr[i][j] = order++;
else {
if(arr[i - 1][j] == '*' || arr[i][j - 1] == '*')
orderArr[i][j] = order++;
}
}
}
}
if(puzzleCase != 0)
System.out.println();
System.out.println("puzzle #" + (++puzzleCase) + ":");
System.out.println("Across");
outputR(arr, orderArr, r, c/*, true*/);
System.out.println("Down");
outputC(arr, orderArr, r, c);
}
}
//输出横向词
private static void outputR(char[][] arr, int[][] orderArr, int r, int c/*, boolean isR*/) {
for(int i = 0; i < r; i++) {
String temp = "";//保存横向词
int firstPos = 0;//记录横向词的起始位置
boolean flag = true;//标记横向词的开头
for(int j = 0; j < c; j++) {
/*if(!isR) {
int t = i;
i = j;
j = t;
}*/
if(arr[i][j] != '*') {
//如果是横向词的开头则记录横向词的起始位置,并且flag修改为false
if(flag) {
firstPos = orderArr[i][j];
flag = false;
}
//连接横向词的一部分
temp = temp + arr[i][j];
}
else {
//遇到黑格时,输出前面所记录的横向词
if(!temp.equals("")) {
System.out.printf("%3d." + temp + "\n", firstPos);
temp = "";
flag = true;
}
}
/*if(!isR) {
int t = i;
i = j;
j = t;
}*/
}
//数组中的行尾可能还有横向词,输出剩下的一个横向词
//(前面是根据遇到黑格时就输出横向词,可能行尾还有一个不能根据此输出,所以要另外进行判断输出)
if(!temp.equals(""))
System.out.printf("%3d." + temp + "\n", firstPos);
}
}
//输出竖向词
private static void outputC(char[][] arr, int[][] orderArr, int r, int c) {
//假设从第N行开始,那么这一行的所有元素将可能成为某个竖向词的开头
//在N行的第一个元素开始,先从上往下找出竖向词,一旦找到第一个竖向词或者根据第一个元素找不到竖向词就立马跳出循环(找到之后进行输出)
//(注意当前元素可能只是竖向词的一部分,所以要判断是否可以作为竖向词的开头,不能的话跳出循环)
//再N行的第二个元素开始,同样也是按照之前的步骤,先从上往下找出竖向词,一旦找到第一个竖……
//N行判断完之后,就进入下一行即N+1行继续同样的步骤
for(int line = 0; line < r; line++) {
for(int i = 0; i < c; i++) {
String temp = "";//保存竖向词
int firstPos = 0;//记录竖向词的起始位置
boolean flag = true;//标记竖向词的开头
for(int j = line; j < r; j++) {
//判断是否可以为竖向词的开头
if(arr[j][i] == '*' || (line != 0 && arr[line - 1][i] != '*'))
break;
if(arr[j][i] != '*') {
temp = temp + arr[j][i];
if(flag) {
firstPos = orderArr[j][i];
flag = false;
}
}
else {
if(!temp.equals("")) {
System.out.printf("%3d." + temp + "\n", firstPos);
temp = "";
flag = true;
break;//找到竖向词后跳出循环
}
}
}
if(!temp.equals("")) {
System.out.printf("%3d." + temp + "\n", firstPos);
}
}
}
}
}