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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <stdio.h>
using namespace std;
int h, w, n, i, j, k, l;
int kan[1201][1201];
int dp[1201][1201];
int main(void) {
  scanf("%d %d %d", &h, &w, &n);
  while (h != 0 && w != 0 && n != 0) {
    for (i = 1; i <= 1200; i++) {
      for (j = 1; j <= 1200; j++) {
        dp[i][j] = 0;
        kan[i][j] = 0;
      }
    }
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        scanf("%d", &kan[i][j]); // kan=kanbann
      }
    }
    dp[1][1] = n - 1;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (kan[i][j] == 1) {
          if (dp[i][j] % 2 == 1) {
            dp[i][j + 1] = dp[i][j + 1] + dp[i][j] / 2 + 1;
            dp[i + 1][j] += (dp[i][j] / 2);
          } else if (dp[i][j] % 2 == 0) {
            /*if(i==2 && j==2){
                    printf("%d %d\\n",dp[i][j],dp[i+1][j]);
            }*/
            dp[i][j + 1] += dp[i][j] / 2;
            dp[i + 1][j] += dp[i][j] / 2;
          }
        } else if (kan[i][j] == 0) {
          if (dp[i][j] % 2 == 1) {
            dp[i][j + 1] += (dp[i][j] / 2);
            dp[i + 1][j] = dp[i + 1][j] + (dp[i][j] / 2) + 1;
          } else if (dp[i][j] % 2 == 0) {
            dp[i][j + 1] += dp[i][j] / 2;
            dp[i + 1][j] += dp[i][j] / 2;
          }
        }
      }
      /*for(l=1;l<=h;l++){
              for(k=1;k<=w;k++){
                      printf("%d ",dp[l][k]);
              }
              printf("\\n");
      }*/
    }
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (dp[i][j] != 0) {
          if (dp[i][j] % 2 == 1) {
            if (kan[i][j] == 0) {
              kan[i][j] = 1;
            } else if (kan[i][j] == 1) {
              kan[i][j] = 0;
            }
          }
        }
      }
    }
    i = 1;
    j = 1;
    while (1) {
      if (kan[i][j] == 1) {
        j++;
      } else if (kan[i][j] == 0) {
        i++;
      }
      if (i == h + 1 || j == w + 1) {
        break;
      }
    }
    printf("%d %d\\n", i, j);
    /*for(i=1;i<=h;i++){
            for(j=1;j<=w;j++){
                    printf("%d ",dp[i][j]);
            }
            printf("\\n");
    }*/
    scanf("%d %d %d", &h, &w, &n);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <stdio.h>
using namespace std;
int h, w, n, i, j, k, l;
int kan[10000][10000];
int dp[10000][10000];
int main(void) {
  scanf("%d %d %d", &h, &w, &n);
  while (h != 0 && w != 0 && n != 0) {
    for (i = 1; i <= 1001; i++) {
      for (j = 1; j <= 1001; j++) {
        dp[i][j] = 0;
      }
    }
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        scanf("%d", &kan[i][j]); // kan=kanbann
      }
    }
    dp[1][1] = n - 1;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (kan[i][j] == 1) {
          if (dp[i][j] % 2 == 1) {
            dp[i][j + 1] = dp[i][j + 1] + dp[i][j] / 2 + 1;
            dp[i + 1][j] += (dp[i][j] / 2);
          } else if (dp[i][j] % 2 == 0) {
            /*if(i==2 && j==2){
                    printf("%d %d\\n",dp[i][j],dp[i+1][j]);
            }*/
            dp[i][j + 1] += dp[i][j] / 2;
            dp[i + 1][j] += dp[i][j] / 2;
          }
        } else if (kan[i][j] == 0) {
          if (dp[i][j] % 2 == 1) {
            dp[i][j + 1] += (dp[i][j] / 2);
            dp[i + 1][j] = dp[i + 1][j] + (dp[i][j] / 2) + 1;
          } else if (dp[i][j] % 2 == 0) {
            dp[i][j + 1] += dp[i][j] / 2;
            dp[i + 1][j] += dp[i][j] / 2;
          }
        }
      }
      /*for(l=1;l<=h;l++){
              for(k=1;k<=w;k++){
                      printf("%d ",dp[l][k]);
              }
              printf("\\n");
      }*/
    }
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (dp[i][j] != 0) {
          if (dp[i][j] % 2 == 1) {
            if (kan[i][j] == 0) {
              kan[i][j] = 1;
            } else if (kan[i][j] == 1) {
              kan[i][j] = 0;
            }
          }
        }
      }
    }
    i = 1;
    j = 1;
    while (1) {
      if (kan[i][j] == 1) {
        j++;
      } else if (kan[i][j] == 0) {
        i++;
      }
      if (i == h + 1 || j == w + 1) {
        break;
      }
    }
    printf("%d %d\\n", i, j);
    /*for(i=1;i<=h;i++){
            for(j=1;j<=w;j++){
                    printf("%d ",dp[i][j]);
            }
            printf("\\n");
    }*/
    scanf("%d &d %d\\n", &h, &w, &n);
  }
  return 0;
}