原题链接
题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
如下图所示: 有 99 只盘子,排成 11 个圆圈。 其中 88 只盘子内装着 88 只蚱蜢,有一个是空盘。 我们把这些蚱蜢顺时针编号为 11 ~ 88。
每只蚱蜢都可以跳到相邻的空盘中, 也可以再用点力,越过一个相邻的蚱蜢跳到空盘中。
请你计算一下,如果要使得蚱蜢们的队形改为按照逆时针排列, 并且保持空盘的位置不变(也就是 1-81−8 换位,2-72−7换位,…),至少要经过多少次跳跃?
解答代码:
public class Main {
static int d[] = {1,2,3,4,5,6,7,8,0};
static int ans[] = {8,7,6,5,4,3,2,1,0};
static ArrayList<Integer> aans;
static Queue<cls> q = new LinkedList<>();
static HashMap<ArrayList<Integer>, Integer> map = new HashMap();
static int ansStep = 0;
static class cls {
public ArrayList<Integer> q;
public int step = 0;
public cls() {
q = new ArrayList<>();
}
}
public static void main(String[] args) throws Exception {
ArrayList<Integer> l = arrayToList(d);
aans = arrayToList(ans);
map.put(arrayToList(d), 1);
cls c = new cls();
c.q = l;
q.add(c);
bfs();
System.out.println("ans:" + ansStep);
}
static void bfs() throws InterruptedException {
while(!q.isEmpty()) {
cls c = q.poll();
if(c.q.equals(aans)) {
ansStep = c.step;
break;
}
int t1 = -1;
for(int i = 0;i < 9;i ++ ) {
if(c.q.get(i) == 0) {
t1 = i;
break;
}
}
if(t1 != -1) {
{
ArrayList<Integer> l1 = (ArrayList<Integer>) c.q.clone();
reduceone(l1, t1);
if(!map.containsKey(l1)) {
System.out.println("swap:" + t1 + " " + (t1 - 1));
map.put(l1, 1);
System.out.println(l1.toString() + " " + (c.step + 1));
cls cc = new cls();
cc.step = c.step + 1;
cc.q = l1;
q.add(cc);
}
}
{
ArrayList<Integer> l1 = (ArrayList<Integer>) c.q.clone();
reducetwo(l1, t1);
if(!map.containsKey(l1)) {
System.out.println("swap:" + t1 + " " + (t1 - 2));
map.put(l1, 1);
System.out.println(l1.toString() + " " + (c.step + 1));
cls cc = new cls();
cc.step = c.step + 1;
cc.q = l1;
q.add(cc);
}
}
{
ArrayList<Integer> l1 = (ArrayList<Integer>) c.q.clone();
addone(l1, t1);
if(!map.containsKey(l1)) {
System.out.println("swap:" + t1 + " " + (t1 + 1));
System.out.println("21312312");
map.put(l1, 1);
System.out.println(l1.toString() + " " + (c.step + 1));
cls cc = new cls();
cc.step = c.step + 1;
cc.q = l1;
q.add(cc);
}
}
{
ArrayList<Integer> l1 = (ArrayList<Integer>) c.q.clone();
addtwo(l1, t1);
if(!map.containsKey(l1)) {
System.out.println("swap:" + t1 + " " + (t1 + 2));
map.put(l1, 1);
System.out.println(l1.toString() + " " + (c.step + 1));
cls cc = new cls();
cc.step = c.step + 1;
cc.q = l1;
q.add(cc);
}
}
}
}
}
static void addone(ArrayList<Integer> d, int i) {
int t = (i + 1 + 9) % 9;
int tt = d.get(i);
d.set(i, d.get(t));
d.set(t, tt);
}
static void addtwo(ArrayList<Integer> d, int i) {
int t = (i + 2 + 9) % 9;
int tt = d.get(i);
d.set(i, d.get(t));
d.set(t, tt);
}
static void reduceone(ArrayList<Integer> d, int i) {
int t = (i - 1 + 9) % 9;
int tt = d.get(i);
d.set(i, d.get(t));
d.set(t, tt);
}
static void reducetwo(ArrayList<Integer> d, int i) {
int t = (i - 2 + 9) % 9;
int tt = d.get(i);
d.set(i, d.get(t));
d.set(t, tt);
}
static ArrayList<Integer> arrayToList(int[] array) {
ArrayList<Integer> list = new ArrayList<>();
for(int i = 0;i < array.length;i ++ ) {
list.add(array[i]);
}
return list;
}
最终答案:20