https://www.acmicpc.net/problem/16234
삼성 SW 역량 테스트 기출 문제 라는 문제집에 있는 문제입니다.
이 문제도 생각보다 많이 쉬웠습니다.
우선 map에 있는 연합을 하나 찾고, 그 연합의 값을 전부 바꿉니다. 이때, visited가 큰 역할을 합니다. 바꾼 연합을 또 바꾸는 일이 사라지거든요. 이 과정을 계속 반복하니 풀렸습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <iostream> #include <queue> #include <cmath> using namespace std; int main(){ ios_base::sync_with_stdio(false); int map[50][50]; int N,L,R; bool chk = true; int dx[4] = {1,-1,0,0}; int dy[4] = {0,0,1,-1}; int ans = 0; queue<pair<int,int>> q; //bfs용 queue<pair<int,int>> pos; //연합의 좌표를 저장 cin >> N >> L >> R; for(int i = 0; i < N; i++){ for(int j = 0; j < N; j++) cin >> map[i][j]; } while(chk){ bool visited[50][50] = {}; chk = false; ans+=1; for(int i = 0; i < N; i++){ for(int j = 0; j < N; j++){ if(visited[i][j]) continue; int sum = map[i][j]; q.push({j,i}); pos.push({j,i}); visited[i][j] = 1; while(!q.empty()){ int x_tmp = q.front().first; int y_tmp = q.front().second; q.pop(); for(int k = 0; k < 4; k++){ int x = x_tmp+dx[k]; int y = y_tmp+dy[k]; if(x < 0 || y < 0 || x >= N || y >= N || visited[y][x] || abs(map[y_tmp][x_tmp]-map[y][x]) < L ||abs(map[y_tmp][x_tmp]-map[y][x])>R) continue; chk = true; q.push({x,y}); pos.push({x,y}); sum+=map[y][x]; visited[y][x] = 1; } } int avr = sum/pos.size(); while(!pos.empty()){ map[pos.front().second][pos.front().first] = avr; pos.pop(); } } } } cout << ans-1; } | cs |
반응형
'프로그래밍 > 문제풀이' 카테고리의 다른 글
[bfs] 백준 13460 구슬 탈출2 (0) | 2018.11.28 |
---|---|
[etc] 백준 13456 시험 감독 (0) | 2018.11.26 |
[bfs] 백준 3055 탈출 (0) | 2018.11.23 |
[bfs] 백준 5014 스타트링크 (0) | 2018.11.21 |
[etc] 백준 1350 진짜 공간 (0) | 2018.11.21 |