CJam,73 69 62 55字节
更新:新算法。更短,更大的改进空间
qN/5ff*{{[{_@_@<{I'0t}*\}*]W%}%z}4fI{):X-'-X~X'.??}f%N*
怎么运行的
逻辑类似于下面的算法,但是在这里我不是在一次迭代中检查所有4个邻居。取而代之的是,我使用一种较小的方法来双向遍历所有行和列。涉及的步骤如下:
- 将每个字符转换成5个字符的集合。在迭代时,将修改前4个字符以判断它们是否大于该行中的相邻单元格。最后一个是出于比较目的。
- 在每行上迭代并在每行上减少。在减少的同时,我有两个5字符串。我知道哪种迭代是[0表示正常行,1表示反转列,2表示行反转,3表示列正常]我更新了i了前5 个字符串中的第个字符,如果小于第二个则将其设为0 。
- 在所有4次迭代之后,如果所有5个字符相同且非零,则为局部最大值。我映射了所有5个字符串,然后将它们转换为一位
.
或-
。
这是在少量输入上运行的示例:
7898
8787
7676
6565
第一步之后:
["77777" "88888" "99999" "88888"
"88888" "77777" "88888" "77777"
"77777" "66666" "77777" "66666"
"66666" "55555" "66666" "55555"]
第二步之后:
["00777" "08888" "99999" "88088"
"88888" "07007" "88808" "77007"
"77707" "06006" "77707" "66006"
"66606" "05005" "66606" "55005"]
最后映射到单个字符后,最终输出:
--9-
8---
----
----
代码说明:
qN/5ff* "Split the input on new line and convert each character";
"to string of 5 of those characters.";
{{[{ }*]W%}%z}4fI "This code block runs 4 times. In each iteration, it";
"maps over each row/column and then for each of them,";
"It reduce over all elements of the row/column";
"Using combination of W% and z ensures that both rows and";
"columns are covered and in both directions while reducing";
_@_@ "Take a copy of last two elements while reducing over";
< "If the last element is bigger than second last:";
{I'0t}*\ "Convert the Ith character of the 5 char string of"
"second last element to 0";
"We don't have to compare Ith character of last two 5 char";
"string as the smaller one will be having more leading";
"0 anyways. This saves 4 bytes while comparing elements";
{):X-'-X~X'.??}f%N* "This part of code converts the 5 char back to single char";
):X "Remove the last character and store in X. This last char";
"was not touched in the prev. loop, so is the original char";
- "Subtract X from remaining 4 char. If string is not empty";
"then it means that it was not all same characters";
"In other words, this character was smaller then neighbors";
'- ? "If non-empty, then replace with - else ...";
X~X'.? "if int(X) is zero, put . else put X";
f%N* "The mapping code block was run for each row and then";
"The rows are joined by newline.";
在这里尝试
较旧的方法
qN/~_,):L0s*]0s*:Q_,{QI=:A[W1LL~)]If+Qf=$W=<'-A?A~\'.?I\t}fIL/W<Wf<N*
怎么运行的
逻辑很简单,遍历网格并查看当前值是否大于或等于其余四个邻居-上,下,左和右。然后根据上述规则转换当前值,如果该值等于0,则将其设置为“。”。。
代码说明
qN/~_,):L0s*]0s*:Q "This part of code pads the grid with 0s";
qN/~ "Read the input, split on new lines and unwrap the arrays";
_,):L "Copy the last row, taken length, increment and store in L";
0s* "Get L length 0 string";
]0s* "Wrap everything in an array and join the rows by 0";
:Q "Store this final single string in Q";
_,{ ... }fI "Copy Q and take length. For I in 0..length, execute block";
QI=:A "Get the I'th element from Q and store in A";
[WiLL~)]If+ "This creates indexes of all 4 neighboring cells to the Ith cell";
Qf= "Get all 4 values on the above 4 indexes";
$W= "Sort and get the maximum value";
<'-A? "If the current value is not the largest, convert it to -";
A~\'.? "If current value is 0, convert it to .";
I\t "Update the current value back in the string";
{ ... }fIL/ "After the loop, split the resulting string into chunks of L";
W<Wf< "Remove last row and last column";
N* "Join by new line and auto print";
在这里在线尝试