[구현] 백준 2564 경비원
2021. 3. 6. 09:55ㆍ알고리즘 문제분석
수행 시간 : 50분
실버 1 난이도의 구현 문제였다. 확실히 구현이나 시뮬레이션 문제들은 레벨에 따라 체감난이도가 확 와닫는데 이번 문제도 크게 어렵진 않았다. 다만 규칙이나 이런 것들을 찾아서 코드를 확 줄일 수 있었지만, 타임어택으로 문제를 푸는 과정이므로 생각나는대로 코드를 짜다보니 지저분하고 길어졌다.
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
int r, c, n;
int dong_dir, dong_dis;
int sol(int x, int y) {
int ans = 0;
if (dong_dir == 1) {
if (x == 1) { //북
ans = abs(y - dong_dis);
}
else if (x == 2) { // 남
ans = min(c + y + dong_dis, (r - y) + c + (r - dong_dis));
}
else if (x == 3) { // 서
ans = dong_dis + y;
}
else if (x == 4) { // 동
ans = (r - dong_dis) + y;
}
}
else if (dong_dir == 2) {
if (x == 1) { //북
ans = min(dong_dis + c + y, (r - dong_dis) + c + (r - y));
}
else if (x == 2) { // 남
ans = abs(y - dong_dis);
}
else if (x == 3) { // 서
ans = (c - y) + dong_dis;
}
else if (x == 4) { // 동
ans = (r - dong_dis) + (c - y);
}
}
else if (dong_dir == 3) {
if (x == 1) { //북
ans = dong_dis + y;
}
else if (x == 2) { // 남
ans = c - dong_dis + y;
}
else if (x == 3) { // 서
ans = abs(dong_dis - y);
}
else if (x == 4) { // 동
ans = min(dong_dis + r + y, (c - dong_dis) + r + (c - y));
}
}
else if (dong_dir == 4) {
if (x == 1) { //북
ans = dong_dis + r - y;
}
else if (x == 2) { // 남
ans = c - dong_dis + r - y;
}
else if (x == 3) { // 서
ans = min(dong_dis + r + y, (c - dong_dis) + r + (c - y));
}
else if (x == 4) { // 동
ans = abs(dong_dis - y);
}
}
return ans;
}
int main() {
vector <int> dir, dis;
int cnt = 0;
cin >> r >> c >> n;
dir.resize(n);
dis.resize(n);
for (int i = 0;i < n;i++) {
cin >> dir[i] >> dis[i];
}
cin >> dong_dir >> dong_dis;
for (int i = 0;i < n;i++) {
cnt += sol(dir[i], dis[i]);
}
cout << cnt;
}
'알고리즘 문제분석' 카테고리의 다른 글
[DP&DFS] 백준 1890 점프 (0) | 2021.03.08 |
---|---|
[완전탐색] 백준 1051 숫자 정사각형 (0) | 2021.03.06 |
[완전탐색] 백준 3085번 사탕 게임 (0) | 2021.02.22 |
[DFS] 백준 1325 효율적인 해킹 (0) | 2021.02.18 |
[구현] 백준 14891 톱니바퀴 (0) | 2021.02.17 |