Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Easier to draw than to explain...</p> <pre><code>2 2 2 2 1 =&gt; A A A A B =&gt; (A: 4, B: 1) 1 2 2 2 1 =&gt; C A A A B =&gt; (A: 3 + 4, B: 1 + 1, C: 1) 3 3 2 3 2 =&gt; D D A E F =&gt; (A: 1 + 7, B: 2, C: 1, D: 2, E: 1, F: 1) 3 1 3 3 1 =&gt; D G E E G =&gt; (A: 8, B: 2, C: 1, D: 2 + 1, E: 2 + 1, F: 1, G: 1) 1 1 2 3 1 =&gt; ... 1 3 1 3 3 =&gt; ... </code></pre> <p><strong>update</strong>:</p> <p>And now, with some real code:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;stdio.h&gt; #define ROWS 6 #define COLS 5 unsigned char eles[ROWS][COLS] = { { 2, 2, 2, 2, 1 }, { 1, 2, 2, 2, 1 }, { 3, 3, 2, 3, 2 }, { 3, 1, 3, 3, 1 }, { 1, 1, 2, 3, 1 }, { 1, 3, 1, 3, 3 } }; struct zone { int acu; int row, col; int refs; }; typedef struct zone zone; zone * new_zone(int row, int col) { zone *z = (zone *)malloc(sizeof(zone)); z-&gt;col = col; z-&gt;row = row; z-&gt;refs = 1; z-&gt;acu = 0; } void croak (const char *str) { fprintf(stderr, "error: %s\n", str); exit(1); } void free_zone(zone *z) { if (z-&gt;refs != 0) croak("free_zone: reference count is not cero"); free(z); } zone * ref_zone(zone *z) { z-&gt;refs++; return z; } void unref_zone(zone *z) { z-&gt;refs--; if (!z-&gt;refs) free_zone(z); } int main() { zone *last[COLS]; zone *current[COLS]; zone *best = new_zone(0, 0); int i, j; memset(last, 0, sizeof(last)); for (j = 0; j &lt; ROWS; j++) { for (i = 0; i &lt; COLS; i++) { unsigned int ele = eles[j][i]; zone *z; /* printf("analyzing ele: %d at row %d, col: %d\n", ele, j, i); */ if (i &amp;&amp; (ele == eles[j][i-1])) { /* printf(" equal to left element\n"); */ z = ref_zone(current[i-1]); if (j &amp;&amp; (ele == eles[j-1][i])) { zone *z1 = last[i]; /* printf(" equal to upper element\n"); */ if (z != z1) { int k; /* printf(" collapsing zone %p\n", z1); */ z-&gt;acu += z1-&gt;acu; for (k = 0; k &lt; COLS; k++) { if (last[k] == z1) { last[k] = ref_zone(z); unref_zone(z1); } } for (k = 0; k &lt; i; k++) { if (current[k] == z1) { current[k] = ref_zone(z); unref_zone(z1); } } } } } else if (j &amp;&amp; (ele == eles[j-1][i])) { /* printf(" equal to upper element\n"); */ z = ref_zone(last[i]); } else { /* printf(" new element\n"); */ z = new_zone(j, i); } z-&gt;acu++; current[i] = z; /* printf(" element zone: %p\n", z); */ } for (i = 0; i &lt; COLS; i++) { if (j) unref_zone(last[i]); last[i] = current[i]; if (best-&gt;acu &lt; current[i]-&gt;acu) { unref_zone(best); best = ref_zone(current[i]); /* printf("best zone changed to %p at row; %d, col: %d, acu: %d\n", best, best-&gt;row, best-&gt;col, best-&gt;acu); */ } } } printf("best zone is at row: %d, col: %d, ele: %d, size: %d\n", best-&gt;row, best-&gt;col, eles[best-&gt;row][best-&gt;col], best-&gt;acu); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload