반응형
https://www.acmicpc.net/problem/6087
6087번: 레이저 통신
크기가 1×1인 정사각형으로 나누어진 W×H 크기의 지도가 있다. 지도의 각 칸은 빈 칸이거나 벽이며, 두 칸은 'C'로 표시되어 있는 칸이다. 'C'로 표시되어 있는 두 칸을 레이저로 통신하기 위해서
www.acmicpc.net
[ 문제풀이 ]
1. 맵을 입력받고, 문의 위치를 vector에 저장합니다.
2. visited [ y ][ x ][ dir ] 배열을 만들어 방문 여부를 저장합니다.
3. pos struct를 만들어 y좌표, x좌표, 빛의 방향, 거울의 개수를 저장합니다.
4. dijkstra를 이용하여 거울의 개수가 적은 순으로 priority_queue에서 뽑고, '.'을 만나면 pq에 넣습니다.
5. vector에 저장된 문의 좌표와 현재 좌표가 일치하면 cnt의 값을 출력해줍니다.
[ 소스코드 ]
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include<iostream> #include<queue> #include<vector> using namespace std; int W, H; char arr[101][101]; int dy[4] = { -1,1,0,0 }; int dx[4] = { 0,0,-1,1 }; int visited[101][101][4]; vector<pair<int, int>> laser; struct pos { int y; int x; int cnt; int dir; }; struct cmp { bool operator()(pos right, pos left) { return left.cnt < right.cnt; } }; int main() { scanf("%d %d", &W, &H); for (int i = 0; i < H; i++) { scanf("%s", arr[i]); } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (arr[i][j] == 'C') { laser.emplace_back(i, j); } } } priority_queue<pos, vector<pos>, cmp> pq; for (int i = 0; i < 4; i++) { pq.push({ laser[0].first, laser[0].second, 0, i }); } while (!pq.empty()) { const int y = pq.top().y; const int x = pq.top().x; const int cnt = pq.top().cnt; const int dir = pq.top().dir; pq.pop(); if (visited[y][x][dir] == 1) continue; visited[y][x][dir] = 1; if (y == laser[1].first && x == laser[1].second) { printf("%d", cnt); return 0; } for (int i = 0; i < 4; i++) { const int yy = y + dy[i]; const int xx = x + dx[i]; if (yy >= 0 && yy < H && xx >= 0 && xx < W) { if (arr[yy][xx] == '*') continue; if (visited[yy][xx][dir] != 1) { if (i != dir) { pq.push({ yy,xx,cnt + 1,i }); } else { pq.push({ yy,xx,cnt,i }); } } } } } } | cs |
반응형
'백준' 카테고리의 다른 글
[ 백준 ] 15591번 - MooTube (Silver) (C++) (0) | 2022.11.26 |
---|---|
[ 백준 ] 4991번 - 로봇 청소기 (C++) (0) | 2022.11.25 |
[ 백준 ] 4485번 - 녹색 옷 입은 애가 젤다지? (C++) (0) | 2022.11.23 |
[ 백준 ] 2151번 - 거울 설치 (C++) (0) | 2022.11.22 |
[ 백준 ] 17386번 - 선분 교차 1 (C++) (0) | 2022.11.21 |