Java 7,927 924 923字节
import java.util.*;int l,k;char[][]z;Set p=new HashSet();String c(String[]a){int x=0,y=0,n,t,u,v,w=t=u=v=-1;l=a.length;k=a[0].length();z=new char[l][k];for(String s:a){for(char c:s.toCharArray())z[x][y++]=c;}x++;y=0;}for(x=0;x<l;x++)for(y=0;y<k;y++){n=0;if(z[x][y]>32){if(x<1|(x>0&&z[x-1][y]<33))n++;if(y<1|(y>0&&z[x][y-1]<33))n++;if(x>l-2|(x<l-1&&z[x+1][y]<33))n++;if(y>k-2|(y<k-1&&z[x][y+1]<33))n++;}if(n>2&t<0){t=x;u=y;}if(n>2&t>v){v=x;w=y;}}if(v+w>t+u){p(t,u);return n(""+z[t][u],t,u);}p(v,w);return n(""+z[v][w],v,w);}String n(String r,int x,int y){int a,b;if(x>0&&z[a=x-1][b=y]>32&q(a,b)){p(a,b);return n(r+z[a][b],a,b);}if(y>0&&z[a=x][b=y-1]>32&q(a,b)){p(a,b);return n(r+z[a][b],a,b);}if(x<l-1&&z[a=x+1][b=y]>32&q(a,b)){p(a,b);return n(r+z[a][b],a,b);}if(y<k-1&&z[a=x][b=y+1]>32&q(a,b)){p(a,b);return n(r+z[a][b],a,b);}return r;}boolean q(int x,int y){return!p.contains(x+","+y);}void p(int x,int y){p.add(x+","+y);}
好的,这花了一段时间。在某些编程语言中,数组x和y是否在2D数组的边界之外并不重要,但是对于Java,它将抛出 ArrayIndexOutOfBoundsExceptions
,因此必须检查所有内容。
我首先确定起点,然后使用递归方法从那里构建字符串。另外,我使用一个列表来跟踪已经遇到的协调,因此它不会前后循环(导致StackOverflowException)。
这可能是我到目前为止发布的最长答案,但是尽管某些部分可以打高尔夫球,但我认为Java中的挑战不会那么短。Java只是不适合遵循网格中的路径。尽管如此,找出它还是一个有趣的挑战。:)
非高尔夫球和测试用例:
在这里尝试。
import java.util.*;
class M{
static int l,
k;
static char[][] z;
static Set p = new HashSet();
static String c(String[] a){
int x=0,
y=0,
n,
t,
u,
v,
w = t = u = v = -1;
l = a.length;
k = a[0].length();
z = new char[l][k];
for(String s:a){
for(char c:s.toCharArray()){
z[x][y++] = c;
}
x++;
y = 0;
}
for(x=0; x<l; x++){
for(y=0; y<k; y++){
n = 0;
if(z[x][y] > 32){ // [x,y] is not a space
if(x < 1 | (x > 0 && z[x-1][y] < 33)){
n++;
}
if(y < 1 | (y > 0 && z[x][y-1] < 33)){
n++;
}
if(x > l-2 | (x < l-1 && z[x+1][y] < 33)){
n++;
}
if(y > k-2 | (y < k-1 && z[x][y+1] < 33)){
n++;
}
}
if(n > 2 & t < 0){
t = x;
u = y;
}
if(n > 2 & t > v){
v = x;
w = y;
}
}
}
if(v+w > t+u){
p(t, u);
return n(""+z[t][u], t, u);
}
p(v, w);
return n(""+z[v][w], v, w);
}
static String n(String r, int x, int y){
int a,b;
if(x > 0 && z[a=x-1][b=y] > 32 & q(a,b)){
p(a, b);
return n(r+z[a][b], a, b);
}
if(y > 0 && z[a=x][b=y-1] > 32 & q(a,b)){
p(a, b);
return n(r+z[a][b], a, b);
}
if(x < l-1 && z[a=x+1][b=y] > 32 & q(a,b)){
p(a, b);
return n(r+z[a][b], a, b);
}
if(y < k-1 && z[a=x][b=y+1] > 32 & q(a, b)){
p(a, b);
return n(r+z[a][b], a, b);
}
return r;
}
static boolean q(int x, int y){
return !p.contains(x+","+y);
}
static void p(int x, int y){
p.add(x+","+y);
}
public static void main(String[] a){
System.out.println(c(new String[]{ "Hel ",
" l rin",
" o,IAmASt g",
" S",
" !ekan" }));
p = new HashSet();
System.out.println(c(new String[]{ "Python" }));
p = new HashSet();
System.out.println(c(new String[]{ "P ngPu Code ",
"r i z d G",
"o m z n o",
"gram lesA lf" }));
p = new HashSet();
System.out.println(c(new String[]{ " ~ zyx tsr XWVUTSR",
" }|{ wvu q Y Q",
"! p Z `ab P",
"\"#$ 6789:; o [ _ c O",
" % 5 < n \\]^ d N",
"('& 432 = m e M",
") 1 > lkjihgf L",
"*+,-./0 ? K",
" @ABCDEFGHIJ" }));
p = new HashSet();
System.out.println(c(new String[]{ " tSyrep ",
" r p ",
" in Sli ",
" g Sile",
" Snakes n",
"Ser ylt",
"a eh ilS ",
"fe w t ",
" emo h ",
" Sre " }));
}
}
输出:
Hello,IAmAStringSnake!
Python
ProgrammingPuzzlesAndCodeGolf
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
SlipperyStringSnakesSilentlySlitherSomewhereSafe