Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준
- 누구나큰일낼수있어
- 백준 1992번 풀이
- 백준 2630번
- JPA
- 스파르타코딩클럽 #spartacodingclub #누구나큰일낼수있어
- 1시간 만에 끝내는 직장인 코딩 용어
- SQL
- 백준 1992번
- 스프링 입문을 위한 자바 객체 지향의 원리와 이해
- spartacodingclub
- 스프링 시큐리티 구조
- 김영한님
- 팀300
- 스파르타코딩클럽
- thymeleaf
- 전문가를 위한 스프링5
- 스프링 MVC 2편
- 타임리프
- 스프링
- 스프링 핵심원리 기본편
- 전문가를 위한 스프링
- C++
- 자바의 정석 기초편
- 백준 #N과 M(4) #백트래킹
- 객체지향의 사실과 오해
- 스프링심화반
- 알고리즘
- 가상 채팅서버
- 채팅서버 설계
Archives
- Today
- Total
민우의 코딩노트
10026번 적록색약 - C++ 본문
✅️[문제]
https://www.acmicpc.net/problem/10026
✅️[풀이]
BFS 문제
적록색약이 아닌 경우, 이중 for문을 돌려 방문횟수가 없는 경우 ans+=1을 해주고, 큐에 넣어 BFS를 진행한다.
적록색약인 경우, 앞서와 같이 이중 for문을 돌려 방문횟수가 없는 경우 ans+=1을 해주고, 큐에 넣는다. BFS를 진행할때 색이 R or G인 경우에 B가 나오면 continue를 시키면 된다!
✅️[코드]
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
string arr[105];
bool vis[105][105];
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for(int i=0; i<n; i++){
cin >> arr[i];
}
queue<pair<int,int>> q;
int ans = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(vis[i][j] == 0) {
q.push({i,j});
ans++;
}
while(!q.empty()){
pair<int,int> q2 = q.front(); q.pop();
for(int i=0; i<4; i++){
int x = q2.X + dx[i];
int y = q2.Y + dy[i];
if(x < 0 || x>=n || y < 0 || y>=n) continue;
if(vis[x][y] != 0 || arr[q2.X][q2.Y] != arr[x][y]) continue;
vis[x][y] = 1;
q.push({x,y});
}
}
}
}
cout << ans << ' ';
memset(vis, 0, sizeof(vis));
ans = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(vis[i][j] == 0){
q.push({i,j});
ans++;
}
while(!q.empty()){
pair<int,int> q2 = q.front(); q.pop();
for(int i=0; i<4; i++){
int x = q2.X + dx[i];
int y = q2.Y + dy[i];
if(x<0|| x>=n || y<0|| y>=n) continue;
if(arr[q2.X][q2.Y] == 'G' || arr[q2.X][q2.Y] == 'R'){
if(vis[x][y] != 0 || arr[x][y] == 'B') continue;
}
else{
if(vis[x][y] != 0 || arr[x][y] != 'B') continue;
}
vis[x][y] = 1;
q.push({x,y});
}
}
}
}
cout << ans;
}
'Algorithm > BOJ' 카테고리의 다른 글
5427번 불 - C++ (0) | 2022.08.02 |
---|---|
1012번 유기농 배추 - C++ (0) | 2022.07.31 |
1697번 숨바꼭질 - C++ (0) | 2022.07.29 |
7576번 토마토 - C++ (0) | 2022.07.29 |
4179번 불! - C++ (0) | 2022.07.29 |