Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- ๋ฐฑ์ค11053 #ํ์ด์ฌ #python
- StringBuffer
- ์ฟ ํกERD
- ์คํ์์ด
- ๋ฐฑ์ค9093
- ์
- ๋ฐ์ดํฐํ์
- ๋ฐฑ์ค9012
- ์คํ
- java
- ๋ฐฐ์ด
- StringBuilder
- ๋
- ์ฐ
- ๋ฌธ์์ด
- ์ฟ ํกDB
- ์คํธ๋ฆผ
- stream
- ์๋ฐ
- ์ฐ์ฐ์
- ํ๋ฐฉ์ฟผ๋ฆฌ
- ๋ฐฑ์ค1874
Archives
- Today
- Total
Tech Log ๐ ๏ธ
[๋ฐฑ์ค] 14889๋ฒ ์คํํธ์ ๋งํฌ ์๋ฐ(๋ฐฑํธ๋ํน , ๋นํธ๋ง์คํน) ๋ณธ๋ฌธ
์๊ณ ๋ฆฌ์ฆ
[๋ฐฑ์ค] 14889๋ฒ ์คํํธ์ ๋งํฌ ์๋ฐ(๋ฐฑํธ๋ํน , ๋นํธ๋ง์คํน)
sehaan 2023. 3. 28. 15:08
๋ถ์
ํด๋น ๋ฌธ์ ๋ ๋ฐฑํธ๋ํน , ๋นํธ๋ง์คํน ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ํ ์ ์์ต๋๋ค.
1. ๋นํธ๋ง์คํน
๋นํธ ๋ง์คํน์ด๋?
๋ถ๋ฆฌ์ธ ๋ฐฐ์ด์ ์ญํ ์ ํ๋ ํ๋์ ์ซ์๋ฅผ ๋ง๋ค์ด์ ๋นํธ ์ฐ์ฐ์๋ฅผ ํตํด ํ์ , ์์ ๋ฑ์ ์์ ์ ํ๋ ๊ฒ์ ๋งํฉ๋๋ค
- ํฐ๋์ ํฐ์ ์ ํ๋ธ
๋ ์์ธํ ์ฌํญ์ ์ด์ ์ ๋ธ๋ก๊ทธ์ ์ ๋ฆฌํด ๋์ ๋ด์ฉ์ ์ฐธ๊ณ ํด์ฃผ์ธ์
https://cb036133.tistory.com/91
static void dfs() {
for (int i = 0; i < (1 << num) ; i++) {
int cnt = 0;
team = new boolean[num];
for (int j = 0; j < num; j++) {
if ((i & (1 << j)) != 0) {
team[j] = true;
cnt++;
}
}
if (cnt == num / 2) cal();
}
}
์ ๋ ๋นํธ๋ง์คํน์ ์ด์ฉํด์ ํ์ ์๊ฐ ๋ฐ๋ฐ์ด ๋๋ฉด ๊ฐ ํ์ ๋ฅ๋ ฅ์น ํฉ์ ๋นผ์ฃผ๋ ์์ผ๋ก ์ฝ๋๋ฅผ ๊ตฌ์ฑํ์์ต๋๋ค.
2. ๋ฐฑํธ๋ํน
๋ฐฑํธ๋ํน์ด๋?
ํด๋ฅผ ์ฐพ๋ ๋์ค ํด๊ฐ ์๋์ด์ ๋งํ๋ฉด, ๋๋์๊ฐ์ ๋ค์ ํด๋ฅผ ์ฐพ์๊ฐ๋ ๊ธฐ๋ฒ
dfs๋ฅผ ์ด์ฉํ์ฌ ๋ฐฑํธ๋ ํน์ ๊ตฌํํ ์ ์์ต๋๋ค.
static void dfs(int idx,int depth) {
if (depth == num / 2) {
cal();
return;
} else {
for (int i = idx; i < num; i++) {
if (check[i] == false) {
check[i] = true;
dfs(i+1,depth + 1);
check[i] = false;
}
}
}
}
์ฝ๋ - ๋นํธ๋ง์คํน
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int num;
static int[][] arr;
static boolean[] team;
static int min = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = Integer.parseInt(br.readLine());
arr = new int[num][num];
team = new boolean[num];
for (int i = 0; i < num; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < num; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
dfs();
System.out.println(min);
}
static void dfs() {
for (int i = 0; i < (1 << num) ; i++) {
int cnt = 0;
team = new boolean[num];
for (int j = 0; j < num; j++) {
if ((i & (1 << j)) != 0) {
team[j] = true;
cnt++;
}
}
if (cnt == num / 2) cal();
}
}
static void cal() {
int num1 = 0;
int num2 = 0;
for(int i=0;i<num-1;i++){
for(int j=i+1;j<num;j++){
if(team[i] == true & team[j] == true){
num1 += arr[i][j];
num1 += arr[j][i];
}
if(team[i] == false & team[j] == false){
num2 += arr[i][j];
num2 += arr[j][i];
}
}
}
int result = Math.abs(num1-num2);
if(result == 0){
System.out.println(0);
System.exit(0);
}
if(min > result) min = result;
}
}
์ฝ๋ - ๋ฐฑํธ๋ํน
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import static java.lang.Math.abs;
public class Main {
static int[][] arr;
static boolean[] check;
static int num;
static int result=Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
check = new boolean[num];
arr = new int[num][num];
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
arr[i][j] = sc.nextInt();
}
}
dfs(0,0);
System.out.println(result);
}
static void dfs(int idx,int depth) {
if (depth == num / 2) {
cal();
return;
} else {
for (int i = idx; i < num; i++) {
if (check[i] == false) {
check[i] = true;
dfs(i+1,depth + 1);
check[i] = false;
}
}
}
}
static void cal() {
int num1 = 0;
int num2 = 0;
for (int i = 0; i < num-1; i++) {
for(int j= i+1;j<num;j++) {
if (check[i] == true & check[j] == true) {
num1 += arr[i][j];
num1 += arr[j][i];
}
else if (check[i] == false & check[j] == false) {
num2 += arr[i][j];
num2 += arr[j][i];
}
}
}
int val = abs(num1 - num2);
if(val == 0){
System.out.println(0);
System.exit(0);
}
result = Math.min(result,val);
}
}
'์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํฌ ํฌ์ธํฐ ์๊ณ ๋ฆฌ์ฆ์ด๋ ? (ํ๋ก๊ทธ๋๋จธ์ค - ์ฐ์๋ ๋ถ๋ถ ์์ด์ ํฉ) (3) | 2023.05.10 |
---|---|
์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ด๋? (0) | 2023.04.23 |
[๋ฐฑ์ค] 1991๋ฒ ํธ๋ฆฌ์ํ JAVA (0) | 2023.03.26 |
[๋ฐฑ์ค] 11723 ์งํฉ JAVA (2) | 2023.03.23 |
[๋ฐฑ์ค] ๋ฐฑ์ค ๊ณจ๋ ๋ฌ์ฑ ! (0) | 2023.03.22 |