InterviewStreet —— Hanoi Moves

昨天发现leetcode down了,所以找了另一个online judge网站interviewstreet

第一次玩,感觉挺新鲜的,风格和leetcode有很多不同,interviewstreet是限时答题的,很有挑战。

而且,除了sample case外,会隐藏掉其他case的内容,只显示结果。


我做的是sample test,给了三道题目,1. Hanoi Moves  2.二叉树的最长路径 3.N以内的素数个数

一共只给了45分钟时间,由于自己的代码能力太差,一上来第一题没完成就到时间了。呵呵,要

好好修炼一番。总体感觉2,3两题相对较水。就说说第一题吧。


原题:

Question 1 / 3 (Hanoi Moves)

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"


思路:读题目就差不多花了我10分钟,英文不给力啊。

其实这道题就是简单的dfs,然后加一些简单的剪枝。

对C++的STL不熟悉,严重影响写代码的速度,哎~~~


直接上代码:

#include 
#include 
#include 

using namespace std;

int minMoveNum;
int N, K;
vector > minTraces;
vector > midState;
vector > finalState;

int init() {
    midState.clear();
    finalState.clear();
    for (int i=0; i temp;
        midState.push_back(temp);
        finalState.push_back(temp);
    }
    int peg;
    for (int i = 1; i<=N; i++) {
        cin >> peg;
        midState[peg-1].insert(midState[peg-1].begin(), i);
    }
    for (int i = 1; i<=N; i++) {
        cin >> peg;
        finalState[peg-1].insert(finalState[peg-1].begin(), i);
    }
    minMoveNum = 7;
}

bool doMatch() {
    for (int i=0; i > &traces) {
    if (traces.size() >= minMoveNum)
        return false;
	if (doMatch()) {
		if (traces.size() < minMoveNum) {
			minMoveNum = traces.size();
			minTraces = traces;
		}
		return false;
	}

    for (int i=0; i0 && midState[j].back()(i+1, j+1));
			if (dfs(traces))
				return true;
			else {
				traces.pop_back();
				midState[j].pop_back();
			}
        }
		midState[i].push_back(from);
    }
	return false;
}

int main() {
	while (cin >> N >> K) {
		init();
		vector > traces;
		dfs(traces);
		cout << minMoveNum << endl;
		for (int i=0; i

你可能感兴趣的:(InterviewStreet)