二次元配列でランレングスエンコーディングを理解しようとしています。サイズ x サイズのボードに 0 と 1 をランダムに配置しました。次に、私のプログラムはジグザグ ウォーク (開始位置 = 右上隅) を実行して、そのパターンで 0 と 1 を読み取ります。以下に示すように、それは私にとってはうまくいきます。ただし、連続する 0 または 1 を読み取り、毎回出現する回数を数えるには助けが必要です。次に例を示します。
/* n = 4 (i.e. 4 x 4 board)
1010
1010
0001
1000
Run-length coding on the zigzag path:
(0,2)
(1,1)
(0,1)
(1,2)
(0,3)
(1,2)
(0,4)
(1,1) */
これが私がこれまでに持っているものです。
void runLengthCoding()
{
int flag = 1; // alternate between one and negative one depending on direction.
//2*maxsize-1 is the number of segments.
for(int i = 2 * maxSize - 1; i >= 0; i--) //outer for loop goes through the segments. #of segments
{
//determine the starting element.
int r, c;
if (flag == -1)// if(i%2==1)
{
if(i > maxSize)
r = i - maxSize - 1;
else
r = maxSize - 1;
}
else
{
if(i >= maxSize)
r = 0;
else
r = maxSize - i;
}
c = i - maxSize + r;
while(r >=0 && r <= maxSize -1 && c >= 0 && c <= maxSize - 1)
{
System.out.print(A[r][c] + " ");
int cnt = 0;
if (A[r][c] == 0)
if(flag == 1)
{
r++;
c++;
}
else
{
r--;
c--;
}
}
//change the moving direction
flag = -flag;
System.out.println();
}
}
// print the run-length coding result, i.e., content of rlc[][]
void printCodingResult()
{
System.out.println("Run-length coding on the zigzag path: ");
}
rlc[][] を実行するには、A[r][c] が 0 から 1 に、またはその逆に変化するたびに、カウントを記録してリセットすることを考えています。しかし、どうすればそれを組み込むことができますか。rlc[][] はそれをどのように覚えているのでしょうか? 例からわかるように、rlc[][] は 2 つの列の形式で表示されます (1 つは 0 または 1、秒はカウント)。どんなアイデアでも大歓迎です。ありがとう。