본문 바로가기
전공공부/코딩테스트

(c++) 백준 "2564. 경비원"

by 시아나 2022. 5. 2.

https://www.acmicpc.net/problem/2564

 

2564번: 경비원

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

www.acmicpc.net


#include <iostream>
#include <vector>

using namespace std;
int w, h, n;
vector<vector<int>> list;

void solution() {
	int result = 0;
	vector<int> now = list[list.size() - 1];
	for (int i = 0; i < list.size() - 1; i++) {
		int sum = 0;
		vector<int> togo = list[i];
		int diff = abs(now[0] - togo[0]);
		if (diff == 2) { //정반대
			sum += togo[1] + togo[2] + now[1] + now[2];
		}
		else { //인접 //같은 곳
			sum += abs(togo[1] - now[1]);
			sum += abs(togo[2] - now[2]);
		}
		result += min(sum, 2 * (w + h) - sum);
	}
	cout << result << endl;
}

int main() {
	cin >> w >> h >> n;
	for (int i = 0; i <= n; i++) {
		int a, b; cin >> a >> b;
		switch (a) {
		case 1: //북
			list.push_back({ 1,0,b });
			break;
		case 2: //남
			list.push_back({ 3,h,b });
			break;
		case 3: //서
			list.push_back({ 4,b,0 });
			break;
		case 4: //동
			list.push_back({ 2,b,w });
			break;
		}
	}
	solution();

	return 0;
}