반응형
https://www.acmicpc.net/problem/16234
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#define ABS(a) ((a<0)?(a*-1):(a))
using namespace std;
int N, L, R;
int map[51][51];
int visited[51][51];
int sum[2503][2];
int groupNumber;
int dy[] = { 1,0,-1,0 };
int dx[] = { 0,1,0,-1 };
queue <pair<int,int > > q;
void bfs(int x, int y,int gn) {
q.push(make_pair(x, y));
visited[x][y] = gn;
while (!q.empty()) {
int cx = q.front().first;
int cy = q.front().second;
sum[gn][0]++;
sum[gn][1] += map[cx][cy];
q.pop();
for (int i = 0; i < 4; i++) {
int nx = cx + dx[i];
int ny = cy + dy[i];
int diff = ABS((map[cx][cy] - map[nx][ny]));
if (nx >= 0 && ny >= 0 && nx < N && ny < N && diff >= L && diff <= R && visited[nx][ny] == 0) {
q.push(make_pair(nx, ny));
visited[nx][ny] = gn;
}
}
}
}
int main() {
int t=0;
scanf("%d%d%d", &N, &L, &R);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
scanf("%d", &map[i][j]);
}
}
while (1) {
int groupNumber = 1;
memset(sum, 0, sizeof(sum));
memset(visited, 0, sizeof(visited));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (visited[i][j] == 0) {
bfs(i,j,groupNumber);
groupNumber++;
}
}
}
if (groupNumber-1 >= N * N) {
printf("%d", t);
break;
}
t++;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int chk = visited[i][j];
map[i][j] = sum[chk][1] / sum[chk][0];
}
}
}
}
반응형
'CS > 알고리즘' 카테고리의 다른 글
백준 14500 테트로미노 (0) | 2020.01.24 |
---|---|
백준 17142 연구소3(bfs dfs) (0) | 2019.10.27 |
백준 9019 DSLR(bfs) (0) | 2019.10.27 |
백준 3055 탈출 (0) | 2019.10.27 |
백준 1987 알파벳(dfs)_알파벳 아스키코드 (0) | 2019.10.27 |