그럴듯한 개발 블로그
Published 2023. 9. 18. 22:23
백준 15683 감시 c++ <algorithm>/백준
반응형

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

 

15683번: 감시

스타트링크의 사무실은 1×1크기의 정사각형으로 나누어져 있는 N×M 크기의 직사각형으로 나타낼 수 있다. 사무실에는 총 K개의 CCTV가 설치되어져 있는데, CCTV는 5가지 종류가 있다. 각 CCTV가 감

www.acmicpc.net

첨 풀어보는 시뮬레이션 문제였다. 시뮬레이션이 뭔가.. 했더니 그냥 빡구현였다. bfs dfs 처음 배우던 몇 개월 전 이 문제가 통곡의 벽이였는데, 42과제 겁나 하다 보니 구현은 매우 이지했다. 물론 디버깅 하는덴 한세월 걸렸다.

처음 cctv좌표를 가지고 옮기면서 x y좌표를 다시 초기화 시켜주지 않는 바람에 모든 cctv가 오른쪽만 보게 되어버렸다;;

역시 프린트찍어보는게 최고인듯 하다.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <deque>
#include <unordered_set>
#include <cstring>
#include <utility>
#include <climits> // int32max

using namespace	std;
int dx[4] = { 0, 1 ,0, -1}; // 오 아 왼 위
int dy[4] = { 1, 0 ,-1, 0};
vector<pair<int, int> > cctv;
vector<vector<int> > board;
int col, row, res = INT32_MAX;

int countZero() {
	int cnt = 0;
	for (int i = 0; i < col; i++)
		for (int j = 0; j < row; j++)
			if (board[i][j] == 0)
				cnt++;
	return cnt;
}

void changeMonitoringArea(int x, int y, int curDx, int curDy) {
	while (x < col && x >= 0 && y < row && y >= 0 && board[x][y] != 6) {
		if (board[x][y] == 0)
			board[x][y] = 7; // 감시영역 7로 표시
		x += curDx;
		y += curDy;
	}
}

void runCctv(int cctvIdx, int direction) {
	int x = cctv[cctvIdx].first;
	int y = cctv[cctvIdx].second;
	int cctvType = board[x][y];
	int curDx, curDy;

	if (true) {// 오른쪽
		curDx = dx[(0 + direction) % 4];
		curDy = dy[(0 + direction) % 4];
		changeMonitoringArea(x, y, curDx, curDy);
	}
	if (cctvType == 2 || cctvType == 4 || cctvType == 5) {// 왼쪽
		curDx = dx[(2 + direction) % 4];
		curDy = dy[(2 + direction) % 4];
		changeMonitoringArea(x, y, curDx, curDy);
	}
	if (cctvType == 3 || cctvType == 4 || cctvType == 5) {// 위
		curDx = dx[(3 + direction) % 4];
		curDy = dy[(3 + direction) % 4];
		changeMonitoringArea(x, y, curDx, curDy);
	}
	if (cctvType == 5) {// 아래
		curDx = dx[(1 + direction) % 4];
		curDy = dy[(1 + direction) % 4];
		changeMonitoringArea(x, y, curDx, curDy);
	}
}

void dfs(int depth) { // cctv 돌면서 4방향 보고있을 때 다 생각할거임
	if (depth == cctv.size()) { // cctv 다 봤음
		res = min(countZero(), res);
		return ;
	}
	for (int i = 0; i < 4; i++) {
		vector<vector<int> > backup(board);
		runCctv(depth, i);
		dfs(depth + 1);
		board = backup;
	}
}

int	main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

	// 6 == 벽
	cin >> col >> row;
	for (int i = 0; i < col; i++) {
		vector<int> v;
		for (int j = 0; j < row; j++) {
			int input;
			cin >> input;
			v.push_back(input);
			if (input >= 1 && input <= 5)
				cctv.push_back(make_pair(i, j)); // cctv 배열에 추가해주기
		}
		board.push_back(v);
	}
	dfs(0);
	cout << res;
}
반응형

'<algorithm> > 백준' 카테고리의 다른 글

백준 12100 2048(Easy) c++  (2) 2023.09.21
백준 18808 스티커 붙이기 c++  (0) 2023.09.21
백준 1107 리모컨 c++  (0) 2023.09.17
백준 18111 마인크래프트 c++  (0) 2023.09.17
백준 18110 solved.ac c++  (0) 2023.09.17
profile

그럴듯한 개발 블로그

@donghyk2

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!