Problem p00464 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <vector>

int **getarray(int w, int h) {
  int **a = new int *[w];
  for (int i = 0; i < w; i++) {
    a[i] = new int[h];
  }
  return a;
}

int remarray(int **a, int w, int h) {
  for (int i = 0; i < w; i++) {
    delete[] a[i];
  }
  delete[] a;
}

int main() {
  int h, w, n;
  int self;
  int **d;
  int **m;

  while (1) {
    scanf("%d %d %d", &h, &w, &n);
    if (!h && !w && !n) {
      break;
    }
    d = getarray(w + 1, h + 1);
    m = getarray(w + 1, h + 1);

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        scanf("%d", &self);
        m[j][i] = self;
      }
    }

    d[0][0] = n - 1;
    for (int j = 0; j < h; j++) {
      for (int i = 0; i < w; i++) {
        if (i == 0 && j == 0) {
          continue;
        }
        d[i][j] = 0;
        if (i != 0 && d[i - 1][j] > 0) {
          d[i][j] += (d[i - 1][j]) / 2;
          if (m[i - 1][j] == 1) {
            d[i][j] += (d[i - 1][j]) % 2;
          }
        }
        if (j != 0 && d[i][j - 1] > 0) {
          d[i][j] += (d[i][j - 1]) / 2;
          if (m[i][j - 1] == 0) {
            d[i][j] += (d[i][j - 1]) % 2;
          }
        }
      }
    }
    for (int j = 0; j < h; j++) {
      for (int i = 0; i < w; i++) {
        m[i][j] = (m[i][j] + d[i][j]) % 2;
        // printf("%d ",m[i][j]);
      }
      // printf("\\n");
    }
    int x, y;
    x = y = 0;
    while (x < w && y < h) {
      if (m[x][y] == 1) {
        // printf("ツ督圭n");
        x += 1;
      } else {
        // printf("ツ禿ャ\\n");
        y += 1;
      }
    }
    printf("%d %d\\n", y + 1, x + 1);
    remarray(m, w + 1, h + 1);
    remarray(d, w + 1, h + 1);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>

int **getarray(int w, int h) {
  int **a = new int *[w];
  for (int i = 0; i < w; i++) {
    a[i] = new int[h];
  }
  return a;
}

int remarray(int **a, int w, int h) {
  for (int i = 0; i < w; i++) {
    delete[] a[i];
  }
  delete[] a;
}

int main() {
  int h, w, n;
  int self;
  int **d;
  int **m;

  while (1) {
    scanf("%d %d %d", &h, &w, &n);
    if (!h && !w && !n) {
      break;
    }
    d = getarray(w + 1, h + 1);
    m = getarray(w + 1, h + 1);

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        scanf("%d", &self);
        m[j][i] = self;
      }
    }

    d[0][0] = n - 1;
    for (int k = 0; k < n - 1; k++) {
      for (int j = 0; j < h; j++) {
        for (int i = 0; i < w; i++) {
          if (i == 0 && j == 0) {
            continue;
          }
          d[i][j] = 0;
          if (i != 0 && d[i - 1][j] > 0) {
            d[i][j] += (d[i - 1][j]) / 2;
            if (m[i - 1][j] == 1) {
              d[i][j] += (d[i - 1][j]) % 2;
            }
          }
          if (j != 0 && d[i][j - 1] > 0) {
            d[i][j] += (d[i][j - 1]) / 2;
            if (m[i][j - 1] == 0) {
              d[i][j] += (d[i][j - 1]) % 2;
            }
          }
        }
      }
    }
    for (int j = 0; j < h; j++) {
      for (int i = 0; i < w; i++) {
        m[i][j] = (m[i][j] + d[i][j]) % 2;
        // printf("%d ",m[i][j]);
      }
      // printf("\\n");
    }
    int x, y;
    x = y = 0;
    while (x < w && y < h) {
      if (m[x][y] == 1) {
        // printf("ツ督圭n");
        x += 1;
      } else {
        // printf("ツ禿ャ\\n");
        y += 1;
      }
    }
    printf("%d %d\\n", x + 1, y + 1);
    remarray(m, w + 1, h + 1);
    remarray(d, w + 1, h + 1);
  }
}