On 16.07.2017 19:10, Timon Gehr wrote:
...

(This works even if there are * at the border.)

Well, not really. :)

Version that actually works if there are * at the border:

import std.stdio, std.range, std.algorithm, std.array;
char[][] arr;
int componentSize(int row,int col){
    if(row>=arr.length||col>=arr[row].length||arr[row][col]!='*')
        return 0;
    arr[row][col]='x';
    return 1+cartesianProduct(iota(row-1,row+2),iota(col-1,col+2))
        .map!(a=>componentSize(a.expand)).sum;
}
void main (){
    arr=["**xxxx*",
         "xxxx*xx",
         "xx**xxx",
         "xxx*x**",
         "**xxxxx"].map!dup.array;

cartesianProduct(iota(cast(int)arr.length),iota(cast(int)arr[0].length))
        .filter!(a=>arr[a[0]][a[1]]=='*')
        .each!(a=>writeln(componentSize(a.expand)));
}

Reply via email to