青蛙跳躍的最後一個變化顯示在the video的末尾。蠻力青蛙跳躍遊戲
總之,你有Ñ的在一條線上百合片數目和每 一一青蛙。
在最後一個變種(我想暴力的那個)中,第二個 第一個和第二個睡蓮葉沒有青蛙。你的目標是讓 讓所有的青蛙到同一個睡蓮池。每隻青蛙可以根據其百合花上的青蛙數量向右或向左跳 ,但不能跳上 空的百合花墊。
(墊1個蛙移動1點,墊用正青蛙移動僅Ñ點)對於n = 12的溶液中的實施例:(有低於12沒有解決方案)
[1,2 0,1,1,1,1,1,1,1,1,0,1] - 開始形成青蛙。 (計數 青蛙從0.到11.)
[1,0,1,0,2,1,1,1,1,1,0,1] - 青蛙3.跳躍 對
[1,0, 1,0,2,1,2,0,1,1,0,1] - 青蛙7.向左跳
[1,0,1,0,4,1,0,0,1,1,0 ,1] - 青蛙6.向左跳
[5,0,1,0,0,1,0,0,1,1,0,1] - 青蛙4.向左跳
[0,0,1 ,0,0,6,0,0,1,1,0,1] - 青蛙0.向右跳
[0,0,1,0,0,0,0,0,1,1,0, 7] - 青蛙5.向右跳
[0,0,1,0,0,0,0,0,0,2,0,7] - 青蛙8.向右跳
[0,0,1, 0,0,0,0,0,0,0,0,9] - 青蛙9.右跳
[0,0,10,0,0,0,0,0,0,0,0, 0] - 青蛙跳了11左手解決
我想找到解決方案對於n青蛙,如果解決方案存在。我知道手12,14,15,16,17,18,19,20至少有一個解決方案,1-11和13沒有解決方案。
我試着寫一段代碼,可以通過所有組合運行,找到n百合片的解決方案。
編輯:現在的代碼,由於OleV.V.,也增加了日誌。
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
// # Brute Force # Brute Force # Brute Force # Brute Force # Brute Force # //
public class Frogger {
/**
* PUBLIC STATIC GLOBAL VARIABLES - Modify these as you wish.
*
* Time Data: Levels < 20 ~ around couple seconds
* Level = 20 ~ around a minute
* Level = 21 ~ around a quarter of an hour
* Level = 22 ~ around a sixth of a minute
* Level = 23 ~ around half an hour
* Level = 24 ~ around a minute
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
public static int Level = 12;
public static boolean LogSolution = true;
public static boolean LogAll = false;
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// used for logging
private static Deque<Jump> solution = new ArrayDeque<>(Level);
private static double time;
public static void main(String[] args) {
// log the time
time = System.currentTimeMillis();
// build the world & start jumping
run(Level);
}
public static void run(int n) {
// create the world
int[] world = new int[n];
for (int i = 0; i < n; i++) {
world[i] = 1;
}
world[1] = 0;
world[n-2] = 0;
// start jumping
if (Level > 11 && Level != 13) jump(world);
else System.out.println("Unsolvable");
}
//////////////////////////////////////////////////////
public static void jump(int[] world) {
for (int i = 0; i < world.length; i++) {
if (world[i] != 0) { // pad has a frog
// check if it is solved at current pad
if (world[i] == Level - 2) {
System.out.println("SOLUTION: " + Arrays.toString(world));
System.out.println(solution);
System.out.println("\n" + (System.currentTimeMillis() - time)/1000 + " seconds");
System.exit(0);
}
// roll-back var
int temp = 0;
// attempts to make a RIGHT jump
if (world[i] + i < world.length) { // right jump is in bound
if (world[i + world[i]] != 0) { // can't jump on empty pad
temp = world[i];
// jump RIGHT
world[i + world[i]] += world[i];
world[i] = 0;
solution.push(new Jump(temp, i, i + temp)); // log the solution step 1/2
if (LogSolution) if (LogAll) System.out.println("J: " + Arrays.toString(world)); // log the jump
// continue jumping
jump(world);
// roll-back right jump
world[i] = temp;
world[i + world[i]] -= world[i];
if (LogAll) System.out.println("R: " + Arrays.toString(world)); // log the rollback
if (LogSolution) solution.pop(); // log the solution step 2/2
}
}
// attempts to make a LEFT jump
if (i - world[i] >= 0) { // left jump is in bound
if (world[i - world[i]] != 0) { // can't jump on empty pad
temp = world[i];
// jump LEFT
world[i - world[i]] += world[i];
world[i] = 0;
if (LogSolution) solution.push(new Jump(temp, i, i - temp)); // log the solution step 1/2
if (LogAll) System.out.println("J:" + Arrays.toString(world)); // log the jump
// continue jumping
jump(world);
// roll-back left jump
world[i] = temp;
world[i - world[i]] -= world[i];
if (LogAll) System.out.println("R: " + Arrays.toString(world)); // log the rollback
if (LogSolution) solution.pop(); // log the solution step 2/2
}
}
}
}
}
}
邊注:此問題已得到解決的數學對所有可解Ñ(所有n> 11,大於13等,有一個解決方案,由可到達的一般化方法)。這段代碼只是我試圖在java中做一些遞歸。
沒有仔細研究過您的代碼,我相信您可以通過每次不復制數組來節省一些時間,而只需在回溯時撤消跳轉。 –
更好的方法是從數學角度思考它,而不是蠻力。 –
在我看來,你的解決方案比需要更復雜。你的'jump'方法是70行;我可以在40以內編寫一個。它在一分鐘內解決了20級。我不介意給你我的代碼,但編寫你自己的代碼不是更有趣嗎? –