완전탐색을 적용해 풀고 있습니다.
switches[10] 배열에 0000000000 ~ 3333333333
을 넣어서 그 숫대로 시계를 돌려 검사하는 방식입니다.
예제 출력은 잘 나오는데 오답 처리가 되네요.
다른 정답 소스를 구해서 testcase를 몇 개 넣어 봤는데 결과가 같아서, 뭐가 문제인지 잘 모르겠습니다.
#include <cstdio>
#pragma warning(disable:4996)
#define INF 987654321
#define CLOCK 16
using namespace std;
int relatedClocks[10][5] = {
{0,1,2,-1,-1},
{ 3, 7, 9, 11, -1 },
{ 4, 10, 14, 15 -1 },
{ 0, 4, 5, 6, 7 },
{ 6, 7, 8, 10, 12 },
{ 0, 2, 14, 15, -1 },
{ 3, 14, 15, -1, -1 },
{ 4, 5, 7, 14, 15 },
{ 1, 2, 3, 4, 5 },
{ 3, 4, 5, 9, 13 }
};
int all12(int clocks[CLOCK]){
int count12 = 0;
for (int i = 0; i < CLOCK; i++){
if (clocks[i] == 12) count12++;
}
if (count12 == CLOCK) return 1;
return 0;
}
void moveClocks(int clocks[CLOCK], int switches[10], int sw_pos, int type){
if (type == 1){
for (int move = 0; move < switches[sw_pos]; move++){
int selectedClock;
for (int i = 0; i < 5; i++){
selectedClock = relatedClocks[sw_pos][i];
if (selectedClock != -1){
clocks[selectedClock] += 3;
if (clocks[selectedClock] > 12) clocks[selectedClock] -= 12;
}
}
}
}
else{
for (int move = 0; move < switches[sw_pos]; move++){
int selectedClock;
for (int i = 0; i < 5; i++){
selectedClock = relatedClocks[sw_pos][i];
if (selectedClock != -1){
clocks[selectedClock] -= 3;
if (clocks[selectedClock] < 1) clocks[selectedClock] += 12;
}
}
}
}
}
int onSwitches(int switches[10]){
int os = 0;
for (int i = 0; i < 10; i++){
os += switches[i];
}
return os;
}
int min(int a, int b){
return a <= b ? a : b;
}
//디버깅용
void showClocks(int clocks[CLOCK], int type){
if (type == 1) printf("moving\t");
else printf("reversing\t");
for (int i = 0; i < CLOCK; i++){
printf("%d\t", clocks[i]);
}
printf("\n");
}
int syncClocks(int clocks[CLOCK], int switches[10], int sw_pos){
// 스위치 경우의 수 완전탐색
// 검사
if (sw_pos == 10){
//printf("\n");
if (all12(clocks))
return onSwitches(switches);
else
return INF;
}
int ret = INF;
for (int times = 0; times < 4; times++){
switches[sw_pos] = times;
//printf("%d\t", times);
moveClocks(clocks, switches, sw_pos, 1);
//showClocks(clocks, 1);
ret = min(ret, syncClocks(clocks, switches, sw_pos + 1));
moveClocks(clocks, switches, sw_pos, -1); // 원상복귀
//showClocks(clocks, -1);
}
return ret;
}
int main(void){
int tc;
int clocks[CLOCK];
scanf("%d", &tc);
for (int i = 0; i < tc; i++){
int switches[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int ret;
for (int j = 0; j < CLOCK; j++){
scanf("%d", &clocks[j]);
}
ret = syncClocks(clocks, switches, 0);
if (ret == INF) ret = -1;
printf("%d\n", ret);
}
return 0;
}
코드 상에 어떤 문제가 있는지,
아니면 오답처리된 test case에 대해 알려주신다면 감사하겠습니다.
kwonmha
[[problem:CLOCKSYNC]] 문제를 풀고 있습니다.
오답이 나는데, 왜 오답이 나는지 잘 모르겠네요 ㅜ
완전탐색을 적용해 풀고 있습니다.
switches[10] 배열에 0000000000 ~ 3333333333
을 넣어서 그 숫대로 시계를 돌려 검사하는 방식입니다.
예제 출력은 잘 나오는데 오답 처리가 되네요.
다른 정답 소스를 구해서 testcase를 몇 개 넣어 봤는데 결과가 같아서, 뭐가 문제인지 잘 모르겠습니다.
코드 상에 어떤 문제가 있는지,
아니면 오답처리된 test case에 대해 알려주신다면 감사하겠습니다.
10년 전