728x90
https://www.acmicpc.net/problem/10026
(1) Python
import sys
from collections import deque
input = sys.stdin.readline
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
def dfs(start_x, start_y, flag):
queue = deque()
queue.append((start_x, start_y))
if flag == 0:
visited_normal[start_x][start_y] = 1
else:
visited_not[start_x][start_y] = 1
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < N and 0 <= ny < N:
if flag == 0 and visited_normal[nx][ny] == 0: #일반인의 경우
if colors[nx][ny] == colors[x][y]:
visited_normal[nx][ny] = 1
queue.append((nx, ny))
elif flag == 1 and visited_not[nx][ny] == 0: #색약의 경우
if colors[x][y] in ("R","G"):
if colors[nx][ny] in ("R","G"):
visited_not[nx][ny] = 1
queue.append((nx, ny))
else:
if colors[nx][ny] == colors[x][y]:
visited_not[nx][ny] = 1
queue.append((nx, ny))
N = int(input())
colors = list()
answer = [0, 0]
visited_normal = [[0] * N for _ in range(N)]
visited_not = [[0] * N for _ in range(N)]
for i in range(N):
color = input().rstrip()
li = list()
for c in color:
li.append(c)
colors.append(li)
for i in range(N):
for j in range(N):
if visited_normal[i][j] == 0:
dfs(i, j, 0)
answer[0] += 1
# print(answer)
if visited_not[i][j] == 0:
dfs(i, j, 1)
answer[1] += 1
# print(answer)
print(answer[0], answer[1], end = " ")
(2) C/C++
#include <iostream>
#include <algorithm>
#include <vector>
#define FIO cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define MAX 101
using namespace std;
int N;
char MAP[MAX][MAX];
bool Visit[MAX][MAX];
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
void Input()
{
cin >> N;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> MAP[i][j];
}
}
}
void BFS(int a, int b)
{
queue<pair<int, int>> Q;
Q.push(make_pair(a, b));
Visit[a][b] = true;
while (Q.empty() == 0)
{
int x = Q.front().first;
int y = Q.front().second;
Q.pop();
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < N && ny < N)
{
if (Visit[nx][ny] == false)
{
if (MAP[nx][ny] == MAP[x][y])
{
Visit[nx][ny] = true;
Q.push(make_pair(nx, ny));
}
}
}
}
}
}
void Solution()
{
int Answer, Answer2;
Answer = Answer2 = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (Visit[i][j] == false)
{
BFS(i, j);
Answer++;
}
}
}
memset(Visit, false, sizeof(Visit));
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (MAP[i][j] == 'G') MAP[i][j] = 'R';
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (Visit[i][j] == false)
{
BFS(i, j);
Answer2++;
}
}
}
cout << Answer << " " << Answer2 << endl;
}
void Solve()
{
Input();
Solution();
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
Solve();
return 0;
}
728x90
'Study > Coding Test' 카테고리의 다른 글
[백준] 1504 - 특정한 최단 경로 Python (1) | 2023.10.12 |
---|---|
[백준] 20920 - 영단어 암기는 괴로워 Python, C/C++ (0) | 2023.10.12 |
[백준] 2470 - 두 용액 Python (0) | 2023.10.11 |
[백준] 1717 - 집합의 표현 Python, C/C++ (1) | 2023.10.11 |
[백준] 1764 - 듣보잡 Python (0) | 2023.10.11 |