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

#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int n, m, a, b, x[110][110], y[110][110], sum = 0;
  memset(x, 0, sizeof(x));
  cin >> m >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> x[i][j];
    }
  }
  memset(y, 0, sizeof(y));
  a = 100;
  if (m == 100) {
    for (int i = 1; i <= 50; i++) {
      if (x[2][i] == 0) {
        b++;
      }
    }
    if (b == 50) {
      a = 10000;
    }
  }
  for (int i = 1; i <= m; i++) {
    if (x[1][i] == 0) {
      y[1][i] = 1;
      for (int j = 1; j < a; j++) {
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (x[k][l] == 0 && y[k][l] == 1) {
              if (x[k][l - 1] == 0) {
                y[k][l - 1] = 1;
              }
              if (x[k - 1][l] == 0) {
                y[k - 1][l] = 1;
              }
              if (k % 2 == 0) {
                if (x[k - 1][l - 1] == 0) {
                  y[k - 1][l - 1] = 1;
                }
                if (x[k + 1][l - 1] == 0) {
                  y[k + 1][l - 1] = 1;
                }
              }
              if (x[k][l + 1] == 0) {
                y[k][l + 1] = 1;
              }
              if (x[k + 1][l] == 0) {
                y[k + 1][l] = 1;
              }
              if (k % 2 == 1) {
                if (x[k + 1][l + 1] == 0) {
                  y[k + 1][l + 1] = 1;
                }
                if (x[k - 1][l + 1] == 0) {
                  y[k - 1][l + 1] = 1;
                }
              }
            }
          }
        }
        sum = 0;
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    if (x[n][i] == 0) {
      y[n][i] = 1;
      for (int j = 1; j < a; j++) {
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (x[k][l] == 0 && y[k][l] == 1) {
              if (x[k][l - 1] == 0) {
                y[k][l - 1] = 1;
              }
              if (x[k - 1][l] == 0) {
                y[k - 1][l] = 1;
              }
              if (k % 2 == 0) {
                if (x[k - 1][l - 1] == 0) {
                  y[k - 1][l - 1] = 1;
                }
                if (x[k + 1][l - 1] == 0) {
                  y[k + 1][l - 1] = 1;
                }
              }
              if (x[k][l + 1] == 0) {
                y[k][l + 1] = 1;
              }
              if (x[k + 1][l] == 0) {
                y[k + 1][l] = 1;
              }
              if (k % 2 == 1) {
                if (x[k + 1][l + 1] == 0) {
                  y[k + 1][l + 1] = 1;
                }
                if (x[k - 1][l + 1] == 0) {
                  y[k - 1][l + 1] = 1;
                }
              }
            }
          }
        }
        sum = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (x[i][1] == 0) {
      y[i][1] = 1;
      for (int j = 1; j < a; j++) {
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (x[k][l] == 0 && y[k][l] == 1) {
              if (x[k][l - 1] == 0) {
                y[k][l - 1] = 1;
              }
              if (x[k - 1][l] == 0) {
                y[k - 1][l] = 1;
              }
              if (k % 2 == 0) {
                if (x[k - 1][l - 1] == 0) {
                  y[k - 1][l - 1] = 1;
                }
                if (x[k + 1][l - 1] == 0) {
                  y[k + 1][l - 1] = 1;
                }
              }
              if (x[k][l + 1] == 0) {
                y[k][l + 1] = 1;
              }
              if (x[k + 1][l] == 0) {
                y[k + 1][l] = 1;
              }
              if (k % 2 == 1) {
                if (x[k + 1][l + 1] == 0) {
                  y[k + 1][l + 1] = 1;
                }
                if (x[k - 1][l + 1] == 0) {
                  y[k - 1][l + 1] = 1;
                }
              }
            }
          }
        }
        sum = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (x[i][m] == 0) {
      y[i][m] = 1;
      for (int j = 1; j < a; j++) {
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (x[k][l] == 0 && y[k][l] == 1) {
              if (x[k][l - 1] == 0) {
                y[k][l - 1] = 1;
              }
              if (x[k - 1][l] == 0) {
                y[k - 1][l] = 1;
              }
              if (k % 2 == 0) {
                if (x[k - 1][l - 1] == 0) {
                  y[k - 1][l - 1] = 1;
                }
                if (x[k + 1][l - 1] == 0) {
                  y[k + 1][l - 1] = 1;
                }
              }
              if (x[k][l + 1] == 0) {
                y[k][l + 1] = 1;
              }
              if (x[k + 1][l] == 0) {
                y[k + 1][l] = 1;
              }
              if (k % 2 == 1) {
                if (x[k + 1][l + 1] == 0) {
                  y[k + 1][l + 1] = 1;
                }
                if (x[k - 1][l + 1] == 0) {
                  y[k - 1][l + 1] = 1;
                }
              }
            }
          }
        }
        sum = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (y[i][j] != 1) {
        x[i][j] = 1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (x[i][j] == 1) {
        if (x[i][j - 1] == 0) {
          sum++;
        }
        if (x[i][j + 1] == 0) {
          sum++;
        }
        if (x[i - 1][j] == 0) {
          sum++;
        }
        if (x[i + 1][j] == 0) {
          sum++;
        }
        if (i % 2 == 0) {
          if (x[i - 1][j - 1] == 0) {
            sum++;
          }
          if (x[i + 1][j - 1] == 0) {
            sum++;
          }
        }
        if (i % 2 == 1) {
          if (x[i - 1][j + 1] == 0) {
            sum++;
          }
          if (x[i + 1][j + 1] == 0) {
            sum++;
          }
        }
      }
    }
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int n, m, x[110][110], y[110][110], t, u, sum = 0;
  memset(x, 0, sizeof(x));
  cin >> m >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> x[i][j];
    }
  }
  t = 0;
  u = 0;
  memset(y, 0, sizeof(y));
  for (int i = 1; i <= m; i++) {
    if (x[1][i] == 0) {
      y[1][i] = 1;
      for (int j = 1; j < 10000; j++) {
        u = t;
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (x[k][l] == 0 && y[k][l] == 1) {
              if (x[k][l - 1] == 0) {
                y[k][l - 1] = 1;
              }
              if (x[k - 1][l] == 0) {
                y[k - 1][l] = 1;
              }
              if (k % 2 == 0) {
                if (x[k - 1][l - 1] == 0) {
                  y[k - 1][l - 1] = 1;
                }
                if (x[k + 1][l - 1] == 0) {
                  y[k + 1][l - 1] = 1;
                }
              }
              if (x[k][l + 1] == 0) {
                y[k][l + 1] = 1;
              }
              if (x[k + 1][l] == 0) {
                y[k + 1][l] = 1;
              }
              if (k % 2 == 1) {
                if (x[k + 1][l + 1] == 0) {
                  y[k + 1][l + 1] = 1;
                }
                if (x[k - 1][l + 1] == 0) {
                  y[k - 1][l + 1] = 1;
                }
              }
            }
          }
        }
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (y[k][l] == 1) {
              t++;
            }
          }
        }
        if (t == u) {
          goto Exit1;
        }
        sum = 0;
      }
    }
  }
Exit1:;
  for (int i = 1; i <= m; i++) {
    if (x[n][i] == 0) {
      y[n][i] = 1;
      for (int j = 1; j < 10000; j++) {
        u = t;
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (x[k][l] == 0 && y[k][l] == 1) {
              if (x[k][l - 1] == 0) {
                y[k][l - 1] = 1;
              }
              if (x[k - 1][l] == 0) {
                y[k - 1][l] = 1;
              }
              if (k % 2 == 0) {
                if (x[k - 1][l - 1] == 0) {
                  y[k - 1][l - 1] = 1;
                }
                if (x[k + 1][l - 1] == 0) {
                  y[k + 1][l - 1] = 1;
                }
              }
              if (x[k][l + 1] == 0) {
                y[k][l + 1] = 1;
              }
              if (x[k + 1][l] == 0) {
                y[k + 1][l] = 1;
              }
              if (k % 2 == 1) {
                if (x[k + 1][l + 1] == 0) {
                  y[k + 1][l + 1] = 1;
                }
                if (x[k - 1][l + 1] == 0) {
                  y[k - 1][l + 1] = 1;
                }
              }
            }
          }
        }
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (y[k][l] == 1) {
              t++;
            }
          }
        }
        if (t == u) {
          goto Exit2;
        }
        sum = 0;
      }
    }
  }
Exit2:;
  for (int i = 1; i <= n; i++) {
    if (x[i][1] == 0) {
      y[i][1] = 1;
      for (int j = 1; j < 10000; j++) {
        u = t;
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (x[k][l] == 0 && y[k][l] == 1) {
              if (x[k][l - 1] == 0) {
                y[k][l - 1] = 1;
              }
              if (x[k - 1][l] == 0) {
                y[k - 1][l] = 1;
              }
              if (k % 2 == 0) {
                if (x[k - 1][l - 1] == 0) {
                  y[k - 1][l - 1] = 1;
                }
                if (x[k + 1][l - 1] == 0) {
                  y[k + 1][l - 1] = 1;
                }
              }
              if (x[k][l + 1] == 0) {
                y[k][l + 1] = 1;
              }
              if (x[k + 1][l] == 0) {
                y[k + 1][l] = 1;
              }
              if (k % 2 == 1) {
                if (x[k + 1][l + 1] == 0) {
                  y[k + 1][l + 1] = 1;
                }
                if (x[k - 1][l + 1] == 0) {
                  y[k - 1][l + 1] = 1;
                }
              }
            }
          }
        }
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (y[k][l] == 1) {
              t++;
            }
          }
        }
        if (t == u) {
          goto Exit3;
        }
        sum = 0;
      }
    }
  }
Exit3:;
  for (int i = 1; i <= n; i++) {
    if (x[i][m] == 0) {
      y[i][m] = 1;
      for (int j = 1; j < 10000; j++) {
        u = t;
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (x[k][l] == 0 && y[k][l] == 1) {
              if (x[k][l - 1] == 0) {
                y[k][l - 1] = 1;
              }
              if (x[k - 1][l] == 0) {
                y[k - 1][l] = 1;
              }
              if (k % 2 == 0) {
                if (x[k - 1][l - 1] == 0) {
                  y[k - 1][l - 1] = 1;
                }
                if (x[k + 1][l - 1] == 0) {
                  y[k + 1][l - 1] = 1;
                }
              }
              if (x[k][l + 1] == 0) {
                y[k][l + 1] = 1;
              }
              if (x[k + 1][l] == 0) {
                y[k + 1][l] = 1;
              }
              if (k % 2 == 1) {
                if (x[k + 1][l + 1] == 0) {
                  y[k + 1][l + 1] = 1;
                }
                if (x[k - 1][l + 1] == 0) {
                  y[k - 1][l + 1] = 1;
                }
              }
            }
          }
        }
        for (int k = 1; k <= n; k++) {
          for (int l = 1; l <= m; l++) {
            if (y[k][l] == 1) {
              t++;
            }
          }
        }
        if (t == u) {
          goto Exit4;
        }
        sum = 0;
      }
    }
  }
Exit4:;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (y[i][j] != 1) {
        x[i][j] = 1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (x[i][j] == 1) {
        if (x[i][j - 1] == 0) {
          sum++;
        }
        if (x[i][j + 1] == 0) {
          sum++;
        }
        if (x[i - 1][j] == 0) {
          sum++;
        }
        if (x[i + 1][j] == 0) {
          sum++;
        }
        if (i % 2 == 0) {
          if (x[i - 1][j - 1] == 0) {
            sum++;
          }
          if (x[i + 1][j - 1] == 0) {
            sum++;
          }
        }
        if (i % 2 == 1) {
          if (x[i - 1][j + 1] == 0) {
            sum++;
          }
          if (x[i + 1][j + 1] == 0) {
            sum++;
          }
        }
      }
    }
  }
  cout << sum << endl;
  return 0;
}