【问题标题】:the largest rectangle in a histogram直方图中最大的矩形
【发布时间】:2018-04-01 20:30:06
【问题描述】:

我正在尝试编写代码来计算直方图中最大矩形的面积及其顶部坐标。 为此,我正在使用堆栈,以便在每个条的情况下将较小的条向左和向右移动。

根据 GeeksForGeeks 文章中的指导方针,我正在执行以下步骤:

"1) 创建一个空栈。

2) 从第一个柱开始,对每个柱“hist[i]”执行以下操作,其中“i”从 0 到 n-1 变化。 ……a) 如果栈为空或 hist[i] 高于栈顶的条,则将‘i’压入栈。 ……b) 如果该条小于栈顶,则在栈顶大于栈顶时继续移除栈顶。让移除的柱为 hist[tp]。以 hist[tp] 作为最小条计算矩形的面积。对于 hist[tp],“左索引”是堆栈中的上一个(在 tp 之前)项,“右索引”是“i”(当前索引)。

3) 如果堆栈不为空,则一个接一个地从堆栈中移除所有柱,并对每个移除的柱执行步骤 2.b。"

但是我的代码给了我一些任意大的值,怀疑是地址值。 请帮我调试。

这是我的代码:`

#include <stdio.h>
#include <stdlib.h>

void maxRectangle(int hist[], int n);
void push(int stack[],int item,int *top,int n);
int pop(int stack[],int *top);
int peek(int stack[],int *top);
int isEmpty(int top);
int isFull(int top,int n);
void display(int stack[],int top);
int main()
{

    int n,i;
    printf("How many bars do you want to enter in the histogram?\n");
    scanf("%d",&n);

    int hist[n];
    printf("enter the hights of the consecutive bars\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&hist[i]);
    }

    maxRectangle(hist,n);
    return 0;
}

void maxRectangle(int hist[], int n)
{

    int top=-1;
    int i;
    int x1,y1,x2,y2;
    int max_area,idx, top_area;
    int stack[n];
    int bar=stack[top];

    while(i<n)
    {
    if(isEmpty(top)||(hist[bar]<hist[i]))
    {
        push(stack,i,&top,n);i++;
    }
    else
    {
        idx=peek(stack,&top); //smaller idx to the right
        pop(stack,&top); //bar idx  to compute the area for
        top_area= hist[idx]*(isEmpty(top)?i:i-peek(stack,&top)-1); //top(stack)is the smaller bar to the left
        if(top_area<max_area)
        {
            max_area=top_area;
            x1=(peek(stack,&top)+1);
            x2=idx+1;
            y1=y2=hist[idx];
        }
    }



    }

    printf("the largest area is %d, the top left coordinate is (%d,%d) and top-right coordinate is (%d,%d)\n",max_area,x1,y1,x2,y2);
}
void push(int stack[],int item,int *top,int n)
{
    if(isFull(*top,n))
    {
        printf("stack overflow!!\n");
        return;
    }
    *top=*top+1;
    stack[*top]=item;
}
int pop(int stack[],int *top)
{

    int item;
    if(isEmpty(*top))
    {
        printf("stack underflow!\n");
        exit(1);
    }
    item=stack[*top];
    *top=*top-1;
    return item;

}
int peek(int stack[],int *top)
{
    if(isEmpty(*top))
    {
        printf("stack underflow!");
        exit(1);
    }
    return stack[*top];
}
int isEmpty(int top)
{
    if(top==-1) return 1;
    else return 0;
}
int isFull(int top,int n)
{
    if(top==(n-1)) return 1;
    else return 0;
}

void display(int stack[],int top)
{
    int i;
    printf("stack elements are:\n\n");
    for(i=top;i>=0;i++)
    {
        printf("%d\n",stack[i]);
    }
    printf("\n");
}

`

【问题讨论】:

  • stack[-1] 不会有好的结局。您在int bar=stack[top] 执行此操作,其中top == -1
  • 您在使用调试器时看到了什么?

标签: c arrays function stack


【解决方案1】:

这里有一些东西。

  1. int bar = stack[top]; 不好,因为 top = -1
  2. 您的大多数变量都未初始化。这就是它们是奇数的原因。
  3. isEmpty(top)||hist[bar]&lt;hist[i] 将始终返回 true,因此您只能推送。
  4. if(top_area&lt;max_area) 如果你想要最大的区域,则向后

还有其他较小的问题,但如果您解决了这些问题,您的程序将正常运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-17
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2013-03-05
    • 2017-01-26
    相关资源
    最近更新 更多