[C++] SWEA 2112 : [모의 SW 역량테스트] 보호 필름
Updated:

1. 문제 풀이
- 한 행에
약품 처리 x / A 약품 처리 / B 약품 처리로 3가지 경우의 수가 있고 모두 확인해야 함.
2.3 전체 코드
#include<iostream>
#include <functional>
#include <cstring>
using namespace std;
int result = 987654321;
int D, W, K;
bool cell[13][20] = { 0, }; //A=true, B=false
bool check() {
for (int i = 0; i < W; i++) {
int colcnt = 1;
bool col = cell[0][i];
for (int j = 1; j < D; j++) {
if (col == cell[j][i]) { //이전 cell과 같다면
colcnt++;
if (colcnt == K)
break;
}
else {
colcnt = 1;
col = cell[j][i];
}
}
if (colcnt != K)
return false;
}
return true;
}
void dfs(int idx, int cnt) {//확인하고 있는 row, 약품처리한 횟수
if (cnt >= result) //약물 횟수가 최소가 될 수 없을 때
return;
if (check()) { //통과대상인지 확인
result = cnt > result ? result : cnt;
return;
}
if (idx == D) return; //모든 row를 확인했을 때
bool temp[20] = { 0, };
dfs(idx + 1, cnt); //약물 처리 없이 넘어가기
copy(cell[idx], cell[idx] + W, temp);
memset(cell[idx], 1, sizeof(bool)*W); //현재 행에 A약물
dfs(idx + 1, cnt + 1);
memset(cell[idx], 0, sizeof(bool)*W); //현재 행에 B약물
dfs(idx + 1, cnt + 1);
copy(temp, temp + W, cell[idx]);
}
int main(int argc, char** argv)
{
int test_case;
int T;
//freopen("input.txt", "r", stdin);
cin >> T;
for (test_case = 1; test_case <= T; ++test_case)
{
result = 987654321;
cin >> D >> W >> K;
for (int j = 0; j < D; j++) {
for (int i = 0; i < W; i++) {
cin >> cell[j][i];
}
}
if(K<2)
cout << "#" << test_case << " 0" << endl;
else {
dfs(0, 0);
cout << "#" << test_case << " " << result << endl;
}
}
return 0;//정상종료시 반드시 0을 리턴해야합니다.
}
2. Review
다 체크해야 된다고 생각하면 금방 구현하는데 그러면 시간 초과 될꺼라고 생각하고 다른 방법 찾다가 오래 걸림ㅎ..
Leave a comment