依旧是非常复杂的输入输出
输出的数字序号要%3d的格式
每组输出间需有空行,最后不能有空行
纵向输出时打印的顺序是按之前的计数,我排了个序
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class UVA232 {
static class st{
String s;
int count;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int h=1;
while(true){
int r=sc.nextInt();
if(r==0)break;
if(h!=1) System.out.println();
System.out.println("puzzle #"+h+":");
h++;
int c=sc.nextInt();
char[][] map=new char[r][c];
int[][] count=new int[r][c];
int k=1;
for (int i = 0; i < r; i++) {
map[i]=sc.next().toCharArray();
for (int j = 0; j < c; j++) {
if(map[i][j]!='*'&&(i==0||map[i-1][j]=='*'||j==0||map[i][j-1]=='*')){
count[i][j]=k++;
}
}
}
System.out.println("Across");
for (int i = 0; i < r; i++) {
int j=0;
while(j<=(c-1)){
if(count[i][j]!=0){
System.out.print(String.format("%3d",count[i][j]));
System.out.print(".");
while(j<=(c-1)&&map[i][j]!='*'){
System.out.print(map[i][j]);
j++;
}
System.out.println();
}
else j++;
}
}
System.out.println("Down");
st[] stcount=new st[100];
for (int i = 0; i < 100; i++) {
stcount[i]=new st();
}
int q=0;
for (int j = 0; j < c; j++) {
int i=0;
while(i<=(r-1)){
if(count[i][j]!=0){
StringBuffer sb=new StringBuffer();
stcount[q].count=count[i][j];
while(i<=(r-1)&&map[i][j]!='*'){
sb.append(map[i][j]);
i++;
}
stcount[q].s=sb.toString();
q++;
}
else i++;
}
}
Comparator<st> cmp=new Comparator<st>() {
@Override
public int compare(st o1, st o2) {
return o1.count-o2.count;
}
};
Arrays.sort(stcount,0,q,cmp);
for (int i = 0; i < q; i++) {
System.out.print(String.format("%3d",stcount[i].count));
System.out.print(".");
System.out.println(stcount[i].s);
}
}
}
}