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0 0 6 6 6 6 6 6 6 0 0 0 0 0 0 0 0

Please help me solve this problem. Thanks!!!(java or javascript code example would be great)

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • 1
    what you have done so far to resolve this ? – Deepak Sharma Feb 28 '17 at 09:47
  • I make 2 for loops for row and col, then for each element, I check for directions to see if their neighbors are the same number, and then grouping them. but I always have a conflict doing that way. – Mickeal Kamps Feb 28 '17 at 20:32
  • @ThienN please don't completely change an answered question. When you can, ask a new one. – ChrisF Mar 01 '17 at 13:55

1 Answers1

1

First, change the value of 1 to -1, because you need to use 1 as flag.

Then you could iterate the elements and perform a check and if it has the flag -1, then change it to the actual value. Proceed with the element of the right and bottom.

If an element was found, increment value.

function test(array, i, j, value) {
    if (array[i] && array[i][j] === -1) {
        array[i][j] = value;
        test(array, i + 1, j, value);
        test(array, i, j + 1, value);
        return true;
    }
}

var data = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1], [0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1], [0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1], [0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1], [0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1], [0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1], [0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0], [0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]],
    value = 1;

data.forEach(function (a) {
    a.forEach(function (b, i, bb) {
        bb[i] = -b;
    });
});

data.forEach(function (a, i, aa) {
    a.forEach(function (b, j, bb) {
        test(aa, i, j, value) && value++;
    });
});

document.getElementById('out').innerHTML = data.map(function (a) { return a.join(' '); }).join('\n');
<pre id="out"></pre>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • I just update the question a little bit, could you please help me out with this? – Mickeal Kamps Feb 28 '17 at 21:26
  • @ThienN, actually you made a whole new question.. please revert it to the original question back - and ask a new one. – Nina Scholz Feb 28 '17 at 21:32
  • I dont have enough points to ask new question. I have to wait 3 more days to do that. do you know how I can get more points so I can ask more question? – Mickeal Kamps Feb 28 '17 at 21:43
  • it makes no sense to vandalize a question to a new purpose and make given answers invalid. so please have a look here (http://stackoverflow.com/help/someone-answers), regarding the question and answer and ask a new question with respect to [mcve]. – Nina Scholz Feb 28 '17 at 21:50