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 |
Tags
- boj 3190
- 백준 16234
- 백준 17779
- BOJ
- 연구소3
- 시뮬레이션
- 헷갈리는 용어
- 백준 뱀
- simulation
- 백준2174
- 브루트포스
- boj 15685
- BOJ 17142
- 백준 연구소3
- boj 연구소3
- 백준 3190
- 알고리즘
- C
- Bruteforce
- 삼성문제
- 백준 인구 이동
- 백준 게리맨더링 2
- dfs
- 삼성 문제
- 로봇 시뮬레이션
- 완전탐색
- 구현
- 백준 로봇 시뮬레이션
- C++
- boj 16234
Archives
- Today
- Total
나의 공부장
백준 2174 로봇 시뮬레이션 [Simulation] 본문
문제 링크: https://www.acmicpc.net/problem/2174
2174번: 로봇 시뮬레이션
문제 가로 A(1≤A≤100), 세로 B(1≤B≤100) 크기의 땅이 있다. 이 땅 위에 로봇들이 N(1≤N≤100)개 있다. 로봇들의 초기 위치는 x좌표와 y좌표로 나타난다. 위의 그림에서 보듯 x좌표는 왼쪽부터, y좌표
www.acmicpc.net
문제 풀이
기존에 풀던 (x, y) 좌표와는 살짝 달라서 헷갈릴 수 있지만, 문제의 설명대로 따라서 구현하면 됩니다.
x좌표는 왼쪽에서 오른쪽으로 가면 증가하고
y좌표는 아래에서 위로 올라가면 증가합니다.
로봇의 상태를 구조체로 만들어서 벡터로 보관해서,
그 이후에 명령을 하나씩 실행하면서 그에 따른 구현을 해주면 됩니다.
따로 visited라는 2차원 배열 pair를 만들었는데, 이 부분은 해당 로봇이 명령에 의해서 움직인 공간에 이미 다른 로봇이 있는 경우를 체크해주기 위해서 구현했습니다.
사실 다 구현해놓고 보니까 코드가 지저분하네요. 반복되는 부분을 함수화하는 능력이 아직 부족한 것 같습니다.
소스 코드
#include <iostream>
#include <vector>
using namespace std;
typedef struct
{
int x, y;
char direction;
}Robot;
int A, B, N, M; // 변수
int a[101][101]; // 맵
pair<int, bool> visited[101][101]; // (로봇 번호, 방문 여부)
vector<Robot> robots(101); // 로봇상태
bool ok = true; // 시뮬레이션 검증
void show(Robot& now)
{
cout << "[" << now.x << "," << now.y << "]" << ' ' << now.direction << endl;
}
int main()
{
//freopen("input.txt", "r", stdin);
cin >> A >> B >> N >> M;
// 초기
for (int i = 1; i <= N; i++)
{
cin >> robots[i].x >> robots[i].y >> robots[i].direction;
visited[robots[i].x][robots[i].y].first = i;
visited[robots[i].x][robots[i].y].second = true;
}
// 명령
for (int i = 0; i < M; i++)
{
int robot_num, loop;
char command;
cin >> robot_num >> command >> loop;
Robot& now = robots[robot_num]; // 현재 로봇
int nx, ny; // 명령이 수행되었을 때의 좌표
// 명령어는 총 F, R, L
if (command == 'F')
{
if (now.direction == 'N')
{
for (int j = 0; j < loop; j++)
{
nx = now.x; ny = now.y + 1;
// 다른 로봇이 있는지 체크
if (visited[nx][ny].second == true)
{
cout << "Robot " << robot_num << " crashes into robot " << visited[nx][ny].first << endl;
ok = false;
break;
}
// 경계 체크
if (nx >= 1 && ny >= 1 && nx <= A && ny <= B)
{
visited[now.x][now.y].second = false;
visited[nx][ny].second = true;
now.x = nx;
now.y = ny;
}
else
{
cout << "Robot " << robot_num << " crashes into the wall" << endl;
ok = false;
break;
}
}
}
else if (now.direction == 'E')
{
for (int j = 0; j < loop; j++)
{
nx = now.x + 1; ny = now.y;
if (visited[nx][ny].second == true)
{
cout << "Robot " << robot_num << " crashes into robot " << visited[nx][ny].first << endl;
ok = false;
break;
}
if (nx >= 1 && ny >= 1 && nx <= A && ny <= B)
{
visited[now.x][now.y].second = false;
visited[nx][ny].second = true;
now.x = nx;
now.y = ny;
}
else
{
cout << "Robot " << robot_num << " crashes into the wall" << endl;
ok = false;
break;
}
}
}
else if (now.direction == 'W')
{
for (int j = 0; j < loop; j++)
{
nx = now.x - 1; ny = now.y;
if (visited[nx][ny].second == true)
{
cout << "Robot " << robot_num << " crashes into robot " << visited[nx][ny].first << endl;
ok = false;
break;
}
if (nx >= 1 && ny >= 1 && nx <= A && ny <= B)
{
visited[now.x][now.y].second = false;
visited[nx][ny].second = true;
now.x = nx;
now.y = ny;
}
else
{
cout << "Robot " << robot_num << " crashes into the wall" << endl;
ok = false;
break;
}
}
}
else if (now.direction == 'S')
{
for (int j = 0; j < loop; j++)
{
nx = now.x; ny = now.y - 1;
if (visited[nx][ny].second == true)
{
cout << "Robot " << robot_num << " crashes into robot " << visited[nx][ny].first << endl;
ok = false;
break;
}
if (nx >= 1 && ny >= 1 && nx <= A && ny <= B)
{
visited[now.x][now.y].second = false;
visited[nx][ny].second = true;
now.x = nx;
now.y = ny;
}
else
{
cout << "Robot " << robot_num << " crashes into the wall" << endl;
ok = false;
break;
}
}
}
// 충돌발생!
if (ok == false) break;
}
else if (command == 'R')
{
for (int j = 0; j < loop; j++)
{
if (now.direction == 'N') now.direction = 'E';
else if (now.direction == 'S') now.direction = 'W';
else if (now.direction == 'W') now.direction = 'N';
else if (now.direction == 'E') now.direction = 'S';
}
}
else if (command == 'L')
{
for (int j = 0; j < loop; j++)
{
if (now.direction == 'N') now.direction = 'W';
else if (now.direction == 'S') now.direction = 'E';
else if (now.direction == 'W') now.direction = 'S';
else if (now.direction == 'E') now.direction = 'N';
}
}
}
if (ok) cout << "OK" << endl;
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
백준 16234 인구 이동 [BruteForce] (0) | 2020.05.19 |
---|---|
백준 15685 드래곤 커브 [Simulation] (0) | 2020.05.18 |
백준 2798 블랙잭 [BruteForce] (0) | 2020.04.28 |
백준 14499 주사위 굴리기 [Simulation] (0) | 2020.04.28 |
백준 1182 부분 수열의 합 [BruteForce] (0) | 2020.04.26 |