ShovelingLife
A Game Programmer
ShovelingLife
전체 방문자
오늘
어제
  • 분류 전체보기 (1067)
    • 그래픽스 (57)
      • 공통 (19)
      • 수학 물리 (22)
      • OpenGL & Vulkan (1)
      • DirectX (14)
    • 게임엔진 (180)
      • Unreal (69)
      • Unity (100)
      • Cocos2D-X (3)
      • 개인 플젝 (8)
    • 코딩테스트 (221)
      • 공통 (7)
      • 프로그래머스 (22)
      • 백준 (162)
      • LeetCode (19)
      • HackerRank (2)
      • 코딩테스트 알고리즘 (8)
    • CS (235)
      • 공통 (21)
      • 네트워크 (44)
      • OS & 하드웨어 (55)
      • 자료구조 & 알고리즘 (98)
      • 디자인패턴 (6)
      • UML (4)
      • 데이터베이스 (7)
    • 프로그래밍 언어 (346)
      • C++ (167)
      • C# (88)
      • Java (9)
      • Python (33)
      • SQL (30)
      • JavaScript (8)
      • React (7)
    • 그 외 (9)
      • Math (5)
      • 일상 (5)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Source Code 좌측 상단에 복사 버튼 추가 완료
  • 언리얼 엔진 C++ 빌드시간 단축 꿀팁
  • 게임 업계 코딩테스트 관련
  • 1인칭 시점으로 써내려가는 글들

인기 글

태그

  • 언리얼
  • 알고리즘
  • C++
  • 백준
  • 그래픽스
  • 배열
  • string
  • SQL
  • C
  • 티스토리챌린지
  • 함수
  • 문자열
  • Unity
  • 유니티
  • 오블완
  • 파이썬
  • 포인터
  • 클래스
  • 프로그래머스
  • c#

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ShovelingLife

A Game Programmer

코딩테스트/백준

[실1] 2667 - 단지 번호 붙이기

2022. 6. 19. 18:58
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
 
using namespace std;
 
#define TOTAL_SIZE 5000
#define y first
#define x second
 
pair<int,int> dir[]
{
    { 1, 0 }, // 상
    {-1, 0 }, // 하
    { 0,-1 }, // 좌
    { 0, 1}   // 우
};
 
vector<int> res(TOTAL_SIZE);
int map[TOTAL_SIZE][TOTAL_SIZE]{ 0 };
bool visited[TOTAL_SIZE][TOTAL_SIZE];
int b_size = 0;
int idx = 0;
 
void BFS(int _y, int _x)
{
    // 탐색 시작
    queue<pair<int,int>> q;
    q.push({ _y, _x });
    visited[_y][_x] = true;
    res[idx]++;
 
    while (!q.empty())
    {
        auto front = q.front(); q.pop();
 
        for (int i = 0; i < 4; i++)
        {
            // 다음 좌표
            int ny = front.y + dir[i].y, nx = front.x + dir[i].x;
 
            // 상하좌우 검사
            if (ny < 0       ||
                ny >= b_size ||
                nx < 0       ||
                nx >= b_size)
                continue;
 
            if (!visited[ny][nx] &&
                map[ny][nx] == 1)
            {
                visited[ny][nx] = true;
                q.push({ ny,nx });
                res[idx]++;
            }
        }
    }
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> b_size;
 
    // 문자열 입력
    vector<string> s(b_size);
    for (int i = 0; i < b_size; i++)
        cin >> s[i];
    
    // char형을 int형으로 캐스팅 대입
    for (int y = 0; y < b_size; y++)
    {
        for (int x = 0; x < b_size; x++)
            map[y][x] = s[y][x] - '0';
    }
    // BFS
    for (int y = 0; y < b_size; y++)
    {
        for (int x = 0; x < b_size; x++)
        {
            if (!visited[y][x] &&
                map[y][x] == 1)
            {
                idx++;
                BFS(y, x);
            }
        }
    }
    sort(res.begin(), res.end());
    cout << idx << endl;
    for (auto item : res)
    {
        if (item > 0)
            cout << item << endl;
    }
    return 0;
}
저작자표시 (새창열림)

'코딩테스트 > 백준' 카테고리의 다른 글

[실1] 11286 - 절대값 힙  (0) 2022.06.21
[실1] 1991 - 트리 순회  (0) 2022.06.20
[실2] 1406 - 에디터  (0) 2022.06.19
[실1] 1926 - 그림  (0) 2022.06.19
[실1] 2178 - 미로탐색  (0) 2022.06.19
    '코딩테스트/백준' 카테고리의 다른 글
    • [실1] 11286 - 절대값 힙
    • [실1] 1991 - 트리 순회
    • [실2] 1406 - 에디터
    • [실1] 1926 - 그림
    ShovelingLife
    ShovelingLife
    Main skill stack => Unity C# / Unreal C++ Studying Front / BackEnd, Java Python

    티스토리툴바