CJam、180 ... 133 101 ... 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)の反復の終わりに、すべての硬い岩がスペースで表されることが保証されます。
こちらからオンラインでお試しください