[백준 2931번 C/C++] 가스관
[백준 2931번 C/C++] 가스관

https://www.acmicpc.net/problem/2931
2931번: 가스관
www.acmicpc.net
해결전략
구현
정답코드
#include <iostream> #include <vector> #include <queue> using namespace std; // 북서남동 int dirY[4] = { -1, 0, 1, 0 }; int dirX[4] = { 0, -1, 0, 1 }; int r, c; // r: 행 c: 열 vector<vector<char>> v; // 격자판 pair<int, int> Moscow, Zagreb; // 'M'과 'Z'의 위치 int resultY, resultX; // 누락된 파이프의 위치 struct Coor { int y, x, dir; }; void FindLostPipe() { // 누락된 파이프 부분을 찾아서 그 종류를 결정하고 출력하는 함수 int move[4] = { 0 }; // .인 곳의 4방향을 탐색하여 연결 가능성이 있는지 저장하는 배열 for (int dir = 0; dir < 4; dir++) { // 4방향에 대하여 탐색하며 연결 가능한 파이프가 있는지 확인 int ny = resultY + dirY[dir]; int nx = resultX + dirX[dir]; if (dir == 0) { if (v[ny][nx] == '|' || v[ny][nx] == '+' || v[ny][nx] == '1' || v[ny][nx] == '4') move[dir] = 1; } else if (dir == 1) { if (v[ny][nx] == '-' || v[ny][nx] == '+' || v[ny][nx] == '1' || v[ny][nx] == '2') move[dir] = 1; } else if (dir == 2) { if (v[ny][nx] == '|' || v[ny][nx] == '+' || v[ny][nx] == '2' || v[ny][nx] == '3') move[dir] = 1; } else if (dir == 3) { if (v[ny][nx] == '-' || v[ny][nx] == '+' || v[ny][nx] == '3' || v[ny][nx] == '4') move[dir] = 1; } } // 연결 가능한 파이프의 종류에 따라 해당 위치와 파이프 종류를 출력 if (move[0] && move[1] && move[2] && move[3]) cout << resultY << " " << resultX << " " << "+"; else if (move[0] && !move[1] && move[2] && !move[3]) cout << resultY << " " << resultX << " " << "|"; else if (!move[0] && move[1] && !move[2] && move[3]) cout << resultY << " " << resultX << " " << "-"; else if (!move[0] && !move[1] && move[2] && move[3]) cout << resultY << " " << resultX << " " << "1"; else if (move[0] && !move[1] && !move[2] && move[3]) cout << resultY << " " << resultX << " " << "2"; else if (move[0] && move[1] && !move[2] && !move[3]) cout << resultY << " " << resultX << " " << "3"; else if (!move[0] && move[1] && move[2] && !move[3]) cout << resultY << " " << resultX << " " << "4"; } // 'M'에서 시작하여 'Z'까지의 경로를 찾는 함수. 중간에 누락된 파이프 위치를 resultY, resultX에 저장 int CheckDirection(int y, int x) { int d = -1; for (int i = 0; i < 4; i++) { int ny = y + dirY[i]; int nx = x + dirX[i]; if (ny < 1 || r < ny || nx < 1 || c < nx || v[ny][nx] == '.') continue; if (i == 0) { if (v[ny][nx] == '|' || v[ny][nx] == '+' || v[ny][nx] == '1' || v[ny][nx] == '4') d = 0; } else if (i == 1) { if (v[ny][nx] == '-' || v[ny][nx] == '+' || v[ny][nx] == '1' || v[ny][nx] == '2') d = 1; } else if (i == 2) { if (v[ny][nx] == '|' || v[ny][nx] == '+' || v[ny][nx] == '2' || v[ny][nx] == '3') d = 2; } else if (i == 3) { if (v[ny][nx] == '-' || v[ny][nx] == '+' || v[ny][nx] == '3' || v[ny][nx] == '4') d = 3; } } return d; } void Flow() { int y = Moscow.first; int x = Moscow.second; int dir = CheckDirection(y, x); if (dir == -1) { y = Zagreb.first; x = Zagreb.second; dir = CheckDirection(y, x); } queue<Coor> Q; Q.push({ y, x, dir }); while (!Q.empty()) { y = Q.front().y; x = Q.front().x; dir = Q.front().dir; Q.pop(); int ny = y + dirY[dir]; int nx = x + dirX[dir]; int nd; if (ny < 1 || r < ny || nx < 1 || c < nx) continue; if (v[ny][nx] == '.') { resultY = ny; resultX = nx; return; } if (dir == 0) { // 북 if (v[ny][nx] == '|' || v[ny][nx] == '+') nd = 0; // 북 else if (v[ny][nx] == '1') nd = 3; // 동 else if (v[ny][nx] == '4') nd = 1; // 서 } else if (dir == 1) { // 서 if (v[ny][nx] == '-' || v[ny][nx] == '+') nd = 1; // 서 else if (v[ny][nx] == '1') nd = 2; // 남 else if (v[ny][nx] == '2') nd = 0; // 북 } else if (dir == 2) { // 남 if (v[ny][nx] == '|' || v[ny][nx] == '+') nd = 2; // 남 else if (v[ny][nx] == '2') nd = 3; // 동 else if (v[ny][nx] == '3') nd = 1; // 서 } else if (dir == 3) { // 동 if (v[ny][nx] == '-' || v[ny][nx] == '+') nd = 3; // 동 else if (v[ny][nx] == '3') nd = 0; // 북 else if (v[ny][nx] == '4') nd = 2; // 남 } Q.push({ ny, nx, nd }); } return; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> r >> c; v.resize(r+2, vector<char>(c+2)); ch.resize(r+2, vector<int>(c+2)); for (int y = 1; y <= r; y++) { for (int x = 1; x <= c; x++) { cin >> v[y][x]; if (v[y][x] == 'M') Moscow = { y, x }; else if (v[y][x] == 'Z') Zagreb = { y, x }; } } Flow(); FindLostPipe(); return 0; }
'⭐ 코딩테스트 > 백준' 카테고리의 다른 글
[백준 8980번 C/C++] 택배 (0) | 2024.04.10 |
---|---|
[백준 24337번 C/C++] 가희와 탑 (0) | 2024.04.09 |
[백준 17825번 C/C++] 주사위 윷놀이 (0) | 2024.04.03 |
[백준 17136번 C/C++] 색종이 붙이기 (0) | 2024.03.26 |
[백준 2623번 C/C++] 음악 프로그램 (0) | 2024.03.21 |
댓글
이 글 공유하기
다른 글
-
[백준 8980번 C/C++] 택배
[백준 8980번 C/C++] 택배
2024.04.10[백준 8980번 C/C++] 택배 https://www.acmicpc.net/problem/8980 8980번: 택배 입력의 첫 줄은 마을 수 N과 트럭의 용량 C가 빈칸을 사이에 두고 주어진다. N은 2이상 2,000이하 정수이고, C는 1이상 10,000이하 정수이다. 다음 줄에, 보내는 박스 정보의 개수 M이 주어진다. M은 1이 www.acmicpc.net 해결전략 그리디 Greedy Algorithm 각 택배를 가능한 한 먼저 배달해야 하는 목적지 마을 순으로 정렬. 각 마을별 남은 용량 계산 트럭이 각 마을을 방문할 때마다 특정 용량만큼의 박스를 싣고 내린다. 이를 관리하기 위해 capacity 벡터를 사용하여 각 마을별로 남은 용량을 관리한다. 초기값은 트럭의 최대 용량인 c로 설정한다. … -
[백준 24337번 C/C++] 가희와 탑
[백준 24337번 C/C++] 가희와 탑
2024.04.09 -
[백준 17825번 C/C++] 주사위 윷놀이
[백준 17825번 C/C++] 주사위 윷놀이
2024.04.03 -
[백준 17136번 C/C++] 색종이 붙이기
[백준 17136번 C/C++] 색종이 붙이기
2024.03.26
댓글을 사용할 수 없습니다.