A Puzzle C++ Consider the following puzzle, you have n black marbles and n white
ID: 653334 • Letter: A
Question
A Puzzle C++
Consider the following puzzle, you have n black marbles and n white marbles. The playing board consists of a line of 2n + 1 locations to place the marbles in. You start with all the black marbles at one end (say, the left), all the white marbles at the other end, and an empty space in beween.
The goal is to reverse the positions of the black and white marbles. The black marbles can move only to the right and the white marbles can move only to the left. At each move, a marble can either Move one space ahead, if that space is clear, or Jump ahead over one marble of the opposite color that is one space ahead, if the space just beyond the marble is clear.
The Solving Algorithm
push the starting position onto the stack while the stack is not empty
{ pop the stack to get the current position if the current position is the goal print the solution and exit the program
for each possible move, push the resulting configuration onto the stack
Explanation / Answer
import java.util.*;
public class Puzzle{
public static void main(String[] args){
String[] arr=new String[200];
int n,,i,j,len,check=0;
System.out.println(
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.