[알고리즘 문제 풀기] 필터(1895)

C#
lhs's avatar
Jan 22, 2025
[알고리즘 문제 풀기] 필터(1895)
notion image

1. 문제 풀이 아이디어

  • 필터 범위에 포함된 값을 배열에 담고, 이를 정렬한 뒤 중간값을 계산해 문제를 해결한다.

2. 나의 정답 코드

StreamReader reader = new StreamReader(Console.OpenStandardInput()); StreamWriter writer = new StreamWriter(Console.OpenStandardOutput()); int result = 0; string[] split = reader.ReadLine().Split(); int r = int.Parse(split[0]); int c = int.Parse(split[1]); int[,] image = new int[r,c]; for (int i = 0; i < r; i++) { split = reader.ReadLine().Split(); for(int j = 0; j < c; j++) { image[i, j] = int.Parse(split[j]); } } int[] f = new int[9]; int t = int.Parse(reader.ReadLine()); for (int i = 0; i < r - 2; i++) { for (int j = 0; j < c - 2; j++) { for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { f[x * 3 + y] = image[i + x,j + y]; } } Array.Sort(f); if (f[4] >= t) result++; } } writer.WriteLine(result); writer.Close(); reader.Close();

3. 정리

  • 입력받은 이미지를 2차원 배열로 저장한 뒤, 3x3 크기의 필터를 이동시키며 각 영역의 값을 배열 f에 담는다.
  • 배열 f를 정렬해 중간값인 f[4]를 구하고, 이 값이 t 이상이면 result를 1 증가시킨다.
  • 모든 처리가 끝난 후 result를 출력하여 문제를 해결한다.
Share article

LHS's Study Space