输入一个r行c列的网格,黑格用‘ * ’表示,每个白格都填有一个字母。如果一个白格的左边相邻位置或者上边相邻位置没有白格(可能是黑格,也可能出了网格边界),则称这个白格是一个起始格。首先把所有的起始格从上到下,从左到右的顺序编号为1,2,3…,
样例输入:
2 2
AT
*O
6 7
AIM*DEN
*ME*ONE
UPON*TO
SO*ERIN
*SA*OR*
IES*DEA
0
样例输出:
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
import java.util.Scanner;
public class Translates {
public static void main(String[] args) {
char[][] a = new char[10][10];
int[][] b = new int[10][10];
// 行列
int r, c;
int Num = 1;
int i, j;
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
r = scanner.nextInt();
c = scanner.nextInt();
System.out.println();
System.out.printf("puzzle #%d:\n", Num++);
for (i = 0; i < r; i++) {
String string = scanner.next();
a[i] = string.toCharArray();
}
int n = 1;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
if ((i == 0 || j == 0 || a[i - 1][j] == '*' || a[i][j - 1] == '*') && a[i][j] != '*') {
b[i][j] = n++;
}
}
}
Translates.Across(r, c, b, a);
Translates.Down(r, c, b, a);
}
}
public static void Across(int r, int c, int[][] b, char[][] a) {
System.out.print("Across");
for (int i = 0; i < r; i++) {
boolean flag = true;
for (int j = 0; j < c; j++) {
if (a[i][j] != '*') {
if (flag) {
System.out.printf("\n %d.", b[i][j]);
System.out.printf("%c", a[i][j]);
flag = false;
} else {
System.out.printf("%c", a[i][j]);
}
}
if (a[i][j] == '*' || j == (c - 1)) {
flag = true;
}
}
}
System.out.println();
}
public static void Down(int r, int c, int[][] b, char[][] a) {
System.out.print("Down");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (b[i][j] != 0) {
System.out.printf("\n %d.", b[i][j]);
System.out.printf("%c", a[i][j]);
for (int x = i + 1, y = j; x < r; x++) {
if (a[x][y] != '*') {
System.out.printf("%c", a[x][y]);
b[x][y] = 0;
} else {
break;
}
}
}
}
}
System.out.println();
}
}