InterviewStreet —— Hanoi Moves
来源:互联网 发布:韩国的文化知乎 编辑:程序博客网 时间:2024/06/10 13:06
昨天发现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 <iostream>#include <vector>#include <utility>using namespace std;int minMoveNum;int N, K;vector<pair<int, int> > minTraces;vector<vector<int> > midState;vector<vector<int> > finalState;int init() { midState.clear(); finalState.clear(); for (int i=0; i<K; i++) { vector<int> 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<K; i++) { if (midState[i].size() == finalState[i].size()) { for (int j=0; j<midState[i].size(); j++) if (midState[i][j] != finalState[i][j]) return false; } else return false; } return true;}bool dfs(vector<pair<int, int> > &traces) { if (traces.size() >= minMoveNum) return false;if (doMatch()) {if (traces.size() < minMoveNum) {minMoveNum = traces.size();minTraces = traces;}return false;} for (int i=0; i<K; i++) {if (midState[i].size() == 0)continue;int from = midState[i].back();midState[i].pop_back(); for (int j = (i+1)%K; j != i; j = (j+1)%K) {if (midState[j].size()>0 && midState[j].back()<from)continue;midState[j].push_back(from);traces.push_back(pair<int, int>(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<pair<int, int> > traces;dfs(traces);cout << minMoveNum << endl;for (int i=0; i<minMoveNum; i++)cout << minTraces[i].first << " " << minTraces[i].second << endl;}return 0;}
- InterviewStreet —— Hanoi Moves
- [FaceBook] Hanoi Moves, Solution
- poj1915——Knight Moves
- hdu——Knight Moves
- BFS——Knight Moves
- hdu1372——Knight Moves
- Uva439——Knight Moves
- 搜索—Problem_1015-Knight Moves
- 算法——汉诺塔(Hanoi)
- 初试——Hanoi塔问题
- 002递归——hanoi塔
- 递归——Hanoi塔问题
- Hanoi问题——解惑自己
- 算法分析——Hanoi塔问题
- 算法分析——Hanoi塔问题
- 简单递归—Hanoi(汉诺塔问题)
- InterviewStreet --Candies
- interviewstreet pair
- C# 中ref 和out 参数的区别
- 多态中虚函数表的地址是所有对象共享的
- 浅谈网络数据包传递过程中涉及的话题
- Oracle数据库备份和恢复(导出,导入)
- myeclipse web工程多余包部署问题(servlet-api.jar,jsp-api.jar冲突问题解决方法)
- InterviewStreet —— Hanoi Moves
- Starting nagios:CONFIG ERROR! Start aborted. Check your Nagios configuration
- 清除windows网上邻居的登陆帐号密码
- eclipse内存溢出报错:java.lang.OutOfMemoryError:
- cococs2d-x 多线程加载plist
- 如何查看mac系统是32位还是64位的操作系统
- libusb开发者指南
- 在任意位置获取应用程序Context
- Drupal7 Note-S1畅游互联网~