-
[C++] Programmers | 무인도 여행Algorithm/Programmers 2023. 2. 5. 12:01
무인도 여행
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
✅ Accept Code
// programmers week3-2 // 무인도 여행 // BFS #include <bits/stdc++.h> using namespace std; int island[100][100]; bool visited[100][100]; vector<pair<int, int>> V; vector<int> days; int di[4] = {0, 0, 1, -1}; int dj[4] = {1, -1, 0, 0}; vector<int> solution(vector<string> maps) { for (int i = 0; i < maps.size(); i++) { for (int j = 0; j < maps[i].size(); j++) { if (maps[i][j] == 'X') continue; island[i][j] = maps[i][j] - '0'; V.push_back({i, j}); } } for (auto ele: V) { if (!island[ele.first][ele.second] || visited[ele.first][ele.second]) continue; int day = 0; queue<pair<int, int>> Q; Q.push(ele); while (!Q.empty()) { pair<int, int> cur = Q.front(); Q.pop(); day += island[cur.first][cur.second]; visited[cur.first][cur.second] = true; for (int idx = 0; idx < 4; idx++) { int new_i = cur.first + di[idx]; int new_j = cur.second + dj[idx]; if (new_i < 0 || new_i > maps.size() - 1 || new_j < 0 || new_j > maps[0].size() - 1) continue; if (!island[new_i][new_j] || visited[new_i][new_j]) continue; visited[new_i][new_j] = true; Q.push({new_i, new_j}); } } days.push_back(day); } sort(days.begin(), days.end()); if (days.size() == 0) days.push_back(-1); return days; } int main() { solution({"X591X", "X1X5X", "X231X", "1XXX1"}); }
728x90'Algorithm > Programmers' 카테고리의 다른 글
[C++] Programmers | 124 나라의 숫자 (0) 2023.03.01 [C++] Programmers | k진수에서 소수 개수 구하기 (0) 2023.03.01 [C++] Programmers | 구명보트 (0) 2023.02.05 [C++] Programmers study week #3 (0) 2023.02.05 [C++] Programmers | 괄호 회전하기 (2) 2023.02.05