[구현] 백준 2564 경비원

2021. 3. 6. 09:55알고리즘 문제분석

www.acmicpc.net/problem/2564

 

2564번: 경비원

첫째 줄에 블록의 가로의 길이와 세로의 길이가 차례로 주어진다. 둘째 줄에 상점의 개수가 주어진다. 블록의 가로의 길이와 세로의 길이, 상점의 개수는 모두 100이하의 자연수이다. 이어 한 줄

www.acmicpc.net

수행 시간 : 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;
}