일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 대구맛집
- SQL
- 대구카페
- 수성못삼겹살
- 안지랑카페
- BFS
- 큐
- 프로그래머스
- 정렬
- 대구삼겹살
- oracle
- 대구고깃집
- 범어동맛집
- 수성못맛집
- 서울맛집
- 앞산카페
- 오라클
- 브루트 포스
- 백준
- 대명동맛집
- programmers
- 별찍기
- C#
- 대구데이트
- 수성구맛집
- 반복문
- 조건문
- 압구정데이트
- 들안길삼겹살
- 수성구데이트
- Today
- Total
모든 일상
C# 백준 2583번 문제 영역 구하기 본문
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApp3
{
public class Program
{
static void Main(string[] args)
{
var input = Console.ReadLine().Split();
int size_y = int.Parse(input[0]);
int size_x = int.Parse(input[1]);
int test_case = int.Parse(input[2]);
int[,] data = new int[100, 100];
int[,] visit = new int[100, 100];
int[] dx = new int[4] { 1, 0, -1, 0 };
int[] dy = new int[4] { 0, 1, 0, -1 };
Queue<(int,int)> q = new Queue<(int,int)>(); // X,Y 축 위치 저장
Queue<int> result = new Queue<int>(); // 넓이 결과 저장
int num = 0;
for (int i = 0; i<test_case; i++) // x축으로 반전되어 값이 들어감 (문제를 푸는데 지장없어 그냥 진행)
{
string[] str = Console.ReadLine().Split();
for (int j = int.Parse(str[1]); j < int.Parse(str[3]); j++ )
{
for (int k = int.Parse(str[0]); k < int.Parse(str[2]); k++)
{
data[j, k] = 1;
}
}
}
for (int i = 0; i< size_y; i++)
{
for (int j =0; j< size_x; j++)
{
if (visit[i,j] == 0 && data[i,j] == 0)
{
int cnt = 0;
cnt++;
num++;
q.Enqueue((i, j));
while(q.Count != 0)
{
var buff = q.Dequeue();
int check_x = buff.Item2;
int check_y = buff.Item1;
visit[check_y, check_x] = 1;
for (int k = 0; k<4; k++)
{
int x = check_x + dx[k];
int y = check_y + dy[k];
if (x < 0 || x >= size_x || y < 0 || y >= size_y) continue;
if (visit[y, x] == 1 || data[y, x] == 1) continue;
visit[y, x] = 1;
cnt++;
q.Enqueue((y, x));
}
}
result.Enqueue(cnt);
}
}
}
int[] arr = new int[result.Count];
int z = 0;
while(result.Count != 0)
{
arr[z] = result.Dequeue();
z++;
}
Array.Sort(arr);
Console.WriteLine(num);
for (int i = 0; i<arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
}
}
}
'코딩 공부 > C#' 카테고리의 다른 글
C# 2566번 문제 최댓값 (0) | 2023.01.09 |
---|---|
C# 백준 2667번 문제 단지번호붙이기 (0) | 2022.08.10 |
C# 백준 7562번 문제 나이트의 이동 (0) | 2022.08.05 |
C# 백준 1696번 문제 숨바꼭질 (0) | 2022.08.05 |
C# 백준 9012번 문제 괄호 (0) | 2022.07.28 |