public int res = Integer.MAX_VALUE; public int getMinLen (int[][] points) { // write code here boolean[] visited = new boolean[points.length]; backtrack(points,visited,0,0,0,0); return res; } public void backtrack(int[][] points,boolean[] visited,int target,int length,int x,int y){ if(target == points.length){ res = Math.min(res,length); return; } for(int i = 0;i < points.length;i++){ if(!visited[i]){ length += Math.abs(points[i][0] - x) + Math.abs(points[i][1] - y); target++; visited[i] = true; backtrack(points,visited,target,length,points[i][0],points[i][1]); length -= Math.abs(points[i][0] - x) + Math.abs(points[i][1] - y); target--; visited[i] = false; } } } }