回溯的特点
可以用作与不可预测的操作步骤次数但是步骤模式是一样的就是可以使用回溯方法
一个字符匹配组合算法代码 思路:使用双端队列及回溯实现
public static List<List<String>> stringGroupWay(String [] stringArr){
List<List<String>> resultList=new ArrayList<>();
Deque<Integer> layerIndexs=new ConcurrentLinkedDeque<>();
stringGroupWay(stringArr,resultList,layerIndexs);
return resultList;
}
private static void stringGroupWay(String [] stringArr, List<List<String>> resultList, Deque<Integer>layerIndexs){
for(int index=0;index<stringArr.length;index++){
if(layerIndexs.contains(index))continue;
layerIndexs.addLast(index);
if(layerIndexs.size()>=stringArr.length){
List<String> groupList=new ArrayList<>();
Integer [] layerArr=new Integer[layerIndexs.size()];
layerArr= Arrays.copyOfRange(layerIndexs.toArray(),0,layerIndexs.size(),Integer[].class);
for(int currIndex=0;currIndex<layerArr.length;currIndex++){
groupList.add(stringArr[layerArr[currIndex]]);
}
resultList.add(groupList);
}else{
stringGroupWay(stringArr,resultList,layerIndexs);
}
layerIndexs.removeLast();
}
}
迷宫回溯算法 配合上面的字符串组合
public class MazeBackDemo {
public static void main(String[] args) {
String[] arr={"上","下","左","右"};
List<List<String>> lists = MazeBack.stringGroupWay(arr);
for(List<String> arrL:lists){
int [][] maze=new int[7][8];
maze[2][1]=3;
maze[2][2]=3;
maze[2][3]=3;
maze[2][4]=3;
maze[2][5]=3;
maze[2][6]=3;
maze[2][7]=3;
boolean bol=MazeBack.mazeBack(maze,1,1,arrL);
System.out.println("=========="+arrL.toString()+"===迷宫开始======="+(bol?"成功":"失败"));
for(int [] arr2:maze){
for(int arr3:arr2){
System.out.print(arr3+"\t");
}
System.out.println();
}
System.out.println("=========="+arrL.toString()+"===迷宫结束=======");
System.out.println();
System.out.println();
}
}
}
class MazeBack{
private static int countNumber=0;
public static List<List<String>> stringGroupWay(String [] stringArr){
List<List<String>> resultList=new ArrayList<>();
Deque<Integer> layerIndexs=new ConcurrentLinkedDeque<>();
stringGroupWay(stringArr,resultList,layerIndexs);
return resultList;
}
private static void stringGroupWay(String [] stringArr, List<List<String>> resultList, Deque<Integer>layerIndexs){
for(int index=0;index<stringArr.length;index++){
if(layerIndexs.contains(index))continue;
layerIndexs.addLast(index);
if(layerIndexs.size()>=stringArr.length){
List<String> groupList=new ArrayList<>();
Integer [] layerArr=new Integer[layerIndexs.size()];
layerArr= Arrays.copyOfRange(layerIndexs.toArray(),0,layerIndexs.size(),Integer[].class);
for(int currIndex=0;currIndex<layerArr.length;currIndex++){
groupList.add(stringArr[layerArr[currIndex]]);
}
resultList.add(groupList);
}else{
stringGroupWay(stringArr,resultList,layerIndexs);
}
layerIndexs.removeLast();
}
}
public static boolean mazeBack(int [][] maze,int line,int column,List<String> directions ){
countNumber++;
if(maze[5][7]==2){
System.out.println("次数:"+countNumber);
countNumber=0;
return true;
}
if(line<0||line>maze.length-1||column<0||column>maze[0].length-1) return false;
if(maze[line][column]==0){
maze[line][column]=2;
if(mazeBack(maze,lineCalculate(line,directions.get(0)),columnCalculate(column,directions.get(0)),directions)){
return true;
}
else if(mazeBack(maze,lineCalculate(line,directions.get(1)),columnCalculate(column,directions.get(1)),directions)){
return true;
}
else if(mazeBack(maze,lineCalculate(line,directions.get(2)),columnCalculate(column,directions.get(2)),directions)){
return true;
}
else if(mazeBack(maze,lineCalculate(line,directions.get(3)),columnCalculate(column,directions.get(3)),directions)){
return true;
}else{
maze[line][column]=3;
return false;
}
}else{
return false;
}
}
private static int lineCalculate(int line,String direction){
if(direction.equals("上")){
line-=1;
}else if(direction.equals("下")){
line+=1;
}
return line;
}
private static int columnCalculate(int column,String direction){
if(direction.equals("左")){
column-=1;
}else if(direction.equals("右")){
column+=1;
}
return column;
}
}
结果:提示显示一部分