首页 > 其他分享 >纵横字谜的答案(Crossword Answers)

纵横字谜的答案(Crossword Answers)

时间:2022-11-28 18:31:55浏览次数:42  
标签:arr squares temp int puzzle Answers Crossword white 字谜


Crossword Answers

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.

Input

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

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

Separate output for successive input puzzlesby a blank line.

Sample Input


2 2AT
*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

【分析】

       (分析过程附加在程序注释中)

用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);
}
}
}
}
}




标签:arr,squares,temp,int,puzzle,Answers,Crossword,white,字谜
From: https://blog.51cto.com/u_15894233/5893378

相关文章

  • regexcrossword Hamlet篇
    一个练习正则表达式的网站,用类似数独的方式填写,但是规则是正则。从简单到难循序渐进,网址:https://regexcrossword.com偶尔访问不了基本的话,DoubleCross篇之前的做一遍,正......
  • 解决方案:有效的字谜(Python)
    解决方案:有效的字谜(Python)给定两个字符串s和吨,返回真的如果吨是一个字谜s,和错误的否则.一个字谜是通过重新排列不同单词或短语的字母而形成的单词......