This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog Algorithm - Classic interesting topics - frog crossing the river - Joshua 317's blog
1, Question
Frog crossing the river is a very interesting puzzle game. Its main idea is as follows:
There are several stones between a river. There are two teams of frogs crossing the river. Each team has three frogs, as shown in the figure. These frogs can only move forward, not backward, and only one frog can move forward at a time. In the process of moving, frogs can move to the front empty space. They can't skip two positions at a time, but they can skip each other's frog into the front empty space. How can the two teams of frogs move to the opposite bank with the least number of steps?
2, Analysis
Let's analyze the problem of frogs crossing the river. The following scheme can be adopted to move the frog, and the operation steps are as follows:
(1) The frog on the left jumps over a frog on the right to the right and falls into an empty position. Perform step (5).
(2) The frog on the right jumps over the frog on the left to the left and falls into the empty position. Perform step (5).
(3) The frog on the left moves one space to the right and falls into the empty position. Perform step (5).
(4) The frog on the right moves one space to the left and falls into an empty position. Perform step (5).
(5) Judge whether the two teams of frogs have been moved to the opposite bank. If not, continue from step (1), otherwise end the procedure.
3, Programming
package com.joshua317; import jdk.nashorn.internal.ir.LiteralNode; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { FrogCrossRiver frogCrossRiver = new FrogCrossRiver(); List frogQueue = frogCrossRiver.initFrogQueue(); String frogJumpInfo = (frogCrossRiver.frogJump(frogQueue, 3)); System.out.println("The frog jumps in the following order:\r\n " + frogJumpInfo); } } class Frog { static enum frogDirection {towards the left, towards the right}; public String frogName;//Frog name public int position;//Frog position public frogDirection direction;//Frog jumping direction public boolean canJump;//Can I jump public boolean isEmpty = false;//Is it a space //Constructor public Frog (int position, String frogName, frogDirection direction, boolean canJump) { this.position = position; this.frogName = frogName; this.direction = direction; this.canJump = canJump; } public Frog (int position) { this.frogName = "empty"; this.position = position; this.canJump = false; this.isEmpty = true; } public Frog (Frog frog) { this.position = frog.position; this.frogName = frog.frogName; this.direction = frog.direction; this.canJump = frog.canJump; this.isEmpty = frog.isEmpty; } } class FrogCrossRiver { //Initialize frog queue public List<Frog> initFrogQueue() { List<Frog> frogQueue = new ArrayList<Frog>(); frogQueue.add(new Frog(0, "Left 1", Frog.frogDirection.towards the right, false)); frogQueue.add(new Frog(1, "Left 2", Frog.frogDirection.towards the right, true)); frogQueue.add(new Frog(2, "Left 3", Frog.frogDirection.towards the right, true)); frogQueue.add(new Frog(3)); frogQueue.add(new Frog(4, "Right 1", Frog.frogDirection.towards the left, true)); frogQueue.add(new Frog(5, "Right 2", Frog.frogDirection.towards the left, true)); frogQueue.add(new Frog(6, "Right 3", Frog.frogDirection.towards the left, false)); return frogQueue; } //When a frog jumps, a new queue is formed private List<Frog> editFrogQueue(List<Frog> frogQueue, String frogName, int oldEmptyPostionId, int newEmptyPostionId) { List<Frog> newFrogQueue = new ArrayList<Frog>(); for (int i=0; i<frogQueue.size(); i++) { Frog frog = (Frog)frogQueue.get(i); Frog newFrog = new Frog(frog); if (newFrog.isEmpty) { newFrog.position = newEmptyPostionId; } if (newFrog.frogName == frogName) { newFrog.position = oldEmptyPostionId; } newFrog.canJump = false; if ((newEmptyPostionId - newFrog.position) > 0 && (newEmptyPostionId - newFrog.position) < 3 && newFrog.direction == Frog.frogDirection.towards the right) { newFrog.canJump = true; } if ((newFrog.position - newEmptyPostionId) > 0 && (newFrog.position - newEmptyPostionId) < 3 && newFrog.direction == Frog.frogDirection.towards the left) { newFrog.canJump = true; } newFrogQueue.add(newFrog); } return newFrogQueue; } //Has the position exchange been completed, that is, the positions of the first three frogs are greater than 3 private boolean isComplete(List<Frog> frogQueue) { return (frogQueue.get(0).position > 3 && frogQueue.get(1).position > 3 && frogQueue.get(2).position > 3); } //Are there any frogs that can jump? Only those that can jump do not reach the final state, //But they can't beat, and they don't have to be replaced. Here's just control recursion private boolean canFrogJump(List<Frog> frogQueue) { for (int i=0; i<frogQueue.size(); i++) { Frog frog = (Frog)frogQueue.get(i); if (frog.canJump) { return true; } } return false; } //To get frog beat public String frogJump(List<Frog> frogQueue, int emptyPositionId) { String frogJumpInfo = ""; for (int i=0; i<frogQueue.size(); i++) { Frog frog = (Frog)frogQueue.get(i); //Is an empty position if (frog.isEmpty) { continue; } //Can't jump if (!frog.canJump) { continue; } frogJumpInfo = "frog" + frog.frogName + " " + frog.direction + "Skip to page" + (emptyPositionId + 1) + "Location" + "\r\n"; int newPositionId = frog.position; List<Frog> newFrogQueue = this.editFrogQueue(frogQueue, frog.frogName, emptyPositionId,newPositionId); //Recursion as long as you can continue to jump if (this.canFrogJump(newFrogQueue)) { frogJumpInfo += this.frogJump(newFrogQueue, newPositionId); } else { if (this.isComplete(newFrogQueue)) { frogJumpInfo += "success"; break; } } if (frogJumpInfo.contains("success")) { break; } } return frogJumpInfo; } }
4, Practice
You can think about it. What if there are four frogs, five frogs and six frogs?
A link to a small game is attached: http://www.4399.com/flash/204168_4.htm
This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog Algorithm - Classic interesting topics - frog crossing the river - Joshua 317's blog