如何在数组/矩阵中获得1的最长行

时间:2013-12-11 19:50:25

标签: c arrays

我的任务是在数组中找到1的“最长行”。水平和垂直。 数组仅由0和1组成,例如如下所示:

4 4
0 1 1 1
0 1 0 1
0 1 1 0
1 0 1 0

输出应打印“开始”1的[i] [j]和“结束”1的[i] [j],因此水平应为[1] [0] [3] [0]。 / p>

我正在使用我的getcolor()函数在[i] [j] spot上获取值。

我正在为WAAAAY考虑这个问题,花了差不多整整一周的时间。我有一些想法,但都没有用。也许是因为我是C的新手并且对数组来说是全新的。

我知道我应该通过数组,每1个发现它应该将坐标保存为“start”,然后转到下一个,将每1个发现保存到“end”。找到0后,比较长度,如果长度最大,则覆盖长度。但我没有设法正确编写代码。有人可以帮我写广告代码吗?非常感谢你。

编辑:这就是我所拥有的,但我刚刚开始,它已经不起作用了:

if(arr != NULL) {
  while (j < arr->cols) {
    while (i <=arr->rows) {
            if (getcolor(arr, i, j) == 1) {
                startI = i;
                startJ = j;
                break;
            }
            else
            i++;
    }
    i=0;
    while (i <=arr->rows) {
            if (getcolor(arr, i, j) == 1) {
                endI = i;
                endJ = j;
            }
            i++;
    }
    i=0;

   printf("start %d %d\nend %d %d\nline %d\n\n", startI, startJ, endI, endJ, line);
   j++;
   }
}

5 个答案:

答案 0 :(得分:3)

当你遇到这样的问题时,有助于将其分解为更容易解决的小问题。你的问题就是一个很好的例子。看起来你正试图立刻解决整个问题,正如你所看到的那样,嵌套循环和所有问题都会变得有点毛茸茸。那么,你怎么能让问题变得更简单呢?好吧,如果你能抓住你的手指并在一行上找到最长的线条,那就更简单了。同样,有一个简单的方法来获得单列中最长的行是很好的。所以,考虑编写这样的函数:

int longestLineInRow(int board[][], int width, int height, int &start, int &end)
{
    // returns length, with start and end being the indices of the beginning and end of the line
}

int longestLineInColumn(int board[][], int width, int height, int &start, int &end)
{
    // returns length, with start and end being the indices of the beginning and end of the line
}

现在很容易找到具有最长行的行:您只需在每行中找到最长的行,然后选择返回最大值的行。列相同。

我还没有解决为你找到行或列中最长行的问题,但这是一个更简单的任务,一旦你不再尝试一次解决整个问题,你可以解决这个问题。

答案 1 :(得分:1)

抱歉为时已晚。我没有准确地写出你想要的东西,但这确实会对你有所帮助。只需阅读它,你就会明白这一点。

链接到代码http://pastebin.com/vLATASab

或者在这里查看:

#include <stdio.h>

main()
{
    int arr[4][4];
    arr[0][0] = 0;arr[0][1] = 1;arr[0][2] = 1;arr[0][3] = 1;
    arr[1][0] = 0;arr[1][1] = 1;arr[1][2] = 0;arr[1][3] = 1;
    arr[2][0] = 0;arr[2][1] = 1;arr[2][2] = 1;arr[2][3] = 0;
    arr[3][0] = 1;arr[3][1] = 0;arr[3][2] = 1;arr[3][3] = 1;

    int i, j, k;
    int line, line_start, line_end, line_max = 0;
    int col, col_start, col_end, col_max = 0;

    // Horizently
    for (k=0; k<4; k++)
    {
        for (i=0; i<4; i++)
        {
            if (!arr[k][i]) continue;
            for (j=i; j<4; j++)
            {
                if (!arr[k][j]) break;
            }
            j--;
            if (j-i+1>line_max)
            {
                line = k;
                line_start = i;
                line_end = j;
                line_max = line_end-line_start+1;
            }
        }
    }

    printf("horizontaly\n");
    printf("start: [%d][%d]\n", line, line_start);
    printf("end: [%d][%d]\n", line, line_end);

    // Verticaly
    for (k=0; k<4; k++)
    {
        for (i=0; i<4; i++)
        {
            if (!arr[i][k]) continue;
            for (j=i; j<4; j++)
            {
                if (!arr[j][k]) break;
            }
            j--;
            if (j-i+1>col_max)
            {
                col = k;
                col_start = i;
                col_end = j;
                col_max = col_end-col_start+1;
            }
        }
    }

    printf("\nverticaly\n");
    printf("start: [%d][%d]\n", col_start, col);
    printf("end: [%d][%d]\n", col_end, col);

}

答案 2 :(得分:0)

我假设您显示的功能只有问题的一半,即找到最长的1s垂直序列。你必须编写一个镜像函数来找到最长的水平序列。

我会告诉你我在你的方法中看到的错误:

  • 您无法计算找到序列的长度
  • 您不存储最长序列的长度和索引
  • while (i <=arr->rows)应为while (i < arr->rows),否则当您只有第0,1,2和3行时,您还会查看第4行。
  • 在找到1s序列的开头后重置ii应继续推进
  • 您在j中也保存startJ,但j不会改变,只要您检查同一列
  • 您每列只查找一个1的序列
  • “水平”和“垂直”应拼写为两个“L”: - )

答案 3 :(得分:0)

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

typedef struct field {
    int rows;
    int cols;
    int *field;
} Field;

Field init_Field(){//Mock
    Field f;
    int data[4][4] = {
        {0, 1, 1, 1},
        {0, 1, 0, 1},
        {0, 1, 1, 0},
        {1, 0, 1, 0}
    };
    f.rows = 4;
    f.cols = 4;
    int size = f.rows * f.cols;
    f.field = malloc(size * sizeof(*(f.field)));
    memcpy(f.field, data, size*sizeof(*(f.field)));
    return f;
}

int getcolor(Field *f, int r, int c){
    if(r < 0 || r >= f->rows) return -1;
    if(c < 0 || c >= f->cols) return -1;
    return f->field[ r * f->cols + c];
}

int search(Field *f, int r, int c, int dr, int dc, int level, int maxLen, int nowMax){
    //dr,dc : incremental
    if(dr > 0 && f->rows - r + level <= nowMax) return -1;//stop search
    if(dc > 0 && f->cols - c + level <= nowMax) return -1;//stop search
    if(level == maxLen)
        return level;
    if(getcolor(f, r, c)==1)
        return search(f, r + dr, c + dc, dr, dc, level + 1, maxLen, nowMax);

    return level;
}

int main(){
    Field f = init_Field();
    int HMaxLen = 0, HMaxRow, HMaxCol;
    int RMaxLen = 0, RMaxRow, RMaxCol;
    int r, c, len;
    for(r = 0; r < f.rows;++r){
        for(c = 0; c < f.cols;++c){
            len = search(&f, r, c, 0, 1, 0, f.cols, HMaxLen);
            if(len > HMaxLen){
                HMaxLen = len;
                HMaxRow = r;
                HMaxCol = c;
            }
            len = search(&f, r, c, 1, 0, 0, f.rows, RMaxLen);
            if(len > RMaxLen){
                RMaxLen = len;
                RMaxRow = r;
                RMaxCol = c;
            }
        }
    }
    free(f.field);
    printf("start %d %d\n  end %d %d\n\n", HMaxRow, HMaxCol, HMaxRow, HMaxCol + HMaxLen-1);
    printf("start %d %d\n  end %d %d\n\n", RMaxRow, RMaxCol, RMaxRow + RMaxLen-1, RMaxRow);
    return 0;
}

答案 4 :(得分:0)

您可以尝试以下

void Reset(int s[2], int e[2], int cs[2],  int ce[2], int *resetState, int cc, int pc)
{
*resetState=0;
            if(cc>pc)
            {
                s[0]=cs[0];
                s[1]=cs[1];
                e[0]=ce[0];
                e[1]=ce[1];
            }
}
void Decide(int value, int s[2], int e[2], int cs[2],  int ce[2], int *resetState, int *cc, int *pc, int i, int j)
{
if(value==0&&*resetState)
{
        Reset(s, e, cs, ce, resetState, *cc, *pc);  
}
else if(value)
{
            if(*resetState==0)
            {
                cs[0]=i;
                cs[1]=j;
                if(*cc>*pc)
                {
                    *pc=*cc;
                }
                *cc=0;
                *resetState=1;
            }
            ce[0]=i;
            ce[1]=j;
            ++*cc;
}
}

void FindLargestRowNdColumn(int a[20][20],int row, int col, int hs[2], int  he[2], int vs[2], int ve[2])
{
int i, j, rcc=0, rpc=0, ccc=0, cpc=0;
int chs[2]={0,0}, che[2]={0,0}, cvs[2]={0,0}, cve[2]={0, 0};
int resetRow=0, restCol=0;
for(i=0; i<row; ++i)
{
    for(j=0; j<col; ++j)
    {
        Decide(a[i][j], hs, he, chs, che, &resetRow, &rcc, &rpc, i, j);
        Decide(a[j][i], vs, ve, cvs, cve, &restCol, &ccc, &cpc, i, j);
    }
    Reset(hs, he, chs, che, &resetRow, rcc, rpc);   
    Reset(vs, ve, cvs, cve, &restCol, ccc, cpc);    
}
}

void main()
{
int a[20][20], indexRow, colIndex;
int colum, row,hs[2], he[2], vs[2], ve[2];
printf("Enter number of rows and columns\n");
scanf("%d%d", &row, &colum);
printf("Enter the array\n");
for(indexRow=0; indexRow<row; ++indexRow)
{
    for(colIndex=0; colIndex<colum; ++colIndex)
    {
        scanf("%d", &a[indexRow][colIndex]);
    }
}
FindLargestRowNdColumn(a, row, colum, hs, he, vs, ve);
printf("Row [%d][%d] [%d][%d]\n", hs[0], hs[1], he[0], he[1]);
printf("column [%d][%d] [%d][%d]\n", vs[0], vs[1], ve[0], ve[1]);
}