CJam,180 ... 133101 ... 94 90 87字节
qN/~'#/S*_,):L;]N*_,_,*{:A1$='#={[W1LL~)]Af+{W>},1$f=S&,{ASct}*}*}/N/z{S/{$W%}%'#*}%zN*
肯定有很多打高尔夫球的可能,但是我想在完全使用它之后首先发布它。
快看 没有滚动条!
取岩石格(由向上.
和#
从STDIN没有尾随换行),并打印输出到STDOUT
更新:使用低效但较短的局部洪水填充来确定坚硬的岩石。
更新2:更改了使岩石掉落的算法。现在要短得多!
更新3:做了几次小的优化,最后我将字节数减少到原始代码的一半!
运作方式:
qN/~'#/S*_,):L;]N* "Preparations";
qN/~ "Read the input, split by new line and expand the array";
'#/S* "In the last row, replace # by space";
_,):L "Copy the last row and store length + 1 in L";
;]N* "Pop the length, wrap everything in array and join by \n";
_,_,*{ ... }/ "Flood fill";
_, "Copy the array and calculate its length";
_, "Copy the length and calculate [0 - length] array";
* "Repeat the above array, length times";
{ ... }/ "Run the code block on each element of the array";
:A1$='#={ ... }* "Process only #";
:A1$ "Store the number in A and copy the input array to stack";
= "Get Ath index element from input array";
'#={ ... }* "Run the code block if Ath element equals #";
[W1LL~)]Af+{W>},1$f=S&,{ASct}* "Flood fill spaces";
[W1LL~)]Af+ "Get the indexes of the 4 elements on the cross formed by"
"the Ath index";
{W>}, "Filter out the negative values";
1$f= "For each of the index, get the char from input string";
S&, "Check if space is one of the 4 chars from above step";
{ }* "Run the code block if space is present";
ASct "Make the Ath character of input string as space";
N/z{S/{$W%}%'#*}%zN* "Let the rocks fall";
N/z "Split the resultant string by newlines and"
"transpose the matrix";
{ }% "Run the code block for each row (column of original)";
S/{ }% "Split by space and run the code block for each part";
$W% "Sort and reverse. This makes # come down and . to go up";
'#* "Join by 3, effectively replacing back spaces with #";
zN* "Transpose to get back final matrix and join by newline";
对于泛洪,我们遍历整个网格长度(网格)时间。在每次迭代中,我们保证将至少1个#
直接与一个空间接触的空间转换为
(空间)。这里的空间代表着坚硬的岩石群。因此,在length(grid)迭代结束时,我们保证将所有坚硬的岩石用空间表示。
在这里在线尝试