Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Seol-JY/202504/02 BOJ G1 외판원 순회.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
static int N;
static int[][] map, dp;
static final int MAX_VALUE = 16_000_001;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;

N = Integer.parseInt(br.readLine());
map = new int[N][N];
dp = new int[N][(1<<N) - 1];
Copy link

Copilot AI Apr 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dp array size calculation may lead to an off-by-one error since the visited states range from 0 to (1<<N) - 1. Consider using 'dp = new int[N][1<<N]' to ensure all states are safely covered.

Suggested change
dp = new int[N][(1<<N) - 1];
dp = new int[N][1<<N];

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이게 뭐셔..ㄷㄷㄷ

for(int i = 0; i < N; i++) Arrays.fill(dp[i], -1);

for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = nextInt(st);
}
}

System.out.println(dfs(0, 1));
}

static int dfs(int now, int visited) {
if (visited == (1 << N) - 1) {
if (map[now][0] == 0) return MAX_VALUE;
return map[now][0];
}

if(dp[now][visited] != -1) return dp[now][visited];
dp[now][visited] = MAX_VALUE;

for (int i = 0; i < N; i++) {
if((visited & (1 << i)) == 0 && map[now][i] != 0) {
dp[now][visited] = Math.min(dfs(i, visited | (1 << i)) + map[now][i], dp[now][visited]);
}
}

return dp[now][visited];
}

private static int nextInt(StringTokenizer st) {
return Integer.parseInt(st.nextToken());
}
}
```