can counting contiguous regions in a bitmap be improved over O(r * c)?

前端 未结 5 1089
南笙
南笙 2021-02-15 16:52

You are given an image of a surface photographed by a satellite.The image is a bitmap where water is marked by \'.\' and land is marked by \'*\'. Adjacent group of

5条回答
  •  暖寄归人
    2021-02-15 17:15

    Without any apriori knowledge about the nature of islands the algorithm can not be made more efficient than O(n) in time, however memory-wise your algo can be improved. The visited array is simply redundant. Here is a quick attempt (pardon the usage of ASCII artihmetics - not so readable, but quicker to code)

    #include 
    #define COLS 12
    
    
    int main()
    {
        char map[][COLS] = {
                "*..........",
                "**........*",
                "...........",
                "...*.......",
                "*........*.",
                "..........*"               
                };
        int rows = sizeof(map)/sizeof(map[0]);
        int i, j;  
    
        int main_count = 0;
    
        if(map[0][0] == '*') {
            main_count++;
        }
        for(j=0; j

提交回复
热议问题