[프로그래머스] level.2 - 무인도 여행(C++)

2023. 5. 23. 23:29·🖥️ Study Note/Coding Test

https://school.programmers.co.kr/learn/courses/30/lessons/154540

내 해답

#include <string>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

int ctoi(char c)
{
	return int(c - '0');
}

vector<int> solution(vector<string> maps)
{
	vector<int>          answer;
	vector<vector<bool>> visit(maps.size(), vector<bool>(maps[0].size(), false));
	int                  dx[4] = {1, -1, 0, 0};
	int                  dy[4] = {0, 0, 1, -1};

	queue<pair<int, int>> q;

	for (int y = 0; y < maps.size(); ++y)
    {
	    for (int x = 0; x < maps[0].size(); ++x)
        {
	        if (!visit[y][x] && 'X' != maps[y][x])
		        q.push({x,y});
            
            int weight = 0;
	        while (!q.empty())
	        {
		        int x = q.front().first;
		        int y = q.front().second;
		        q.pop();

		        if (visit[y][x])
			        continue;
                
                weight += ctoi(maps[y][x]);
		        visit[y][x] = true;

		        for (int i = 0; i < 4; ++i)
		        {
			        int nx = x + dx[i];
			        int ny = y + dy[i];

			        if (nx >= 0 && nx < maps[0].size() && ny >= 0 && ny < maps.size())
			        {
				        if (!visit[ny][nx] && 'X' != maps[ny][nx])
				        {
					        q.push({nx, ny});
				        }
			        }
		        }
	        }
            
            if(weight != 0)
                answer.push_back(weight);
        }
    }
    
    if(answer.empty())
        answer.push_back(-1);
    else
	    sort(answer.begin(), answer.end());

	return answer;
}
저작자표시 비영리 변경금지 (새창열림)

'🖥️ Study Note > Coding Test' 카테고리의 다른 글

[프로그래머스] level.3 - 최고의 집합(C++)  (0) 2023.05.31
[프로그래머스] level.2 - 귤 고르기(C++)  (0) 2023.05.30
[프로그래머스] level.2 - 땅따먹기(C++)  (0) 2023.05.22
[프로그래머스] level.3 - 경주로 건설(C++)  (1) 2023.05.19
[프로그래머스] level.3 - 선입 선출 스케줄링 (C++)  (0) 2023.05.18
'🖥️ Study Note/Coding Test' 카테고리의 다른 글
  • [프로그래머스] level.3 - 최고의 집합(C++)
  • [프로그래머스] level.2 - 귤 고르기(C++)
  • [프로그래머스] level.2 - 땅따먹기(C++)
  • [프로그래머스] level.3 - 경주로 건설(C++)
Beankong_
Beankong_
주니어 클라이언트 프로그래머 공부 기록
  • Beankong_
    Beankong's Devlog
    Beankong_
  • 전체
    오늘
    어제
    • 전체 글 (146)
      • ⛅ Daily (0)
      • 🖥️ Study Note (2)
        • C++ (1)
        • Unreal Engine (5)
        • Coding Test (123)
        • Design Patteren (5)
        • VCS (Git..) (1)
        • Server (1)
      • 🧭 Devlog (8)
        • 오답노트 (4)
        • UE5 GameLift Server Test Project (1)
        • TIL (3)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      unrealengine build system
      UnrealEngine
      프료그래머스
      그리디(greedy)
      코딩테스트
      UnrealEngine5
      OnlineSubsystem
      알고리즘
      programmers
      최단 거리 알고리즘
      헬테이커
      게임 프로그래밍
      게임프로그래밍
      propertyaccess
      게임 개발
      게임 모작
      프로그래머스
      그래프 순회
      cpp
      unrealengine module
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.3
    Beankong_
    [프로그래머스] level.2 - 무인도 여행(C++)
    상단으로

    티스토리툴바