gpt4 book ai didi

java - 尝试用 Java 解魔方

转载 作者:行者123 更新时间:2023-12-01 13:58:59 27 4
gpt4 key购买 nike

到目前为止,我已经开发了一个类,可以使用树形图表示魔方(最好的方法?),每种颜色都映射到键 0 - 53。键始终保持不变,并由魔方的 2D View 表示立方体。我做了一个rotate90方法,它将采用一种颜色并将该颜色面向(中间的立方体从不移动)顺时针旋转90度。所以我的问题是,如何实现一个好的搜索(也许是 A*?)以使这个立方体朝着正确的方向到达其 goalState(我也在代码中将其初始化为全局,以便我可以将其与当前状态)。任何正确方向的反馈或提示都会很棒。谢谢!

这是没有旋转 90 方法和 checkInput 方法的代码,该方法仅检查文件文本是否正确。输入只是一个表示 2D 立方体的文本文件,如下所示。现在的解决方法只是我乱搞,并没有真正取得任何进展。

   GGW
RRG
RRG
OWWGGOYYR
OGOYYYRBR
YYYRBGRWW
BOY
BOB
BOB
OGO
WWB
WWB

package rubik;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.NavigableMap;
import java.util.TreeMap;

public class RubikCube {
final static String FILE_NAME = "rubikTest.txt";
public TreeMap<Integer, Character> goalState;


//swaps two colors given a cube
public static void swapColorValue(int color1, int color2, TreeMap<Integer, Character> rubik){
char tempColor = rubik.get(color1);
rubik.put(color1,rubik.get(color2));
rubik.put(color2, tempColor);
}

//initialize the goal map
public void goalInit(TreeMap<Integer, Character> rubik){
//initialize the goal state map each rubik.get(magicNumber) is the static center cubie
TreeMap<Integer, Character> goalMap = new TreeMap<Integer,Character>();
for(int i =0;i<=8;i++){
goalMap.put(i,rubik.get(4));
}
//the left side
goalMap.put(9, rubik.get(19));
goalMap.put(10, rubik.get(19));
goalMap.put(11, rubik.get(19));
goalMap.put(18, rubik.get(19));
goalMap.put(19, rubik.get(19));
goalMap.put(20, rubik.get(19));
goalMap.put(27, rubik.get(19));
goalMap.put(28, rubik.get(19));
goalMap.put(29, rubik.get(19));
//the middle
goalMap.put(12, rubik.get(22));
goalMap.put(13, rubik.get(22));
goalMap.put(14, rubik.get(22));
goalMap.put(21, rubik.get(22));
goalMap.put(22, rubik.get(22));
goalMap.put(23, rubik.get(22));
goalMap.put(30, rubik.get(22));
goalMap.put(31, rubik.get(22));
goalMap.put(32, rubik.get(22));
//right side
goalMap.put(15, rubik.get(25));
goalMap.put(16, rubik.get(25));
goalMap.put(17, rubik.get(25));
goalMap.put(24, rubik.get(25));
goalMap.put(25, rubik.get(25));
goalMap.put(26, rubik.get(25));
goalMap.put(33, rubik.get(25));
goalMap.put(34, rubik.get(25));
goalMap.put(35, rubik.get(25));
//bottom
for(int i = 36;i<=44;i++){
goalMap.put(i, rubik.get(40));
}
//back
for(int i = 45;i<=53;i++){
goalMap.put(i, rubik.get(49));
}
//give it to the global variable
goalState = (TreeMap<Integer, Character>) goalMap.clone();

}

//Maps a Integer key to a color given a file.
public static NavigableMap<Integer, Character> setup(String file) throws IOException{
TreeMap<Integer, Character> rubik = new TreeMap<Integer, Character>();
BufferedReader br = new BufferedReader(new FileReader(file));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
//add each line (without white spaces) to the stringbuilder
while (line != null) {
sb.append(line.trim());
line = br.readLine();
}
//convert the stringbuilder(which has all the input from the file) to a char array
char [] colors = sb.toString().toCharArray();
//put the key,color into the Treemap.
for(int i =0; i < colors.length;i++){
rubik.put(i, colors[i]);
}
} finally {
br.close();
}
//type Tree map
return rubik;

}

public int solve(TreeMap<Integer, Character> rubik){
int j = 1;
int check = 0;
int redMatches=0;
char [] colors = {'r','g','y','b','o','w'};

for(int i = 0; i < 100;i++ ){
if(j==6) j = 1;
redMatches = 0;
rotate90(colors[j],rubik);
if(rubik.get(check)==goalState.get(check)){
System.out.print("rubik match at: "+i+" with: "+rubik.get(i)+"match at goalState: "+i+" with: "+goalState.get(i));
redMatches++;
}

j++;
}


return redMatches;
}

public static void main (String[] args){
try {
//check if file input is good
//System.out.print(new RubikCube().checkInput(FILE_NAME));
//Map the rubik cube(key,Color)
RubikCube cubeInst = new RubikCube();
//make sure to set up the mapping before calling rotate and goalInit
TreeMap<Integer, Character> cube = (TreeMap<Integer, Character>) (setup(FILE_NAME));
cubeInst.goalInit(cube);
//System.out.print(cubeInst.goalState);
//rotate90('y',cube);
//rotate90('y',cube);
//System.out.print(cube);
System.out.print(cubeInst.solve(cube));
} catch (IOException e) {
e.printStackTrace();
}
}

}

最佳答案

我从未编写过解决魔方问题的代码,但如果您使用像 A* 这样的启发式图遍历算法,我可以想到一些好的启发式方法来判断您距离目标有多远:

  1. 丢失的小柜子数量。

  2. 每个小房间距离目标状态有多远的总和,可能使用曼哈顿距离来判断它有多远。

问题是我认为你不能只为每个立方体状态使用一种颜色。您可能需要将立方体表示为从某个任意角度的数据结构位置。因此,例如“左上角”是目标状态下的左上角 block (也是左侧和背面的角 block ......)然后您就会知道您的“左上角” block 是否出局地点。

关于java - 尝试用 Java 解魔方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19455829/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com