public static enum Action {
No, LToM, MToL, MToR, RToM
}
public static int hanoiProblem2(int num, String left, String mid, String right) {
Stack<Integer> lS = new Stack<Integer>();
Stack<Integer> mS = new Stack<Integer>();
Stack<Integer> rS = new Stack<Integer>();
lS.push(Integer.MAX_VALUE);
mS.push(Integer.MAX_VALUE);
rS.push(Integer.MAX_VALUE);
for (int i = num; i > 0; i--) {
lS.push(i);
}
Action[] record = { Action.No };
int step = 0;
while (rS.size() != num + 1) {
step += fStackTotStack(record, Action.MToL, Action.LToM, lS, mS, left, mid);
step += fStackTotStack(record, Action.LToM, Action.MToL, mS, lS, mid, left);
step += fStackTotStack(record, Action.RToM, Action.MToR, mS, rS, mid, right);
step += fStackTotStack(record, Action.MToR, Action.RToM, rS, mS, right, mid);
}
return step;
}
public static int fStackTotStack(Action[] record, Action preNoAct,
Action nowAct, Stack<Integer> fStack, Stack<Integer> tStack,
String from, String to) {
if (record[0] != preNoAct && fStack.peek() < tStack.peek()) {
tStack.push(fStack.pop());
System.out.println("Move " + tStack.peek() + " from " + from + " to " + to);
record[0] = nowAct;
return 1;
}
return 0;
}
public static void main(String[] args) {
int num = 4;
// solution 2
int steps2 = hanoiProblem2(num, "left", "mid", "right");
System.out.println("It will move " + steps2 + " steps.");
System.out.println("===================================");
}
これは、ハノイの塔を解くためのコードです。 3つのスタックを使用して3つのタワーをシミュレートします。 私の質問は、レコード変数を配列として定義するのはなぜですか? アクション[] record = {Action.No}; レコード[0] = nowAct;スタックを使用してハノイの塔を解決する(java)
私は アクションレコード= Action.Noにそれらを変更しようとしました。 record = nowAct; このコードは実行されません。
私は理由を知りません。誰かが理由を説明できると本当に感謝しています。おかげさまで その値がfStackTotStack
メソッド内で変更されているため
私が言及するのを忘れてもう一つの条件は、この質問であります。ディスクは、右またはワンステップで右から左に、左から移動することはできません。中から左に、または中から左に左に置く必要があります。 –
'{Action.No}'は1つの要素を含む配列で、 'Action []'と入力します。 'Action.No'と同じではなく、' Action'と入力します。 –
私はそれがレコード[0]を使用したことを意味します。配列から変数に 'レコード'を変更できますか?ありがとうございました。 –