Created
May 14, 2019 17:53
-
-
Save whatalnk/fc0762791b5e52dbe7125242ba577b3d to your computer and use it in GitHub Desktop.
AtCoder AGC #033 A - Darker and Darker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <queue> | |
#include <string> | |
using namespace std; | |
const int MAX_H = 1000; | |
const int MAX_W = 1000; | |
typedef pair<int, int> P; | |
typedef pair<P, int> PP; | |
int H, W; | |
char field[MAX_H][MAX_W]; | |
bool visited[MAX_H][MAX_W]; | |
queue<PP> que; | |
int solve() { | |
int cnt; | |
PP pp; | |
P p; | |
int i, j, ni, nj; | |
int di[4] = {-1, 0, 1, 0}; | |
int dj[4] = {0, 1, 0, -1}; | |
while (que.size()) { | |
pp = que.front(); | |
que.pop(); | |
p = pp.first; | |
cnt = pp.second; | |
i = p.first; | |
j = p.second; | |
for (int d = 0; d < 4; d++) { | |
ni = i + di[d]; | |
nj = j + dj[d]; | |
if (ni >= 0 && ni < H && nj >= 0 && nj < W && !visited[ni][nj] && | |
field[ni][nj] == '.') { | |
que.push(make_pair(make_pair(ni, nj), cnt + 1)); | |
visited[ni][nj] = true; | |
field[ni][nj] = '#'; | |
} | |
} | |
} | |
return cnt; | |
} | |
int main() { | |
cin >> H >> W; | |
char cell; | |
for (int i = 0; i < H; i++) { | |
for (int j = 0; j < W; j++) { | |
cin >> cell; | |
if (cell == '#') { | |
visited[i][j] = true; | |
que.push(make_pair(make_pair(i, j), 0)); | |
} else { | |
visited[i][j] = false; | |
} | |
field[i][j] = cell; | |
} | |
} | |
cout << solve() << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment