Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 위상정렬
- 백트래킹
- 투 포인터
- mst
- 수학
- 그리디
- 세그먼트 트리
- 스택
- Kotlin
- CS
- 동적계획법
- swea
- 이분탐색
- 유니온 파인드
- Network
- BFS
- 문자열
- 시뮬레이션
- Effective Java
- java
- dfs
- 후니의 쉽게 쓴 시스코 네트워킹
- 백준
- 플로이드-와샬
- 구현
- 알고리즘
- JUnit 5
- 에라토스테네스의 체
- 완전탐색
- 프로그래머스
Archives
반갑습니다!
[백준] 3987 보이저 1호 본문
풀이
단순 시뮬레이션 문제이다. 4방향을 모두 확인해보고 N*M
초가 지나도 항성계 내부에 있는 경우는 무한히 전파되는 것으로 처리하면 된다.
코드
#include <iostream>
#include <algorithm>
using namespace std;
int n, m, pr, pc;
char space[501][501];
const int dx[] = { 0, 1, 0, -1 }, dy[] = { -1, 0, 1, 0 };
bool inRange(int x, int y) {
return (1 <= x && x <= m) && (1 <= y && y <= n);
}
int change_dir(char planet, int cur_dir) {
if (planet == '/') {
if (cur_dir == 0) return 1;
if (cur_dir == 1) return 0;
if (cur_dir == 2) return 3;
if (cur_dir == 3) return 2;
}
else {
if (cur_dir == 0) return 3;
if (cur_dir == 1) return 2;
if (cur_dir == 2) return 1;
if (cur_dir == 3) return 0;
}
}
int simulation(int dir) {
int x = pc;
int y = pr;
int d = dir;
int cnt = 0;
while(true){
if (cnt > n * m) break;
if(!inRange(x, y) || space[y][x] == 'C') return cnt;
if (space[y][x] == '/' || space[y][x] == '\\') d = change_dir(space[y][x], d);
x += dx[d]; y += dy[d];
cnt++;
}
return 987654321;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> space[i][j];
cin >> pr >> pc;
int ans = 0, dir;
for (int i = 0; i < 4; i++) {
int tmp = simulation(i);
if (ans < tmp) {
ans = tmp;
dir = i;
}
}
if (dir == 0) cout << "U\n";
else if (dir == 1) cout << "R\n";
else if (dir == 2) cout << "D\n";
else cout << "L\n";
if (ans == 987654321) cout << "Voyager\n";
else cout << ans << '\n';
return 0;
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[백준] 14502 연구소 (0) | 2020.04.30 |
---|---|
[백준] 2174 로봇 시뮬레이션 (0) | 2020.04.30 |
[백준] 9655 돌 게임 (0) | 2020.04.29 |
[백준] 16197 두 동전 (0) | 2020.04.29 |
[백준] 14500 테트로미노 (0) | 2020.04.29 |