Unidirectional+TSP

Source Code code format="java"

import java.util.Stack;

public class UnidirectionalTSP { public static void main(String[] args) { int[][] map = { {3, 4, 1, 2, 8, 6},   {6, 1, 8, 2, 7, 4},    {5, 9, 3, 9, 9, 5},    {8, 4, 1, 3, 2, 6},    {3, 7, 2, 8, 6, 4}    };    solve(map); }   public static void solve(int[][] map) { int width = map[0].length;// ���� ���� int height = map.length;// ���� ���� int[][] sum = new int[height][width];// �հ� �迭 // ù��° ���� �ʰ� �����Ƿ� �׳� ���� ���ִ´�. for(int i = 0; i < height; i++) { sum[i][0] = map[i][0]; }   // ������ ���� �ּҰ��� �־�� �ϹǷ� �ִ밪���� �ʱ�ȭ�Ѵ�. for(int r = 0; r < height; r++) { for(int c = 1; c < width; c++) { sum[r][c] = Integer.MAX_VALUE; }   }    for(int c = 1; c < width; c++) { for(int r = 0; r < height; r++) { for(int k = -1; k <= 1; k++) { int n = r + k;   int min; if(n < 0) { min = sum[height-1][c-1] + map[r][c]; }else if(n > height - 1) { min = sum[0][c-1] + map[r][c]; }else { min = sum[n][c-1] + map[r][c]; }   if(sum[r][c] > min) { sum[r][c] = min; }   }    }    }    // ������ ������ �ּҰ��� ã�´�. int min = Integer.MAX_VALUE; for(int r = 0; r < height; r++) { if(min > sum[r][width-1]) { min = sum[r][width-1]; }   }    // �ּҰ��� ��ġ�� �� ���� ã�Ƴ���. int row = -1; for(int r = 0; r < height; r++) { if(sum[r][width-1] == min) { row = r;   } }   Stack stack = new Stack; stack.push(row + 1); // �������Ѵ�. for(int c = width - 1; c > 0; c--) { int pre = sum[row][c] - map[row][c]; for(int k = -1; k <= 1; k++) { int n = row + k;   int r;    if(n < 0) { r = height-1; } else if(n > height - 1) { r = 0; } else { r = n;   } if(sum[r][c-1] == pre) { stack.push(r + 1); row = r;   break; }   }    }    // ����Ѵ�. while(!stack.empty) { System.out.print(stack.pop + " "); }   System.out.println("\n" + min); }   }

code Comment 문제를 푼지 오래되어 어떻게 풀었는지 잘 기억이 안나네요;;