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; } } } }
6 1

相关推荐

Noob1024:一笔传三代,人走笔还在
点赞 评论 收藏
分享
牛客网
牛客企业服务