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

#include <iostream>
using namespace std;

void input(int **, int, int);
int howManyIslands(int **, int, int);
void howBigIslands(int **, bool **, int, int, int, int);

int main() {
  int **island, w, h;
  while (true) {
    cin >> w >> h;
    if (w == h && h == 0)
      break;
    island = new int *[w];
    for (int i = 0; i < w; i++)
      island[i] = new int[h];
    input(island, w, h);
    cout << howManyIslands(island, w, h) << endl;
    for (int i = 0; i < w; i++)
      delete[] island[i];
    delete[] island;
  }
  return 0;
}

void input(int **island, int w, int h) {
  char *buf;
  cin.clear();
  cin.ignore();
  for (int i = 0; i < h; i++) {
    buf = new char[w * 2];
    cin.getline(buf, sizeof(char) * w * 2);
    int j = 0, k = 0;
    while (buf[j] != '\\0') {
      if (buf[j] != ' ') {
        switch (buf[j]) {
        case '0':
          island[k][i] = 0;
          k++;
          break;
        case '1':
          island[k][i] = 1;
          k++;
          break;
        }
        if (w == k)
          break;
      }
      j++;
    }
    delete[] buf;
  }
}

int howManyIslands(int **island, int w, int h) {
  bool **check;
  int ans = 0;
  check = new bool *[w];
  for (int i = 0; i < w; i++) {
    check[i] = new bool[h];
    for (int j = 0; j < h; j++)
      check[i][j] = false;
  }
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      if (check[i][j])
        continue;
      if (island[i][j]) {
        ans++;
        howBigIslands(island, check, i, j, w, h);
      } else {
        check[i][j] = true;
      }
    }
  }
  for (int i = 0; i < w; i++)
    delete[] check[i];
  delete[] check;

  return ans;
}

void howBigIslands(int **island, bool **check, int x, int y, int w, int h) {
  if (check[x][y])
    return;
  enum __DIR {
    UPPER_LEFT,
    UP,
    UPPER_RIGHT,
    LEFT,
    RIGHT,
    LOWER_LEFT,
    UNDER,
    LOWER_RIGHT,
    DIR_END
  };
  int periphe[DIR_END][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0},
                             {1, 0},   {-1, 1}, {0, 1},  {1, 1}};
  check[x][y] = true;
  for (int i = 0; i < DIR_END; i++) {
    int newx = x + periphe[i][0], newy = y + periphe[i][1];
    if (newx < 0 || w - 1 < newx || newy < 0 || h - 1 < newy)
      continue;
    if (island[newx][newy]) {
      if (!check[newx][newy])
        howBigIslands(island, check, newx, newy, w, h);
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

void input(int **, int, int);
int howManyIslands(int **, int, int);
void howBigIslands(int **, bool **, int, int, int, int);

int main() {
  int **island, w, h;
  while (true) {
    cin >> w >> h;
    if (w == h && h == 0)
      break;
    island = new int *[w];
    for (int i = 0; i < w; i++)
      island[i] = new int[h];
    input(island, w, h);
    cout << howManyIslands(island, w, h) << endl;
    for (int i = 0; i < w; i++)
      delete[] island[i];
    delete[] island;
  }
  return 0;
}

void input(int **island, int w, int h) {
  char *buf;
  cin.clear();
  cin.ignore();
  for (int i = 0; i < h; i++) {
    buf = new char[w * 2];
    cin.getline(buf, sizeof(buf));
    int j = 0, k = 0;
    while (buf[j] != '\\0') {
      if (buf[j] != ' ') {
        switch (buf[j]) {
        case '0':
          island[k][i] = 0;
          k++;
          break;
        case '1':
          island[k][i] = 1;
          k++;
          break;
        }
        if (w == k)
          break;
      }
      j++;
    }
    delete[] buf;
  }
}

int howManyIslands(int **island, int w, int h) {
  bool **check;
  int ans = 0;
  check = new bool *[w];
  for (int i = 0; i < w; i++) {
    check[i] = new bool[h];
    for (int j = 0; j < h; j++)
      check[i][j] = false;
  }

  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++)
      cout << check[i][j];
    cout << endl;
  }

  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      if (check[i][j])
        continue;
      if (island[i][j]) {
        ans++;
        howBigIslands(island, check, i, j, w, h);
      } else {
        check[i][j] = true;
      }
    }
  }
  for (int i = 0; i < w; i++)
    delete[] check[i];
  delete[] check;

  return ans;
}

void howBigIslands(int **island, bool **check, int x, int y, int w, int h) {
  cout << "check" << endl;
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++)
      cout << check[i][j];
    cout << endl;
  }
  cout << endl << "island" << endl;
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++)
      cout << island[i][j];
    cout << endl;
  }
  cout << endl << endl;
  if (check[x][y])
    return;
  enum __DIR {
    UPPER_LEFT,
    UP,
    UPPER_RIGHT,
    LEFT,
    RIGHT,
    LOWER_LEFT,
    UNDER,
    LOWER_RIGHT,
    DIR_END
  };
  int periphe[DIR_END][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0},
                             {1, 0},   {-1, 1}, {0, 1},  {1, 1}};
  check[x][y] = true;
  for (int i = 0; i < DIR_END; i++) {
    int newx = x + periphe[i][0], newy = y + periphe[i][1];
    if (newx < 0 || w - 1 < newx || newy < 0 || h - 1 < newy)
      continue;
    if (island[newx][newy]) {
      if (!check[newx][newy])
        howBigIslands(island, check, newx, newy, w, h);
    }
  }
}