阵列位置自动变为1

时间:2016-01-19 17:31:40

标签: c multidimensional-array c99

由于我已经发布了一个CodeForce问题的可能解决方案,导致时间限制超出错误enter link description here,因此出现了一些解决方案。不过我用另一个解决方案做了..

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

int greatestX = 0, greatestY = 0;
int counter = 0;

void prune_boxes(int boxes[][greatestY], int x, int y){
  printf("boxes[2][0] = %d\n", boxes[2][0]);
  if (y < 0)
    return;
  else{
    //printf("x = %d y = %d\n", x, y);
    if (boxes[x][y] == 0){
      printf("x = %d y = %d\n", x, y);
      counter++;
      boxes[x][y] = 1;
    }
    prune_boxes(boxes, x, y - 1);
    prune_boxes(boxes, x + 1, y - 1);
  }
}


int main() {
  int wetBoxes, i, j;
  scanf("%d", &wetBoxes);
  int coordinates[wetBoxes][2];
  for(i = 0; i < wetBoxes; i++){
    scanf("%d%d", &coordinates[i][0], &coordinates[i][1]);
    if (coordinates[i][0] > greatestX)
      greatestX = coordinates[i][0];
    if (coordinates[i][1] > greatestY)
      greatestY = coordinates[i][1];
  }
  int boxes[greatestY + 1][greatestY + greatestX + 1];
  memset(boxes, 0, sizeof(boxes));
  /*
   for(i = 0; i < greatestY + 1; i++){
    for (j = 0; j < greatestY + greatestX + 1; j++){
      printf("%d ", boxes[i][j]);
    }
    printf("\n");
  }
  */

  for(i = 0; i < wetBoxes; i++){
    //printf("value = %d\n", boxes[coordinates[i][0]][coordinates[i][1]]);
    prune_boxes(boxes, coordinates[i][0], coordinates[i][1]);
  }
  printf("counter = %d\n", counter);

return 0;
} 

现在没有导致时间限制超过,但它减少了任何特定值的数量。

进一步调试我发现,对于(1, 3)的输入,代码不计算坐标(2, 0)

即使进行了进一步调试,我发现boxes[2][0]在我实际手动设置该坐标之前变为1。

示例输出如下所示

1
1 3
boxes[2][0] = 0
x = 1 y = 3
boxes[2][0] = 1
x = 1 y = 2
boxes[2][0] = 1
x = 1 y = 1
boxes[2][0] = 1
x = 1 y = 0
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
x = 2 y = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
x = 3 y = 0
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
x = 2 y = 2
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
x = 3 y = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
boxes[2][0] = 1
x = 4 y = 0
boxes[2][0] = 1
boxes[2][0] = 1
counter = 9

正如您所看到的,第二个递归级别的boxes[2][0]变为1但为什么? 修改

enter image description here

2 个答案:

答案 0 :(得分:1)

我收到了很多关于这部分的编译器警告

int greatestX = 0, greatestY = 0;
int counter = 0;

void prune_boxes(int boxes[][greatestY], int x, int y){

greatestY不是常数。那么函数如何知道数组的大小呢?

如果它此时接受greatestY作为大小,则数组将为int boxes[][0],并且所有访问都将超出范围。这肯定会产生意想不到的结果。

你传递的数组不是那些尺寸,而是

int boxes[greatestY + 1][greatestY + greatestX + 1];

答案 1 :(得分:0)

感谢大家回答这个问题看起来问题是将2D数组传递给函数。 gcc没有抛出任何错误或警告,但clang发出了一些错误。因此我决定用C ++解决它。这是解决方案

#include <iostream>
#include <vector>
#include <algorithm>

static int counter;
std::vector<std::vector<int>> boxes;
void prune_boxes(int x, int y){
 if (y < 0)
   return;
 else{
   if (boxes[x][y] == 0){
     boxes[x][y] = 1;
     counter++;
   }
   prune_boxes(x, y - 1);
   prune_boxes(x + 1, y - 1);
 }
}



int main(){
  int wetBoxes;
  std::cin >> wetBoxes;
  std::vector<int> coordinatesX;
  std::vector<int> coordinatesY;
  int input;
  for (int i = 0; i < wetBoxes; i++){
    std::cin >> input;
    coordinatesX.push_back(input);
    std::cin >> input;
    coordinatesY.push_back(input);
  }

  int greatestX = *max_element(coordinatesX.begin(), coordinatesX.end());
  int greatestY = *max_element(coordinatesY.begin(), coordinatesY.end());

  int Y =  greatestY + 1;
  int X =  greatestX + greatestY + 1;
  boxes.resize(X);
  for (int i = 0; i < X; i++){
    for (int j = 0; j < Y; j++){
      boxes[i].push_back(0);
    }
  }

  for(int i = 0; i < wetBoxes; i++){
    prune_boxes(coordinatesX[i], coordinatesY[i]);
    std::cout << counter << std::endl;
    counter = 0;
  }
  return 0;
}