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
- 스택
- Effective Java
- 유니온 파인드
- BFS
- 이분탐색
- 구현
- 위상정렬
- Kotlin
- 후니의 쉽게 쓴 시스코 네트워킹
- 플로이드-와샬
- dfs
- 동적계획법
- 완전탐색
- 시뮬레이션
- 수학
- mst
- 알고리즘
- 프로그래머스
- 그리디
- swea
- 세그먼트 트리
- 백트래킹
- 문자열
- JUnit 5
- CS
- 에라토스테네스의 체
- java
- Network
- 투 포인터
- 백준
Archives
반갑습니다!
[백준] 2174 로봇 시뮬레이션 본문
풀이
문제의 조건에 맞춰서 구현하면 된다. 방향 전환을 쉽게 구현하기 위해 숫자로 저장하였다.
코드
#include <iostream>
#include <map>
using namespace std;
struct robot {
int x, y, dir;
};
int a, b, n, m;
robot r[101];
int dx[] = { 0, 1, 0, -1 }, dy[] = { 1, 0, -1, 0 };
bool inRange(int x, int y) {
return (1 <= x && x <= a) && (1 <= y && y <= b);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> a >> b;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int tmp1, tmp2;
char tmp3;
cin >> tmp1 >> tmp2 >> tmp3;
r[i].x = tmp1;
r[i].y = tmp2;
if (tmp3 == 'N') r[i].dir = 0;
else if (tmp3 == 'E') r[i].dir = 1;
else if (tmp3 == 'S') r[i].dir = 2;
else r[i].dir = 3;
}
for (int i = 0; i < m; i++) {
int num, repeat;
char cmd;
cin >> num >> cmd >> repeat;
for (int j = 0; j < repeat; j++) {
if (cmd == 'F') {
r[num].x += dx[r[num].dir];
r[num].y += dy[r[num].dir];
if (!inRange(r[num].x, r[num].y)) {
cout << "Robot " << num << " crashes into the wall\n";
exit(0);
}
for (int l = 1; l <= n; l++) {
if (l == num) continue;
if (r[num].x == r[l].x && r[num].y == r[l].y) {
cout << "Robot " << num << " crashes into robot " << l << '\n';
exit(0);
}
}
}
else if (cmd == 'R') r[num].dir = (r[num].dir + 1) % 4;
else r[num].dir = (r[num].dir - 1 + 4) % 4;
}
}
cout << "OK\n";
return 0;
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[백준] 11404 플로이드 (0) | 2020.05.01 |
---|---|
[백준] 14502 연구소 (0) | 2020.04.30 |
[백준] 3987 보이저 1호 (0) | 2020.04.30 |
[백준] 9655 돌 게임 (0) | 2020.04.29 |
[백준] 16197 두 동전 (0) | 2020.04.29 |