Problem p00193 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  int id, dist;
};

struct Data {
  Data(int arg_row, int arg_col, int arg_dist) {
    row = arg_row;
    col = arg_col;
    dist = arg_dist;
  }
  int row, col, dist;
};

int W, H;
int diff_row_GU[6] = {-1, -1, 0, 0, 1, 1},
    diff_col_GU[6] = {-1, 0, -1, 1, -1, 0};
int diff_row_KI[6] = {-1, -1, 0, 0, 1, 1}, diff_col_KI[6] = {0, 1, -1, 1, 0, 1};

bool rangeCheck(int row, int col) {
  if (row >= 0 && row <= H - 1 && col >= 0 && col <= W - 1)
    return true;
  else {
    return false;
  }
}

void func() {

  Info map[H][W];

  for (int row = 0; row < H; row++) {
    for (int col = 0; col < W; col++) {
      map[row][col].dist = BIG_NUM;
    }
  }

  int num_conv;
  scanf("%d", &num_conv);

  int tmp_col, tmp_row;
  int adj_row, adj_col;

  queue<Data> Q;

  for (int loop = 0; loop < num_conv; loop++) {
    scanf("%d %d", &tmp_col, &tmp_row);
    tmp_col--;
    tmp_row--;

    map[tmp_row][tmp_col].id = loop;
    map[tmp_row][tmp_col].dist = 0;

    Q.push(Data(tmp_row, tmp_col, 0));

    while (!Q.empty()) {

      if (Q.front().row % 2 == 0) {

        for (int i = 0; i < 6; i++) {
          adj_row = Q.front().row + diff_row_GU[i];
          adj_col = Q.front().col + diff_col_GU[i];

          if (!rangeCheck(adj_row, adj_col))
            continue;

          if (map[adj_row][adj_col].dist > Q.front().dist + 1) {
            map[adj_row][adj_col].dist = Q.front().dist + 1;
            map[adj_row][adj_col].id = loop;
            Q.push(Data(adj_row, adj_col, Q.front().dist + 1));
          }
        }
      } else {
        for (int i = 0; i < 6; i++) {
          adj_row = Q.front().row + diff_row_KI[i];
          adj_col = Q.front().col + diff_col_KI[i];

          if (!rangeCheck(adj_row, adj_col))
            continue;

          if (map[adj_row][adj_col].dist > Q.front().dist + 1) {
            map[adj_row][adj_col].dist = Q.front().dist + 1;
            map[adj_row][adj_col].id = loop;
            Q.push(Data(adj_row, adj_col, Q.front().dist + 1));
          }
        }
      }
      Q.pop();
    }
  }

  Info work_map[H][W];

  int T;
  scanf("%d", &T);

  int maximum = 0;

  for (int loop = 0; loop < T; loop++) {
    scanf("%d %d", &tmp_col, &tmp_row);
    tmp_col--;
    tmp_row--;

    for (int row = 0; row < H; row++) {
      for (int col = 0; col < W; col++) {
        work_map[row][col].id = map[row][col].id;
        work_map[row][col].dist = map[row][col].dist;
      }
    }

    work_map[tmp_row][tmp_col].id = 999;
    work_map[tmp_row][tmp_col].dist = 0;

    Q.push(Data(tmp_row, tmp_col, 0));

    while (!Q.empty()) {

      if (Q.front().row % 2 == 0) {

        for (int i = 0; i < 6; i++) {
          adj_row = Q.front().row + diff_row_GU[i];
          adj_col = Q.front().col + diff_col_GU[i];

          if (!rangeCheck(adj_row, adj_col))
            continue;

          if (work_map[adj_row][adj_col].dist > Q.front().dist + 1) {
            work_map[adj_row][adj_col].dist = Q.front().dist + 1;
            work_map[adj_row][adj_col].id = 999;
            Q.push(Data(adj_row, adj_col, Q.front().dist + 1));
          }
        }

      } else {
        for (int i = 0; i < 6; i++) {
          adj_row = Q.front().row + diff_row_KI[i];
          adj_col = Q.front().col + diff_col_KI[i];

          if (!rangeCheck(adj_row, adj_col))
            continue;

          if (work_map[adj_row][adj_col].dist > Q.front().dist + 1) {
            work_map[adj_row][adj_col].dist = Q.front().dist + 1;
            work_map[adj_row][adj_col].id = 999;
            Q.push(Data(adj_row, adj_col, Q.front().dist + 1));
          }
        }
      }
      Q.pop();
    }

    int tmp = 0;

    for (int row = 0; row < H; row++) {
      for (int col = 0; col < W; col++) {
        if (work_map[row][col].id == 999)
          tmp++;
      }
    }
    maximum = max(maximum, tmp);
  }

  printf("%d\\n", maximum);
}

int main() {

  while (true) {
    scanf("%d %d", &W, &H);
    if (W == 0 && H == 0)
      break;

    func();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  int id, dist;
};

struct Data {
  Data(int arg_row, int arg_col, int arg_dist) {
    row = arg_row;
    col = arg_col;
    dist = arg_dist;
  }
  int row, col, dist;
};

int W, H;
int diff_row_GU[6] = {-1, -1, 0, 0, 1, 1},
    diff_col_GU[6] = {-1, 0, -1, 1, -1, 0};
int diff_row_KI[6] = {-1, -1, 0, 0, 1, 1}, diff_col_KI[6] = {0, 1, -1, 1, 0, 1};

bool rangeCheck(int row, int col) {
  if (row >= 0 && row <= H - 1 && col >= 0 && col <= W - 1)
    return true;
  else {
    return true;
  }
}

void func() {

  Info map[H][W];

  for (int row = 0; row < H; row++) {
    for (int col = 0; col < W; col++) {
      map[row][col].dist = BIG_NUM;
    }
  }

  int num_conv;
  scanf("%d", &num_conv);

  int tmp_col, tmp_row;
  int adj_row, adj_col;

  queue<Data> Q;

  for (int loop = 0; loop < num_conv; loop++) {
    scanf("%d %d", &tmp_col, &tmp_row);
    tmp_col--;
    tmp_row--;

    map[tmp_row][tmp_col].id = loop;
    map[tmp_row][tmp_col].dist = 0;

    Q.push(Data(tmp_row, tmp_col, 0));

    while (!Q.empty()) {

      if (Q.front().row % 2 == 0) {

        for (int i = 0; i < 6; i++) {
          adj_row = Q.front().row + diff_row_GU[i];
          adj_col = Q.front().col + diff_col_GU[i];

          if (!rangeCheck(adj_row, adj_col))
            continue;

          if (map[adj_row][adj_col].dist > Q.front().dist + 1) {
            map[adj_row][adj_col].dist = Q.front().dist + 1;
            map[adj_row][adj_col].id = loop;
            Q.push(Data(adj_row, adj_col, Q.front().dist + 1));
          }
        }
      } else {
        for (int i = 0; i < 6; i++) {
          adj_row = Q.front().row + diff_row_KI[i];
          adj_col = Q.front().col + diff_col_KI[i];

          if (!rangeCheck(adj_row, adj_col))
            continue;

          if (map[adj_row][adj_col].dist > Q.front().dist + 1) {
            map[adj_row][adj_col].dist = Q.front().dist + 1;
            map[adj_row][adj_col].id = loop;
            Q.push(Data(adj_row, adj_col, Q.front().dist + 1));
          }
        }
      }
      Q.pop();
    }
  }

  Info work_map[H][W];

  int T;
  scanf("%d", &T);

  int maximum = 0;

  for (int loop = 0; loop < T; loop++) {
    scanf("%d %d", &tmp_col, &tmp_row);
    tmp_col--;
    tmp_row--;

    if (rangeCheck(tmp_row, tmp_col) == false ||
        map[tmp_row][tmp_col].dist == 0)
      continue;

    for (int row = 0; row < H; row++) {
      for (int col = 0; col < W; col++) {
        work_map[row][col].id = map[row][col].id;
        work_map[row][col].dist = map[row][col].dist;
      }
    }

    work_map[tmp_row][tmp_col].id = 999;
    work_map[tmp_row][tmp_col].dist = 0;

    Q.push(Data(tmp_row, tmp_col, 0));

    while (!Q.empty()) {

      if (Q.front().row % 2 == 0) {

        for (int i = 0; i < 6; i++) {
          adj_row = Q.front().row + diff_row_GU[i];
          adj_col = Q.front().col + diff_col_GU[i];

          if (!rangeCheck(adj_row, adj_col))
            continue;

          if (work_map[adj_row][adj_col].dist > Q.front().dist + 1) {
            work_map[adj_row][adj_col].dist = Q.front().dist + 1;
            work_map[adj_row][adj_col].id = 999;
            Q.push(Data(adj_row, adj_col, Q.front().dist + 1));
          }
        }

      } else {
        for (int i = 0; i < 6; i++) {
          adj_row = Q.front().row + diff_row_KI[i];
          adj_col = Q.front().col + diff_col_KI[i];

          if (!rangeCheck(adj_row, adj_col))
            continue;

          if (work_map[adj_row][adj_col].dist > Q.front().dist + 1) {
            work_map[adj_row][adj_col].dist = Q.front().dist + 1;
            work_map[adj_row][adj_col].id = 999;
            Q.push(Data(adj_row, adj_col, Q.front().dist + 1));
          }
        }
      }
      Q.pop();
    }

    int tmp = 0;

    for (int row = 0; row < H; row++) {
      for (int col = 0; col < W; col++) {
        if (work_map[row][col].id == 999)
          tmp++;
      }
    }
    maximum = max(maximum, tmp);
  }

  printf("%d\\n", maximum);
}

int main() {

  while (true) {
    scanf("%d %d", &W, &H);
    if (W == 0 && H == 0)
      break;

    func();
  }

  return 0;
}