Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

struct coord {
  int x;
  int y;
};
int H, W;
char map[1005][1005];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ans;
long long blackcount;
std::queue<coord> queue_next;
void bfs() {
  while (blackcount < H * W) {
    std::queue<coord> queue(queue_next);
    while (queue.size()) {
      coord thiscoord = queue.front();
      queue.pop();
      for (int i = 0; i < 4; i++) {
        if (thiscoord.x + dx[i] >= 0 && thiscoord.x + dx[i] < H &&
            thiscoord.y + dy[i] >= 0 && thiscoord.y + dy[i] < W) {
          if (map[thiscoord.x + dx[i]][thiscoord.y + dy[i]] == '.') {
            map[thiscoord.x + dx[i]][thiscoord.y + dy[i]] = '#';
            queue_next.push((coord){thiscoord.x + dx[i], thiscoord.y + dy[i]});
            blackcount++;
            // printf("map[%d][%d] is pushed to
            // stack\n",thiscoord.x+dx[i],thiscoord.y+dy[i]);
          }
        }
      }
    }
    ans++;
    // printf("ans=%d\n",ans);
  }
  return;
}

int main() {

  scanf("%d", &H);
  scanf("%d", &W);
  // printf("H=%d, W=%d\n",H,W);
  std::string S;
  blackcount = 0;
  for (int i = 0; i < H; i++) {
    std::cin >> S;
    // printf("S=%s\n",S.c_str());
    for (int j = 0; j < W; j++) {
      map[i][j] = S[j];
      if (S[j] == '#') {
        queue_next.push((coord){i, j});
        blackcount++;
      }
    }
  }
  // printf("read end\n");
  //   printf("blackcount=%lld\n",blackcount);
  bfs();
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

struct coord {
  int x;
  int y;
};
int H, W;
char map[1005][1005];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ans;
long long blackcount;
std::queue<coord> queue_next;
void bfs() {
  while (blackcount < H * W) {
    std::queue<coord> queue(queue_next);
    while (queue.size()) {
      coord thiscoord = queue.front();
      queue.pop();
      queue_next.pop();
      for (int i = 0; i < 4; i++) {
        if (thiscoord.x + dx[i] >= 0 && thiscoord.x + dx[i] < H &&
            thiscoord.y + dy[i] >= 0 && thiscoord.y + dy[i] < W) {
          if (map[thiscoord.x + dx[i]][thiscoord.y + dy[i]] == '.') {
            map[thiscoord.x + dx[i]][thiscoord.y + dy[i]] = '#';
            queue_next.push((coord){thiscoord.x + dx[i], thiscoord.y + dy[i]});
            blackcount++;
            // printf("map[%d][%d] is pushed to
            // stack\n",thiscoord.x+dx[i],thiscoord.y+dy[i]);
          }
        }
      }
    }
    ans++;
    // printf("ans=%d\n",ans);
  }
  return;
}

int main() {

  scanf("%d", &H);
  scanf("%d", &W);
  // printf("H=%d, W=%d\n",H,W);
  std::string S;
  blackcount = 0;
  for (int i = 0; i < H; i++) {
    std::cin >> S;
    // printf("S=%s\n",S.c_str());
    for (int j = 0; j < W; j++) {
      map[i][j] = S[j];
      if (S[j] == '#') {
        queue_next.push((coord){i, j});
        blackcount++;
      }
    }
  }
  // printf("read end\n");
  //   printf("blackcount=%lld\n",blackcount);
  bfs();
  printf("%d\n", ans);
  return 0;
}