15683번 감시

동도리동·2021년 10월 13일
0

코딩테스트

목록 보기
59/76

맞았넹.. 벡터를 함수로 건네줄 때 call by value로 할지, reference로 할지 많이 헷갈린다..
value로 주게 되면 건네주기 전의 벡터값은 바뀌지 않지만, reference로 주게되면 건네주기전의 벡터값이 바뀌기 때문에 주의가 필요할듯??

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
int n, m, k;
int map[8][8];
vector<pair<int, int> > v;
int ans = -1;
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };
void f(vector<vector<int> > ch) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << ch[i][j] << " ";
		}
		cout << '\n';
	}
	cout << '\n';
}
vector<vector<int> > go(int dir, vector<vector<int> > ch, int x, int y) {
	while (1) {
		int nx = x + dx[dir];
		int ny = y + dy[dir];
		if (nx >= n || nx < 0 || ny >= m || ny < 0) break;
		if (map[nx][ny] == 6) break;
		ch[nx][ny] = 1;
		x = nx;
		y = ny;
	}
	
	return ch;
}
void DFS(int L, vector<vector<int> > ch) {
	int sum = 0;
	if (L == k) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (ch[i][j] == 0 && map[i][j]==0) sum++;
			}
		}
		if (ans == -1 || ans > sum) ans = sum;
		
	}
	else {
		int x, y;
		tie(x, y) = v[L];
		if (map[x][y] == 1) {
			for (int i = 0; i < 4; i++) {
				vector<vector<int> > ch2 = ch;
				ch2 = go(i, ch2, x, y);
				DFS(L + 1, ch2);
			}
		}
		else if (map[x][y] == 2) {
			for (int i = 0; i < 2; i++) {
				vector<vector<int> > ch2 = ch;
				ch2 = go(i, ch2, x, y);
				ch2 = go(i + 2, ch2, x, y);
				DFS(L + 1, ch2);
			}
		}
		else if (map[x][y] == 3) {
			for (int i = 0; i < 4; i++) {
				vector<vector<int> > ch2 = ch;
				ch2 = go(i, ch2, x, y);
				ch2 = go((i + 1)%4, ch2, x, y);
				DFS(L + 1, ch2);
			}
		}
		else if (map[x][y] == 4) {
			for (int i = 0; i < 4; i++) {
				vector<vector<int> > ch2 = ch;
				ch2 = go((i + 1) % 4, ch2, x, y);
				ch2 = go((i + 2) % 4, ch2, x, y);
				ch2 = go((i + 3) % 4, ch2, x, y);
				DFS(L + 1, ch2);
			}
		}
		else if (map[x][y] == 5) {
			vector<vector<int> > ch2 = ch;
			ch2 = go(0, ch2, x, y);
			ch2 = go(1, ch2, x, y);
			ch2 = go(2, ch2, x, y);
			ch2 = go(3, ch2, x, y);
			DFS(L + 1, ch2);
		}
	}
}
int main() {
	//freopen("in1.txt", "rt", stdin);
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> map[i][j];
			if (map[i][j] >= 1 && map[i][j] < 6) {
				v.push_back(make_pair(i, j));
			}
		}
	}
	k = v.size();
	vector<vector<int> > ch(n, vector<int>(m));
	DFS(0,ch);
	cout << ans << '\n';

	return 0;
}
profile
긍정코딩세상

0개의 댓글

관련 채용 정보