フィールドで最高のスコア


18

前書き

フィールドを、文字とのみで満たされた長方形-とし[0-9]ます。フィールドの例は次のとおりです。

11-011123
111-010--
0010---01
111-01234

このフィールドは、3つの小さな領域に分割されていることがわかります。

ここに画像の説明を入力してください

より小さなエリアのスコアを計算するには、すべての数値を加算します。例えば:

11
111
0010
111

1 + 1 + 1 + 1 + 1 + 0 + 0 + 1 + 0 + 1 + 1 + 1 = 9

このエリアの合計スコアは9です。次に、2番目の領域についても同じことを行います。

   011123
    010

0 + 1 + 1 + 1 + 2 + 3 + 0 + 1 + 0 = 9

合計スコアも9です。次に、最後の領域を調べる必要があります。

       01
    01234

0 + 1 + 0 + 1 + 2 + 3 + 4 = 11

これの合計スコアは11です。フィールドの最高スコアは11なので、これが出力する必要があるものです。

タスク

フィールド(2D文字列、配列などの形式)を指定すると、そのフィールドで最高のスコアが出力されます。指定されたフィールドには常に少なくとも1桁が含まれると想定できます。これはであるため、バイト数が最小の提出が勝ちです!

テストケース

テストケース1:

Input:
1

Output:
1

テストケース2:

Input:
1-1-1-1
-1-1-1-
2-1-1-1
-1-1-1-

Output:
2

テストケース3:

Input:
12-45-
4-65-9
87-654
12-487
45----
684764

Output:
69

テストケース4:

Input:
111-12
------
21--10

Output:
3

1
うわー...素敵な挑戦。
R. Kap

「0010 --- 01」は、代わ​​りに[[0010]、 ""、 ""、 "01"]ではありませんか?
ヴェン

「111-01234」も、なぜそうではないのです["111", "01234"]か?
ヴェン

分かりません。私-は領域を分離したと思いましたか?「エリアを定義するもの」の部分をより明確にしてください。
ヴェン

それを説明するために挑戦を言い換えることができますか?
2016

回答:


3

MATL54 51 49バイト

n:"G~1@(2Y6Z+leG45>1e*5M@)*]vtz:"otY*g]G48-X:*sX>

入力はMATL(AB)形式の2D char配列で;、行区切り記号として使用されます。例とテストケースの入力はそれぞれ次のとおりです。

['11-011123';'111-010--';'0010---01';'111-01234']
['1']
['1-1-1-1';'-1-1-1-';'2-1-1-1';'-1-1-1-']
['12-45-';'4-65-9';'87-654';'12-487';'45----';'684764']
['111-12';'------';'21--10']

オンラインでお試しください!

説明

これは、「接続されている」という関係によって定義されたグラフの隣接行列を構築することにより機能します。例として、3×4フィールドを考えます

52-4
15-8
3-72

2D配列のエントリは、(列優先)線形インデックスを使用してMATLで簡単に記述できます。3×4の場合、各エントリの線形インデックスは次のように与えられます

1  4  7 10
2  5  8 11
3  6  9 12

隣接行列は、行列乗算を使用して段階的に構築されます。最初のステップでは、すぐ隣の人が考慮されます。たとえば、3のインデックスが付けられたポイントは、それ自体とインデックス2のポイントの隣にあります。このポイントにはフィールドに応じた番号が含まれていないため、6の隣ではありません。この例では、関係「イミディエート-ネイバー」の隣接行列は次のように与えられる12×12行列Lです。

1 1 0 1 0 0 0 0 0 0 0 0
1 1 1 0 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0
1 0 0 1 1 0 0 0 0 0 0 0
0 1 0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 1 0 1 1

(列3の1行2と3に値があることがわかります。)この行列は常に対称であり、その対角線にはを1含まないポイントの値があります-

次のステップは、「最大で1つのポイントで接続され」関係の隣接行列です。これを取得するには、L自体を乗算し、非ゼロエントリをに設定するだけで十分1です。一般に、「何らかのパスで接続されている」関係の隣接行列MはLを可能な最大パス長を表す指数(行列の意味)に上げることで取得されます。最大パス長の上限は、Lの非ゼロエントリの数です。

行列の力を直接計算すると、大きな数がすぐに発生するため、オーバーフローが発生する可能性があります。したがって、同じ行列を徐々に乗算して、各ステップの後にゼロ以外のエントリを1に変換して、大きな数が蓄積しないようにすることをお勧めします。

IMは、点で(任意の経路によって)接続されている点を表す私は。これで、レベルフィールドを線形順序で列ベクトルcに縮小できます。各エントリには、対応する数値またはの未定義の値が含まれ-ます。したがって、この場合、c

5
1
3
2
5
-
-
-
7
4
8
2

各列Mutiplying Mをすることにより、Cの要素ごと、各列の和を計算する各点について、得られるI、領域点の合計スコアは、iが属します。エリアは、相互に接続されているすべてのポイントによって定義されます。多くの列で同じ結果が得られることに注意してください。つまり、ポイントijが(同じ領域に属する)接続されている場合、列ijは同じ合計を返します。最終結果は、それらの合計の最大値です。

        % Implicitly take input: 2D char array
n:      % Range [1,...,N], where N is number of entries in the input
"       % For loop. Each iteration builds a row of matrix L
  G     %   Push input again
  ~     %   Logical negate: transform into matrix of zeros
  1     %   Push 1, to be written into a matrix entry
  @     %   Iteration index. Ranges from 1 to N
  (     %   Write that 1 into the N-th entry (linear order)
  2Y6   %   Push array [0 1 0; 1 1 1; 0 1 0]: mask of immediate neighbours
  Z+    %   Convolve and keep same-size result
  le    %   Linearize into row array
  G45>  %   Array of same size as the input that contains 1 for numbers, 0 for '-'
  1e    %   Linearize into row array
  *     %   Multiply element-wise
  5M    %   Push last array again: 1 for numbers, 0 for '-'
  @)    %   Get 0 or 1 value of that array corresponding to current iteration
  *     %   Multiply. This is to give a row of zeros for non-numbers
]       % End. We have all rows of L in the stack
v       % Concatenate all rows into a matrix: L.
tz:     % Duplicate. Range [1,...,K], where K is the number of nonzeros in L
"       % For loop. Repear K times. This loop computes the 0/1 matrix power
  o     %   Convert matrix entries to double
  tY*   %   Duplicate and matrix-multiply
  g     %   Convert to logical values, that is, nonzero values become 1
]       % End. We have matrix M
G48-    % Convert input chars to the corresponding numbers by subtractig 48
X:      % Linearize into column array. This is vector c
*       % Element-wise multiplication with broadcast (implicit repetition)
s       % Sum of each column. Gives a row array
X>      % Maximum of that row array
        % Implicitly display

3

JavaScript(ES6)、157バイト

s=>[...o=s].map((n,i)=>o=n<'.'|(a=[...s]).map(_=>a.map((c,j)=>c>'-'&c<10&(a[j+1]|a[j-1]|a[j+l]|a[j-l])>90?a[n-=-c,j]=99:0),a[i]=99)|o>n?o:n,l=~s.search`
`)|o

説明

入力フィールドを文字列として受け取ります。フィールド内の各数値について、エリア内のすべての数値を合計します。これは、フィールドの各数値を複数回繰り返し、隣接するセルに以前にカウントされた数値が含まれている場合にスコアに数値を追加することによりこれを行います。領域の一部であるカウントされた数字は、それらが再びカウントされないように99に設定することによって表されます。最高スコアを数値として出力します。

var solution =

s=>
  [...o=s].map((n,i)=>o=n<'.'|             // for each number on the field
                                           // n = area score
      (a=[...s])                           // a = array of each field character
      .map(_=>                             // loop to ensure whole area is found
        a.map((c,j)=>                      // for each cell c at index j
          c>'-'&c<10&                      // if the current cell is a number
          (a[j+1]|a[j-1]|a[j+l]|a[j-l])>90 // and an adjacent cells is in the area
          ?a[n-=-c,j]=99:0                 // add the cell to the area
        ),                                 // and the number to the score
        a[i]=99                            // mark the starting cell as counted
      )
      |o>n?o:n,                            // o = output (max of o and n)
    l=~s.search`
`                                          // l = line length of field
  )
  |o                                       // return o
<textarea id="input" rows="6" cols="40">12-45-
4-65-9
87-654
12-487
45----
684764</textarea><br />
<button onclick="result.textContent=solution(input.value)">Go</button>
<pre id="result"></pre>


2

Pyth、93バイト

A,hlh.zjJ\-.zKsm?qdJd\#HD'b=KXKbJR+i@HbTsm?&&gd0<dlKq@Kd\#'d0[tbhb-bG+bG;Wh=NxK\#=+Y'N)h.MZY

オンラインでお試しください!

使い方


最初のステップ:入力を読む

A,hlh.zjJ\-.zKsm?qdJd\#H
A,                           Assign the following to G and H:
  hlh.z                          G = increment(length(first(all_input())))
       jJ\-.z                    H = join(J="-",all_input())
                m       H    for d in H:
                 ?qdJ            if equal(d,J):
                     d               add d to the list
                                 else:
                      \#             add "#" to the list
                             end
               s             sum the list
              K              assign to K

Sample input:
11-011123
111-010--
0010---01
111-01234

G = 10
H = "11-011123-111-010---0010---01-111-01234" (note the extra dashes connecting each line)
J = "-"
K = "##-######-###-###---####---##-###-#####"

2番目のステップ:1つの領域を評価する関数を定義します

D'b=KXKbJR+i@HbTsm?&&gd0<dlKq@Kd\#'d0[tbhb-bG+bG;
D'b                                             ;  def quote(b):
   =KXKbJ                                              K[b] = J
         R+                                            return the sum of A and B, where:
           i@HbT                                         A = to_integer(H[b],10)

                 m                   [tbhb-bG+bG         for d in {dec(b), inc(b), minus(b,G), add(b,G)}:
                  ?&&                                      if .... and ........ and ............... :
                     gd0                                      d>=0
                        <dlK                                           d<len(K)
                            q@Kd\#                                                  equal(K[d],"#")
                                  'd                           add quote(d) to temp_list
                                                           else:
                                    0                          add 0 to temp_list
                s                                        B = sum(temp_list)

Basically, this function (quote) is given a starting
point (b), and then recursively find its neighbour and
add their values to the output.

3番目のステップ:すべての領域を読み取り、必要な最大値を見つけます

Wh=NxK\#=+Y'N)h.MZY
Wh=NxK\#     )         while inc(N=find(K,"#")):   --while K still has "#"
        =+Y'N              Y+= quote(N)
               .MZY    find the maximum of Y,
              h        then print the first.
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.