6 * 6阵列中的最大沙漏总和 [英] Maximum hourglass sum possible in a 6*6 array

查看:77
本文介绍了6 * 6阵列中的最大沙漏总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

二维数组上有一个问题

鉴于6 * 6矩阵,我们必须打印矩阵中发现的最大(最大)沙漏和. 沙漏描述为:

Given a 6*6 matrix we have to print the largest (maximum) hourglass sum found in the matrix. An hourglass is described as:

a b c
  d
e f g

样本输入

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0

样本输出

19

说明

示例矩阵包含以下沙漏:

The sample matrix contains the following hourglasses:

1 1 1   1 1 0   1 0 0   0 0 0
  1       0       0       0
1 1 1   1 1 0   1 0 0   0 0 0

0 1 0   1 0 0   0 0 0   0 0 0
  1       1       0       0
0 0 2   0 2 4   2 4 4   4 4 0

1 1 1   1 1 0   1 0 0   0 0 0
  0       2       4       4
0 0 0   0 0 2   0 2 0   2 0 0

0 0 2   0 2 4   2 4 4   4 4 0
  0       0       2       0
0 0 1   0 1 2   1 2 4   2 4 0

最大沙漏(19)的沙漏是

The hourglass with maximum sum (19) is

2 4 4
  2
1 2 4

我编写了一个程序,其中编写了一个用于计算沙漏总和的函数.现在,我创建了一个循环,该循环可以连续每四个沙漏调用一次此函数.并且每四行可以制作一个沙漏.

I have written a program where I have made a function for calculating the sum of hourglass. Now I have made a loop that calls this function for every four hourglass possible for a row. And for every four rows that can make a hourglass.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int sum(int a[6][6],int i,int j)
{
    int n=i+3;
    int m=j+3;
    int sum=0;
   for(i;i<n;i++)
   {
       for(j;j<m;j++)
       {
           if(i==n-2)
           {
               sum += a[i][j+1];
               break;
           }
          else
             sum += a[i][j];
       }   
   }
   // printf("%d\t",sum);
    return sum;
}

int main(){
    int arr[6][6];
    int i,j,n,k;
    int max=0;
    for(int arr_i = 0; arr_i < 6; arr_i++){
       for(int arr_j = 0; arr_j < 6; arr_j++){

          scanf("%d",&arr[arr_i][arr_j]);
       }
    }
    for(int i=0;i<4;i++)
    {
        k=0;
        while(k<4)
        {
            n=sum(arr,i,k);
          //  printf("%d\t",n);
            k++;
            if(n>max)
                max=n;

        }
    }
    printf("%d",max);
    return 0;
}

有人可以告诉我我要去哪里了吗,或者这种方法对解决这个问题不正确吗?

Can anyone tell me where I am going wrong, or is this method not correct for doing this problem?

我的程序将10打印为输出.

My program prints 10 as output.

推荐答案

该错误可能与您的sum函数有关.您这样做的方法有些过头,对您来说,这样做会更简单(更易读!):

The error is probably in your sum function. The way you did it is a bit of an overkill, it'd be much simpler (and readable!) for you to do sth like this:

#define GRID_SIZE (6)
int sum(int a[GRID_SIZE][GRID_SIZE], int i, int j)
{   // Define an hourglass by the index i,j of its central element
    int sum = a[j-1][i-1] + a[j-1][i] + a[j-1][i+1] +
                            a[j][i] +
              a[j+1][i-1] + a[j+1][i] + a[j+1][i+1];
    return sum;
}

然后只需确保使用理智的值(在[1,len-2]中)进行迭代:

Then just be sure you iterate with sane values (in [1, len-2]):

for (int i = 1; i < (GRID_SIZE-1); i++)
{
    for (int j = 1; j < (GRID_SIZE-1); j++)
    {
        n = sum(arr, i, j);
        if (n > max)
            max = n;

    }
}

检查它是否在这里起作用: http://www.cpp.sh/46jhy

Check that it works here: http://www.cpp.sh/46jhy

谢谢,这很有趣:-).

Thanks, that was light fun :-).

PS:请确保您检查了一些编码标准文档,从长远来看,这将使您的工作更加轻松,只需搜索"C代码格式标准"并习惯于尝试使用自己喜欢的标准即可.除非您自己做一些新的事情,否则您可能必须遵循一个标准,甚至可能没有说出哪个标准,因此请熟悉一般规则并习惯于遵循一个自己喜欢的规则.

PS: Make sure you check some coding standards document, it'll make your life a lot easier in the long run, just search for "C code format standard" and get used to trying to work with one you like. Unless you do something new and on your own, you will probably have to follow a standard and maybe not even have a say in which one, so get familiar with general rules and used to following one, whichever you like.

这篇关于6 * 6阵列中的最大沙漏总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆