문제
나의 풀이
public class Pracc {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
ArrayList<int[]> s = new ArrayList<>();
int[] color = new int[N];
int[] sum = new int[3];
while (N > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int[] list = new int[] {a, b, c};
s.add(list);
N--;
}
for (int i = 0; i < 3; i++) {
int min = s.get(0)[i];
color[0] = min;
int total = 0;
for (int j = 1; j < s.size(); j++) {
int last = color[j - 1];
int min2 = 0;
if (last == s.get(j-1)[0]) {
min2 = Math.min(s.get(j)[1], s.get(j)[2]);
} else if (last == s.get(j-1)[1]) {
min2 = Math.min(s.get(j)[0], s.get(j)[2]);
} else {
min2 = Math.min(s.get(j)[0], s.get(j)[1]);
}
color[j] = min2;
}
for (int k = 0; k < s.size(); k++) {
total += color[k];
}
sum[i] = total;
}
int answer = Math.min(Math.min(sum[0], sum[1]), Math.min(sum[1], sum[2]));
System.out.println(answer);
}
}
- 반드시 작은 값들로만 채웠을 때 최종적으로 최소값이 나오지 않기 때문에
정확한 값 산출 x
- 앞 행, 다음 행 들의 합을 모두 고려하여 최소값이 나오도록 함수 생성해야 함
반복문 풀이
public class Pracc {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int[][] Cost = new int[N][3];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
Cost[i][0] = Integer.parseInt(st.nextToken());
Cost[i][1] = Integer.parseInt(st.nextToken());
Cost[i][2] = Integer.parseInt(st.nextToken());
}
for (int i = 1; i < N; i++) {
Cost[i][0] += Math.min(Cost[i - 1][1], Cost[i - 1][2]);
Cost[i][1] += Math.min(Cost[i - 1][0], Cost[i - 1][2]);
Cost[i][2] += Math.min(Cost[i - 1][0], Cost[i - 1][1]);
}
System.out.println(Math.min(Math.min(Cost[N - 1][0], Cost[N - 1][1]), Cost[N - 1][2]));
}
}
재귀 풀이
public class Pracc {
static int[][] Cost;
static int[][] DP;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Cost = new int[N][3];
DP = new int[N][3];
StringTokenizer st;
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
Cost[i][0] = Integer.parseInt(st.nextToken());
Cost[i][1] = Integer.parseInt(st.nextToken());
Cost[i][2] = Integer.parseInt(st.nextToken());
}
DP[0][0] = Cost[0][0];
DP[0][1] = Cost[0][1];
DP[0][2] = Cost[0][2];
System.out.println(Math.min(cost(N- 1, 0), Math.min(cost(N - 1, 1), cost(N - 1, 2))));
}
static int cost(int N, int n) {
if(DP[N][n] == 0) {
if(n == 0) {
DP[N][0] = Math.min(cost(N - 1, 1), cost(N - 1, 2)) + Cost[N][0];
}
else if(n == 1) {
DP[N][1] = Math.min(cost(N - 1, 0), cost(N - 1, 2)) + Cost[N][1];
}
else {
DP[N][2] = Math.min(cost(N - 1, 0), cost(N - 1, 1)) + Cost[N][2];
}
}
return DP[N][n];
}
}
1194번 재귀 풀이