简体   繁体   English

无法理解问题的要求。 (ProjectEuler 问题:11)

[英]Can't understand the requirement of the problem. (ProjectEuler problem: 11)

I am trying to solve this problem: https://projecteuler.net/problem=11 However, this part confuses me:我正在尝试解决这个问题: https://projecteuler.net/problem=11但是,这部分让我感到困惑:

What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?在 20×20 的方格中,同一方向(上、下、左、右、对角线)的四个相邻数字的最大乘积是多少?

what does in the same direction and up, down, left, right or diagonally mean? in the same direction and up, down, left, right or diagonally是什么意思? Is it just me or is the language vague here?只是我还是这里的语言含糊不清?

this is what I have tried so far:到目前为止,这是我尝试过的:

long int prod{0}, n{20};

for(int i{0}; i <= n; i++) {
    for(int j{0}; j <= n; j++) {
        long int a{grid[i][j]}, b{grid[i+1][j+1]}, c{grid[i+2][j+2]}, d{grid[i+3][j+3]};
        if(prod < (a * b * c * d))
            prod = a * b * c * d;
    }
}

return prod;

With this function I satisfy the first demand but up down left right or diagonally?有了这个 function,我满足了第一个需求,但从左到右还是对角线? what does or mean there?那里是or意思?

A direction in the context of a grid is the geometrical space whose all points are on the same line.网格上下文中的方向是所有点都在同一条线上的几何空间。

If we take a point, then we can cross it with 4 different lines:如果我们取一个点,那么我们可以用 4 条不同的线穿过它:

\   |   /
 \  |  /
  \ | /
   \|/
----*----
   /|\
  / | \
 /  |  \
/   |   \

So, how could we define these directions?那么,我们如何定义这些方向呢? There is a very simple way to do so:有一种非常简单的方法可以做到这一点:

  • you loop the rows你循环行
    • you loop the columns你循环列
      • at the current point, you try to get 4 values, including the current point在当前点,你尝试获取4个值,包括当前点
        • downwards向下
        • rightwards向右
        • right-downwards右下
        • right-upwards右上

I say "try", which means that you will have to ignore quite a few possibilities due to the boundaries of your grid.我说“尝试”,这意味着由于网格的边界,您将不得不忽略很多可能性。 Yet, it is a neat way to get all four directions:然而,这是获得所有四个方向的巧妙方法:

int bestProduct = -1; //Assuming you have positives
for (int row = 0; row < n; row++) {
    for (int column = 0; column < n; column++) {
        int ignore = 0;
        int rowDir = 0;
        int colDir = 1;
        int product = grid[row][column];
        for (int index = 0; (!ignore) && (index < 3); index++) {
            if (
                   (row + rowDir < 0) ||
                   (row + rowDir >= n) ||
                   (column + colDir < 0) ||
                   (column + colDir >= m)
               ) {
                ignore = 1;
            }
            else product *= grid[row + rowDir][column + colDir];
        }
        if ((!ignore) && (bestProduct < product)) bestProduct = product;
    }
}

This is not a full implementation, since you also need to do some work.这不是一个完整的实现,因为您还需要做一些工作。 You will need to continue with:您将需要继续:

  • converting the inner part of the second loop into a function except the if conditional that checks whether the product is higher than the best product so far将第二个循环的内部部分转换为 function 除了if条件检查产品是否高于目前为止最好的产品
  • remove that inner code and replace it with the function call删除该内部代码并将其替换为 function 调用
  • call the function three more times, once for each other direction再拨3次function,一次互换方向
  • the other directions are:其他方向是:
    • 1: having a 1 colDir and 0 rowDir 1:有 1 个colDir和 0 rowDir
    • 2: having a 1 colDir and 1 rowDir 2:有 1 个colDir和 1 个rowDir
    • 3: having a 1 colDir and -1 rowDir 3:有一个 1 colDir和 -1 rowDir

I know it is more difficult to consider this partial solution, but it will help you a lot in the long run if you do the rest for yourself, as the ideas are all laid down here.我知道考虑这个部分解决方案比较困难,但如果你自己做 rest 从长远来看,它会对你有很大帮助,因为所有的想法都在这里。

You need to check each row, column, and diagonal of 4. This means you need to check:您需要检查 4 的每一行、每一列和对角线。这意味着您需要检查:

from grid[i-4][j] to grid[i][j]  
from grid[i][j-4] to grid[i][j]  
from grid[i-4][j-4] to grid[i][j] (diagonal) 
from grid[i+4][j-4] to grid[i][j] (other diagonal)

Make sure to watch for the grid sides too as if you're on the very left, you'll need to look to the right一定要注意网格的两侧,就好像你在最左边一样,你需要向右看

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

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