简体   繁体   English

为什么这个程序占用太多内存?

[英]Why does this program take too much memory?

I'm solving a task from a high school programming competition.我正在解决高中编程比赛中的一项任务。 Here's a short description:这是一个简短的描述:

We have a grid with height h and width w.我们有一个高度为 h 和宽度为 w 的网格。 The grid is filled with chars '#' and '.'.网格填充有字符“#”和“.”。 Octothorps represent land and dots represent water. Octothorps代表土地,圆点代表水。 It is guarranted that the first and last row and column of the grid will be all dots.保证网格的第一行和最后一行都是点。 Connected octothorps form islands which can have lakes and every lake can also have islands and these islands can have lakes and so on... Every island has a degree which is defined as the number of lakes which have to be crossed in order to reach it if one starts from the water on the edges of the grid (this water is regarded as the sea and is excluded from the number of lakes you have to cross to reach an island).连接起来的章鱼形成可以有湖泊的岛屿,每个湖泊也可以有岛屿,这些岛屿可以有湖泊等等......每个岛屿都有一个度数,该度数定义为到达它必须穿过的湖泊数量如果从网格边缘的水开始(此水被视为大海,不包括在到达岛屿所需的湖泊数量之外)。 Find the maximum degree amongst islands.找出岛屿之间的最大度数。

I'm running my program on a school server and I'm getting a memory limit exceeded error because the program allegedely takes more than 300 MiB of memory.我在学校服务器上运行我的程序,但我收到了超出内存限制的错误,因为据称该程序占用了超过 300 MiB 的内存。 Here is the program:这是程序:

#include <stdio.h>

char a[3010][3010];
int degree[3010][3010];
bool visited[3010][3010];
int h, w;

void searchSame(int x, int y, int d)
{
    degree[x][y] = d;

    if(x + 1 < h && a[x + 1][y] == a[x][y] && degree[x + 1][y] == -1)
        searchSame(x + 1, y, d);

    if(x - 1 >= 0 && a[x - 1][y] == a[x][y] && degree[x - 1][y] == -1)
        searchSame(x - 1, y, d);

    if(y + 1 < w && a[x][y + 1] == a[x][y] && degree[x][y + 1] == -1)
        searchSame(x, y + 1, d);

    if(y - 1 >= 0 && a[x][y - 1] == a[x][y] && degree[x][y - 1] == -1)
        searchSame(x, y - 1, d);
}

void search(int x, int y, int d)
{
    visited[x][y] = true;
    
    if(degree[x][y] == -1)
        searchSame(x,y,d);

    if(x + 1 < h && a[x + 1][y] == a[x][y] && !visited[x + 1][y])
        search(x + 1, y, d);
    else if(x + 1 < h && !visited[x + 1][y])
        search(x + 1, y, degree[x][y] + 1);

    if(x - 1 >= 0 && a[x - 1][y] == a[x][y] && !visited[x - 1][y])
        search(x - 1, y, d);
    else if(x - 1 >= 0 && !visited[x - 1][y])
        search(x - 1, y, degree[x][y] + 1);

    if(y + 1 < w && a[x][y + 1] == a[x][y] && !visited[x][y + 1])
        search(x, y + 1, d);
    else if(y + 1 < w && !visited[x][y + 1])
        search(x, y + 1, degree[x][y] + 1);

    if(y - 1 >= 0 && a[x][y - 1] == a[x][y] && !visited[x][y - 1])
        search(x, y - 1, d);
    else if(y - 1 >= 0 && !visited[x][y - 1])
        search(x, y - 1, degree[x][y] + 1);
}

int main()
{
    int mx = 0;
    scanf("%d %d",&h,&w);

    for(int i = 0; i < h; i++)
        scanf("%s",a[i]);

    for(int i = 0; i < h; i++)
    {
        for(int j = 0; j < w ; j++)
            degree[i][j] = -1;
    }   

    search(0,0,1);

    for(int i = 0; i < h; i++)
    {
        for(int j = 0; j < w ; j++)
        {
            if(degree[i][j] > mx)
                mx = degree[i][j];
        }
    }

    printf("%d",mx/2 - 1);
}

Values of h and w range from 3 to 3000. Why does this program take so much memory? h 和 w 的值在 3 到 3000 之间。为什么这个程序要占用这么多内存? Does it maybe have something to do with the recursive functions?它可能与递归函数有关吗? How can I improve my memory management?如何改进我的内存管理?

int degree[3010][3010]; reserves space for 3010 * 3010 int s, that is over 9 million.为 3010 * 3010 int s 预留空间,即超过 900 万。 If you are on a 32bit system, that means you need 9.06 million times 4 bytes = 36.2 MB just for this array;如果您使用的是 32 位系统,这意味着仅此数组就需要 906 万次 4 字节 = 36.2 MB; on a 64bit system (standard today), it needs 72.5 MB .在 64 位系统(今天的标准)上,它需要72.5 MB
Add the space for the other two, and you'll see why the program needs that much space.为另外两个添加空间,您就会明白为什么程序需要那么多空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM