There are K pegs. Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg. There are N discs which have radius 1 to N; Given the initial configuration of the pegs and the final configuration of the pegs, output the moves required to transform from the initial to final configuration. You are required to do the transformations in minimal number of moves.
- A move consists of picking the topmost disc of any one of the pegs and placing it on top of anyother peg.
- At anypoint of time, the decreasing radius property of all the pegs must be maintained.
Constraints:
1<= N<=8
3<= K<=5
Input Format:
N K
2nd line contains N integers.
Each integer in the second line is in the range 1 to K where the i-th integer denotes the peg to which disc of radius i is present in the initial configuration.
3rd line denotes the final configuration in a format similar to the initial configuration.
Output Format:
The first line contains M - The minimal number of moves required to complete the transformation.
The following M lines describe a move, by a peg number to pick from and a peg number to place on.
If there are more than one solutions, it's sufficient to output any one of them. You can assume, there is always a solution with less than 7 moves and the initial confirguration will not be same as the final one.
Sample Input #00:
2 3 1 1 2 2
Sample Output #00:
3 1 3 1 2 3 2
Sample Input #01:
6 4 4 2 4 3 1 1 1 1 1 1 1 1
Sample Output #01:
5 3 1 4 3 4 1 2 1 3 1
NOTE: You need to write the full code taking all inputs are from stdin and outputs to stdout
If you are using "Java", the classname is "Solution"
Download sample testcases as zip ['Compile & Test' will run your code against these testcases]
code:
/*
Please write complete compilable code.
Read input from standard input (STDIN) and print output to standard output(STDOUT).
For more details, please check http://www.interviewstreet.com/recruit/challenges/faq/view#stdio
*/
#include <iostream>
using namespace std;
int start[9];
int last[9];
int pegs[6];
int steps[9][2];
int minSteps = 7;
int optimSteps[9][2];
int N = -1;
int K = -1;
int MAX = 100;
int oldi;
int oldj;
int old;
void print()
{
cout << minSteps << endl;
for(int i = 1; i <= minSteps; ++i)
{
cout << optimSteps[i][0] << " " << optimSteps[i][1] << endl;
}
}
void initPeg()
{
for(int i = 1; i <= K; ++i)
{
pegs[i] = MAX;
for(int j = 1; j <= N; ++j)
{
if(start[j] == i )
{
pegs[i] = j;
break;
}
}
}
}
void fun(int depth)//steps: 0-7
{
if(depth++ >= minSteps)
return;
for(int i = 1; i <= K; ++i)
{
for(int j = 1; j <= K; ++j)
{
if(pegs[i] < pegs[j])// possible step, go ahead!
{
int disc = pegs[i];
start[pegs[i]] = j;
initPeg();
steps[depth][0] = i;
steps[depth][1] = j;
//after the switch
bool flag = true;
for(int idx = 1; idx <= N; ++idx)
if(start[idx] != last[idx])
{
flag = false;
break;
}
if(flag == true)//find one
{
minSteps = depth;
for(int idx = 1; idx <= depth; ++idx)
{
optimSteps[idx][0] = steps[idx][0];
optimSteps[idx][1] = steps[idx][1];
}
return ;
}
else//not a result
{
fun(depth);
}
start[disc] = i;
initPeg();
}
}
}
}
int main()
{
cin >> N;
cin >> K;
for(int i = 1; i <= N; ++i)
cin >> start[i];
for(int i = 1; i <= N; ++i)
cin >> last[i];
initPeg();
fun(0);
print();
return 0;
}