Notice
Recent Posts
Recent Comments
-
[삼성SW테스트] 백준 14503번 - 로봇 청소기 (정답률 50%) 본문
https://www.acmicpc.net/problem/14503
삼성 SW테스트 기출 문제다.
유형은 시뮬레이션이며, 문제에서 요구하는 그대로 조건을 구현하면 된다.
나같은 경우 정말 조건에서 주어진 순서 그대로 코딩하였는데, 순서 흐름은 while 내에서 continue와 카운트 변수를 이용했다. 문제에서 주어진 조건의 중요성을 다시금 깨닫게해주는 문제였다. 바로 풀려서 기분좋다.
아래는 전체 소스코드 (0ms)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, M;
int r, c, d; // d : 0123(NESW)
int answer = 0;
bool Move = true;
int turn_cnt = 0;
//pair<int, bool> map[50][50];
vector<vector<pair<int, bool> > > map;
typedef struct{
int dr, dc;
}dir;
dir direction[4] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } }; // NESW
void clean(int row, int col){
if (!map[row][col].second){
map[row][col].second = true;
answer++;
}
}
void getAnswer(){
while (Move){
clean(r, c);
if (turn_cnt == 4){
int opposite_d;
if (d > 1) opposite_d = d - 2;
else if(d == 0) opposite_d = 2;
else if (d == 1) opposite_d = 3;
int n_r = r + direction[opposite_d].dr;
int n_c = c + direction[opposite_d].dc;
if (n_r >= 0 && n_r < N && n_c >= 0 && n_c < M){
// 내부에 있지만, 뒤가 벽인경우 정지.
if (map[n_r][n_c].first){
Move = false;
break;
}
}
if (n_r < 0 || n_r >= N || n_c < 0 || n_c >= M){
// 후방이 맵 바깥(벽)인 경우.
Move = false;
break;
} // 2 - d
// 2 - c
turn_cnt = 0;
r = n_r;
c = n_c;
continue;
}
// 1. 현 위치 청소
clean(r, c);
// 2. 2번 - 1. 왼쪽 회전.
int temp_d;
if (d == 0)
temp_d = 3;
else
temp_d = d - 1;
int n_r = r + direction[temp_d].dr;
int n_c = c + direction[temp_d].dc;
if (n_r >= 0 && n_r < N && n_c >= 0 && n_c < M){
// 2-a. 청소할 공간이 있다면 해당 공간으로 이동하고 청소한 뒤 다시 맨 위로.
if (!map[n_r][n_c].second && map[n_r][n_c].first == 0){
d = temp_d;
r = n_r;
c = n_c;
clean(r, c);
map[r][c].second = true;
turn_cnt = 0;
continue; // 다시 1번으로. (2-a).
}
}
// 2 - b. 범위내에 있지만 1인 경우거나 이미 청소한 경우 회전만 하고 다시 while 시작.
if (n_r >= 0 && n_r < N && n_c >= 0 && n_c < M){
if (map[n_r][n_c].first == 1 || map[n_r][n_c].second){
d = temp_d;
turn_cnt++;
continue;
}
}
// 2 - b. 범위 바깥에 있는 경우 : 회전만 하고 다시 while 시작
if (n_r < 0 || n_r >= N || n_c < 0 || n_c >= M){
d = temp_d;
turn_cnt++;
continue;
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
cin >> r >> c >> d;
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++){
int a;
cin >> a;
map[i][j].first = a; // 빈 공간 or 벽
map[i][j].second = false; // 방문 여부
}
}
getAnswer();
cout << answer << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
'1-1. 삼성 SW 테스트' 카테고리의 다른 글
[삼성SW테스트] 백준 14501번 - 퇴사 (정답률 46%) (0) | 2020.01.13 |
---|---|
[삼성SW테스트] 백준 14502번 - 연구소 (정답률 54%) (0) | 2020.01.13 |
[삼성SW테스트] 백준 14888번 - 연산자 끼워넣기 (정답률 47%) (0) | 2020.01.11 |
[삼성SW테스트] 백준 14889번 - 스타트와 링크 (정답률 50%) (0) | 2020.01.11 |
[삼성SW테스트] 백준 14891번 - 톱니바퀴 (정답률 50%) (0) | 2020.01.10 |
Comments