8.22
作者:互联网
exercise
1.组合总数
package org.example.api.test.exercise;
import java.util.*;
public class zuhezongshu {
//例: canditates[2,3,5,7] target=7
//output:[[7],[2,3,5]]
public static void main(String[] args) {
int [] p={2,3,5,7};
System.out.println(combinationSum(p,7));
}
//优化前---------------------------------------
public static List<List<Integer>> combinationSum(int[] p, int t) {
List<List<Integer>> ans=new ArrayList<>();
dfs(p,t,ans,new ArrayList<>());
return ans;
}
//list 2 2 2 2 ->2 2 2 -> 2 2 3 -> 2 2 5 -> 2 2 7->23 25 27->2 3 5 7
public static void dfs(int []p,int t,List<List<Integer>> ans,List<Integer> list){
if(sum(list)>=t){
if(sum(list)==t){
Collections.sort(list);
if(!ans.contains(list)) {
ans.add(new ArrayList<>(list));//截至条件 深拷贝
}
}
return;
}
for (int i = 0; i <p.length ; i++) {
list.add(p[i]);
dfs(p,t,ans,list);
list.remove(list.size()-1);
}
}
public static int sum(List<Integer> list){
int sum=0;
for(Integer a:list){
sum+=a;
}
return sum;
}
//优化后
public static List<List<Integer>> combinationSum1(int[] candidates, int target) {
int len = candidates.length;
List<List<Integer>> res = new ArrayList<>();
if (len == 0) {
return res;
}
Deque<Integer> path = new ArrayDeque<>();
dfs1(candidates, 0, len, target, path, res);
return res;
}
/**
* @param candidates 候选数组
* @param begin 搜索起点
* @param len 冗余变量,是 candidates 里的属性,可以不传
* @param target 每减去一个元素,目标值变小
* @param path 从根结点到叶子结点的路径,是一个栈
* @param res 结果集列表
*/
private static void dfs1(int[] candidates, int begin, int len, int target, Deque<Integer> path, List<List<Integer>> res) {
// target 为负数和 0 的时候不再产生新的孩子结点
if (target < 0) {
return;
}
if (target == 0) {
res.add(new ArrayList<>(path));
return;
}
// 重点理解这里从 begin 开始搜索的语意
for (int i = begin; i < len; i++) {
path.addLast(candidates[i]);
// 注意:由于每一个元素可以重复使用,下一轮搜索的起点依然是 i,这里非常容易弄错
dfs1(candidates, i, len, target - candidates[i], path, res);
// 状态重置
path.removeLast();
}
}
}
2.礼物迷宫
@Data
public class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
package org.example.api.test.exercise;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class findGift {
static List<Point> temp =new LinkedList<>();
static List<Point> result =new LinkedList<>();
static int minCount =Integer.MAX_VALUE;
//static Point gift=null;
public static void main(String[] args) {
int[][] test={ {0,1,1,1},{0,0,0,1},{1,0,8,1},{1,0,1,1}};
List<Point> result=new ArrayList<>();
result=winMazeGift(test);
for(Point p:result){
System.out.println(p);
}
}
//输入{ {0,1,1,1},
// {0,0,0,1},
// {1,0,8,1},
// {1,0,1,1}}
//输出{{3,1},{2,1},{2,2}} 这条路最近
public static ArrayList<Point> winMazeGift(int[][] maze) {
// write code here
Point gift=new Point(0,0);
boolean flag=false;
int n = maze.length;
int m = maze[0].length;
ArrayList<Point> ans = new ArrayList<>();
//找到礼物点
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (maze[i][j] == 8) {
gift.x=i;
gift.y=j;
flag=true;
temp.add(new Point(i,j));
break;
}
}
if(flag) break;
}
//从礼物点回溯
dfs(gift,0,maze);
//正序输出
ArrayList<Point> finalResult=new ArrayList<>();
while (!result.isEmpty()){
finalResult.add(result.get(result.size()-1));
result.remove(result.size()-1);
}
return finalResult;
}
private static void dfs(Point gift,int count,int [][] maze){
//若走到边界,说明发现了路径,比较是否最短
if(gift.x==0||gift.x==3||gift.y==0||gift.y==3){
if(count< minCount){
result =new LinkedList<>(temp);
minCount =count;
}
return;
}
//判断下个点能不能走,上下左右四个方向,
if(maze[gift.x-1][gift.y]==0){
Point point1=new Point(gift.x-1,gift.y);
//下个点加入路径
temp.add(point1);
//将当前点设置为-1,表示不可走(不加的话来回走,超时)
int kk=maze[gift.x][gift.y];
maze[gift.x][gift.y]=-1;
//向下递归
dfs(point1,count+1,maze);
maze[gift.x][gift.y]=kk;
temp.remove(temp.size()-1);
}
if(maze[gift.x+1][gift.y]==0){
Point point2=new Point(gift.x+1,gift.y);
temp.add(point2);
int kk=maze[gift.x][gift.y];
maze[gift.x][gift.y]=-1;
dfs(point2,count+1,maze);
maze[gift.x][gift.y]=kk;
temp.remove(temp.size()-1);
}
if(maze[gift.x][gift.y-1]==0){
Point point3=new Point(gift.x,gift.y-1);
temp.add(point3);
int kk=maze[gift.x][gift.y];
maze[gift.x][gift.y]=-1;
dfs(point3,count+1,maze);
maze[gift.x][gift.y]=kk;
temp.remove(temp.size()-1);
}
if(maze[gift.x][gift.y+1]==0){
Point point3=new Point(gift.x,gift.y+1);
temp.add(point3);
int kk=maze[gift.x][gift.y];
maze[gift.x][gift.y]=-1;
dfs(point3,count+1,maze);
maze[gift.x][gift.y]=kk;
temp.remove(temp.size()-1);
}
}
}
3.罗马迷宫
package org.example.api.test.exercise;
public class romanMaze {
static int x,y;
static int [][] visitor;
public static void main(String[] args) {
//.是空地,O出口
char [][] maze={{'.',',','.','.','.'},
{'.','R','R','D','.'},
{'.','U','.','D','R'},
{'.','U','L','L','.'},
{'.','.','.','.','O'}};
System.out.println(findPath(maze));
}
public static int findPath(char[][] maze){
int n=maze.length;
int m=maze[0].length;
visitor=new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m ; j++) {
if(maze[i][j]=='O'){
visitor[i][j]=1;
x=i;
y=j;
}
}
}
dfs(x-1,y,'D',maze);
dfs(x+1,y,'U',maze);
dfs(x,y-1,'R',maze);
dfs(x,y+1,'L',maze);
int ans=0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m ; j++) {
if(visitor[i][j]== 1 ){
ans++;
}
}
}
return n*m-ans;
}
public static void dfs(int i,int j,char c,char[][] maze){
int n=maze.length;
int m=maze[0].length;
if(i==x &&j==y) return;
if(i<0||i>= n || j<0 ||j>=m || visitor[i][j]==1){
return;
}
if(maze[i][j]=='.'||maze[i][j]==c){
dfs(i-1,j,'D',maze);
dfs(i+1,j,'U',maze);
dfs(i,j-1,'R',maze);
dfs(i,j+1,'L',maze);
}
}
}
标签:gift,int,dfs,static,8.22,new,maze 来源: https://www.cnblogs.com/ttxxdd/p/16613269.html