์ฒ์์ ์ธ์์ด์ ์นด๋ ์์ด์ ์ ์ฅํ๋ ๊ฒ์ ์๋ฃ๊ตฌ์กฐ๋ฅผ List๋ฅผ ์ฌ์ฉํ๋๋ ๊ฒฐ๊ณผ๊ฐ์ด ์ ๋๋ก ๋์ค์ง ์์๋ค. ๊ทธ ์ด์ ๋ add๋ฅผ ํด์ฃผ๋๋ฐ ์ค๊ฐ์ ๊ฐ๋ค์ด ์ด๊ธฐํ ๋์ง ์๊ณ ๊ณ์ ๊ธธ์ด์ง๊ธฐ ๋๋ฌธ์ด์๋ค.
๊ทธ๋์ ๊ฐ์ ๋ฎ์ด์์์ ์ด๊ธฐํ๋ฅผ ์ํฌ ์ ์๋ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก ๋ฐ๊ฟจ๋๋ ๊ฐ์ด ์ ๋๋ก ๋์๋ค.
์์ด ์กฐํฉ์์ ๊ฐ์ ์ ์ฅํ ๋๋ ๋ฆฌ์คํธ ๋ง๊ณ , ๋ฐฐ์ด์ ์ฌ์ฉํ์!
์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Solution {
static List<Integer> list_g;
static List<Integer> list_i;
static int[] arr_i;
static boolean[] visited;
static int win, lose;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int tc = 1; tc <= T; tc++) {
list_g = new ArrayList<>(); //๊ท์์ด ์นด๋
list_i = new ArrayList<>(); //์ธ์์ด ์นด๋
arr_i = new int[9]; //์ธ์์ด ์นด๋ ์์ด
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for(int i = 0; i < 9; i++) { //๊ท์์ด ์นด๋
int num = Integer.parseInt(st.nextToken());
list_g.add(num);
}
for(int i = 1; i <= 18; i++) {
if(!list_g.contains(i)) {
list_i.add(i);
}
}
visited = new boolean[9];
win = 0;
lose = 0;
permutation(0);
System.out.println("#" + tc + " " + win + " " + lose);
}
}
public static void permutation(int count) {
if(count == 9) {
//๊ฒ์ ์งํ
int score_g = 0;
int score_i = 0;
for(int i = 0; i < 9; i++) {
if(list_g.get(i) > arr_i[i]) {
score_g += list_g.get(i) + arr_i[i];
}else if(list_g.get(i) < arr_i[i]) {
score_i += list_g.get(i) + arr_i[i];
}
}
if(score_g > score_i) {
win++;
}else if(score_g < score_i) {
lose++;
}
return;
}
for(int i = 0; i < 9; i++) {
if(visited[i]) continue;
visited[i] = true;
int num = list_i.get(i);
arr_i[count] = num;
permutation(count + 1);
visited[i] = false;
}
}
}
'Algorithm > SWEA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SWEA - ์๋ก์ด ๋ถ๋ฉด์ฆ ์น๋ฃ๋ฒ (0) | 2023.02.05 |
---|---|
SWEA - 1249๋ฒ ๋ณด๊ธ๋ก (0) | 2022.09.30 |
SWEA 5215๋ฒ - ํ๋ฒ๊ฑฐ ๋ค์ด์ดํธ (0) | 2022.08.11 |
SWEA 9229๋ฒ - ํ๋น์ด์ Spot Mart (0) | 2022.08.08 |
SWEA 1228๋ฒ - ์ํธ๋ฌธ1 (0) | 2022.08.08 |