由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 面试F家让先做programming puzzle
相关主题
[提议]算法coding题目需要太傻那样的黑宝书amazon的那道题目
汉若塔问题Interview questions, Bloomberg
FB面试题一道 求解一个容易记忆的permutation算法
snapchat面经,已挂好记(但不是最优)的combination算法
FB coding challenge sample questionone C++ question
请教一道题C++ object size一问
上个啰嗦的2西格玛失败面经吧One C++ question
大家刷题怎么debugone C++ question
相关话题的讨论汇总
话题: int话题: pegs话题: std话题: vector话题: peg
进入JobHunting版参与讨论
1 (共1页)
o******3
发帖数: 91
1
找人Refer了F家
HR写信来让先做一个programming puzzle
有人做过么?难度怎么样?求指导
我刚才在网上做了一下facebook programming puzzle的Sample
就是那个hanoi的题目
我思路是用BFS
不过没能在规定时间内调好程序啊。。。汗
我先熟悉了一下做题环境,输入输出,然后慢慢悠悠开始做,开始还没看到有时间限制
,剩下5分钟他开始提示我的时候 我才意识到还有时间限制。。。然后然后最后五分钟
狂写 也没来得及。。。
r*********n
发帖数: 4553
2
hanoi tower?
这个复杂度是指数级吧
o******3
发帖数: 91
3
他只需要最短路径 而且规定了小于等于7步
所以就还好

【在 r*********n 的大作中提到】
: hanoi tower?
: 这个复杂度是指数级吧

p*****2
发帖数: 21240
4
跟我一起做hackerrank不久没事了吗?
o******3
发帖数: 91
5
好的啊 怎么跟你一起做?

【在 p*****2 的大作中提到】
: 跟我一起做hackerrank不久没事了吗?
o******3
发帖数: 91
6
决定把题目跟我写的很挫的代码贴上来,有需要的同学可以来看。。。
代码可以过sample test
但是目前还不能过所有的Test
题目:
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
代码
写的很挫,主要思想就是BFS
#include
#include
#include
#include
#include
#include
using namespace std;
struct aStatus{
vector > pegs;
vector path;

aStatus(vector > pe, vector pa)
{
pegs = pe;
path = pa;
}
};
bool isDone(vector > pegs, int finalpos, int N)
{
if(pegs[finalpos].size() return false;
for(int i=0; i {
int curr = pegs[finalpos].top();
pegs[finalpos].pop();
if(curr> pegs[finalpos].top())
return false;
}
return true;
}
void printRes(vector path)
{
cout<
for(int i=0; i {
if(i%2==0)
cout<
cout< }
cout< }
void move(vector >& pegs, int fir, int sec)
{
int tmp = pegs[fir].top();
pegs[fir].pop();
pegs[sec].push(tmp);
}
bool checkMove(vector > pegs, int i, int j)
{
if(i==j)
return false;
if((pegs[i].size()>0)&&(pegs[j].size()>0)&&(pegs[i].top() return true;
if((pegs[i].size()>0)&&(pegs[j].size()==0))
return true;
return false;
}
bool bfs(vector > pegs, int N, int K, int finalpos, vector
path)
{
queue myqueue;
set > > mymap;
myqueue.push(new aStatus(pegs, path));

while(!myqueue.empty())
{
aStatus* currStatus = myqueue.front();
pegs = currStatus->pegs;
path = currStatus->path;
mymap.insert(currStatus->pegs);
myqueue.pop();

for(int i=1; i<=K; i++)
{
for(int j=1; j<=K; j++)
{
if(checkMove(pegs, i, j))
{
move(pegs, i, j);
path.push_back(i);
path.push_back(j);

if(isDone(pegs, finalpos, N))
{
printRes(path);
return true;
}

aStatus* newStatus = new aStatus(pegs, path);
if(mymap.count(newStatus->pegs)==0)
{
myqueue.push(new aStatus(pegs, path));
}
move(pegs, j, i);
path.pop_back();
path.pop_back();
}

}
}
}

return false;
}
int main()
{
int N;
int K;
cin>>N;
cin>>K;

vector > pegs;
pegs.resize(K+1);
vector line2;

int pos = 0;
for(int i=0; i {
cin>>pos;
line2.push_back(pos);
}

for(int i=N-1; i>=0; i--)
{
pegs[line2[i]].push(i+1);
}

int finalpos = 0;
for(int i=0; i {
cin>>finalpos;
}

vector path;
bool status = bfs(pegs, N, K, finalpos, path);
return 0;
}
r*********n
发帖数: 4553
7
this is what i wrote a while ago to simulate hanoi tower. not sure if it
solves this problem, but probably yes with some modifications
typedef stack ST;
class TowerHanoi{
void mvtower(ST& ls, ST& ms, ST& rs, int sz){
int level = rs.size();
while( sz-- > 0 ) mvdisk(ls, ms, rs, level);
}
void mvdisk(ST& ls, ST& ms, ST& rs, int level){
ms.push(ls.top());
ls.pop();
int sz = int(rs.size())-level;
mvtower(rs, ms, ls, sz);
rs.push(ms.top());
ms.pop();
mvtower(ls, ms, rs, sz);
}
public:
void play(ST& ls, ST& ms, ST& rs){
mvtower(ls, ms, rs, ls.size());
}
};
game starts with all disks on left tower, empty middle and right towers.
game ends when all disks are on the right tower
p*****2
发帖数: 21240
8

去hackerrank.com
F的OJ就是他们提供的。做熟了hackerrank做F和startup OJ问题不大了。

【在 o******3 的大作中提到】
: 好的啊 怎么跟你一起做?
o******3
发帖数: 91
9
哥们,你这是C++书上的hanoi, 跟题目完全不一样啊。
你看我写了那么长的。。。

【在 r*********n 的大作中提到】
: this is what i wrote a while ago to simulate hanoi tower. not sure if it
: solves this problem, but probably yes with some modifications
: typedef stack ST;
: class TowerHanoi{
: void mvtower(ST& ls, ST& ms, ST& rs, int sz){
: int level = rs.size();
: while( sz-- > 0 ) mvdisk(ls, ms, rs, level);
: }
: void mvdisk(ST& ls, ST& ms, ST& rs, int level){
: ms.push(ls.top());

r*********n
发帖数: 4553
10
恩,我就是来打酱油了 :)

【在 o******3 的大作中提到】
: 哥们,你这是C++书上的hanoi, 跟题目完全不一样啊。
: 你看我写了那么长的。。。

相关主题
请教一道题amazon的那道题目
上个啰嗦的2西格玛失败面经吧Interview questions, Bloomberg
大家刷题怎么debug一个容易记忆的permutation算法
进入JobHunting版参与讨论
p*****2
发帖数: 21240
11
看了一下。这题BF行吗?
o******3
发帖数: 91
12
弱问BF跟BFS的区别?

【在 p*****2 的大作中提到】
: 看了一下。这题BF行吗?
o******3
发帖数: 91
13
好的 我也去做做

【在 p*****2 的大作中提到】
: 看了一下。这题BF行吗?
o******3
发帖数: 91
14
谢谢二爷指点

【在 p*****2 的大作中提到】
: 看了一下。这题BF行吗?
p*****2
发帖数: 21240
15

想了一下 最短路径 确实bfs + backtrack

【在 o******3 的大作中提到】
: 弱问BF跟BFS的区别?
d****o
发帖数: 2
16
抛砖引玉
#include
#include
#include
const int kMaxK = 5;
const int kMaxN = 8;
const int kMaxS = 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5;
#define DEBUG(v) std::cerr << #v << " = " << (v) << "\n"
int N, K, max_state;
short top[kMaxS][kMaxK];
int G[kMaxS][kMaxK * (kMaxK - 1) + 1];
int src = 0, dst = 0;
int f[kMaxS];
int pow(int base, int power) {
int r = 1;
for (int i = 0; i < power; r *= base, ++i);
return r;
}
int get(int num, int d) {
for (int i = 0; i < d; num /= K, ++i);
return num % K;
}
int set(int num, int d, int v) {
int co = 1, r;
for (int i = 0; i < d; co *= K, ++i);
r = num % co;
return ((num / (co * K)) * K + v) * co + r;
}
int init() {
max_state = pow(K, N);
//DEBUG(max_state);
for (int state = 0; state < pow(K, N); ++state) {
//for (int disc = 0; disc < N; ++disc) { std::cout << get(state, disc); }
//std::cout << std::endl;
for (int peg = 0; peg < K; ++peg) {
top[state][peg] = -1;
for (int disc = 0; disc < N; ++disc) {
if (get(state, disc) == peg) {
top[state][peg] = disc;
break;
}
}
//std::cout << top[state][peg] << " ";
}
//std::cout << std::endl;
}
return 0;
}
int BuildGraph() {
memset(G, 0, sizeof(G));
for (int state = 0; state < max_state; ++state) {
for (int pfr = 0; pfr < K; ++pfr) {
for (int pto = 0; pto < K; ++pto) {
if (pfr != pto && top[state][pfr] != -1 &&
(top[state][pto] == -1 || top[state][pfr] < top[state][pto])) {
//std::cout << state << " " << pfr << " " << pto << std::endl;
//for (int i = 0; i < N; ++i) { std::cout << get(state, i); }
//std::cout << "--(" << pfr << ", " << pto << ")-->";
G[state][++G[state][0]] = set(state, top[state][pfr], pto);
//int tmp = G[state][G[state][0]];
//for (int i = 0; i < N; ++i) { std::cout << get(tmp, i); }
//std::cout << std::endl;
//std::cout << G[state][G[state][0]] << std::endl;
}
}
}
}
return 0;
}
void print(int v) {
if (f[v] != v) {
print(f[v]);
for (int i = 0; i < K; ++i) {
int pfr = get(f[v], i), pto = get(v, i);
if (pfr != pto) {
std::cout << pfr + 1 << " " << pto + 1 << std::endl;
break;
}
}
}
}
int solve() {
std::cin >> N >> K;
init();
BuildGraph();
int co = 1;
for (int i = 0; i < N; ++i, co *= K) {
int peg;
std::cin >> peg;
src = src + (peg - 1) * co;
}
co = 1;
for (int i = 0; i < N; ++i, co *= K) {
int peg;
std::cin >> peg;
dst = dst + (peg - 1) * co;
}
//DEBUG(src);
//DEBUG(dst);
for (int state = 0; state < max_state; f[state++] = -1);
f[src] = src;
std::vector queue;
queue.push_back(src);
int top = 0;
while (f[dst] == -1) {
int v = queue[top++];
for (int i = 1; i <= G[v][0]; ++i) {
if (f[G[v][i]] == -1) {
//for (int ii = 0; ii < N; ++ii) {std::cout << get(v, ii); } std::
cout << "->";
//for (int ii = 0; ii < N; ++ii) {std::cout << get(G[v][i], ii); }
std::cout << std::endl;
f[G[v][i]] = v;
queue.push_back(G[v][i]);
}
}
}
print(dst);
return 0;
}
int main(int argc, char *argv[]) {
solve();
return 0;
}
o******3
发帖数: 91
17
不用backtrack, BFS存着路径就好了。
问题是要30分钟把这个无bug写出来。
我跪了。。。
这有点儿难度啊。。。

【在 p*****2 的大作中提到】
:
: 想了一下 最短路径 确实bfs + backtrack

o******3
发帖数: 91
18
稍微解释一下可以不?

【在 d****o 的大作中提到】
: 抛砖引玉
: #include
: #include
: #include
: const int kMaxK = 5;
: const int kMaxN = 8;
: const int kMaxS = 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5;
: #define DEBUG(v) std::cerr << #v << " = " << (v) << "\n"
: int N, K, max_state;
: short top[kMaxS][kMaxK];

x*****0
发帖数: 452
19
mark
p*****2
发帖数: 21240
20
我写了一个。看看有没有问题?
import collection.mutable.{Map, Queue}
object test2 extends App {
val Array(n,k)=readLine.split(" ").map(_.toInt)
val start=readLine.split(" ").map(_.toInt-1).mkString(" ") //pegs and
discs start from 0
val end=readLine.split(" ").map(_.toInt-1).mkString(" ")

val queue=new Queue[String]()
val map=Map[String,String]()

var distance=0
var count=1
queue+=end
map(end)=null
while(count>0){
distance+=1
while(count>0){
val curr=queue.dequeue
move(curr)
if(map.contains(start))
{
result()
exit(0)
}
count-=1
}
count=queue.size
}

def move(s:String)={
val arr=s.split(" ").map(_.toInt)
val pegs=Array.fill[Int](k)(-1)
for(i<-n-1 to 0 by -1) pegs(arr(i))=i
for(i<-0 until k if pegs(i)>=0){
for(j<-0 until k if i!=j && (pegs(j)== -1 || pegs(j)>pegs(i))){
val tmp=arr(pegs(i))
arr(pegs(i))=j
val key=arr.mkString(" ")
if(!map.contains(key)){
map(key)=pegs(i)+" "+i+" "+j //disc pegs(i) from peg i
to j
queue+=key
}
arr(pegs(i))=tmp;
}
}
}

def result()={
println(distance)
var s=start
while(map(s)!=null){
val arr=s.split(" ").map(_.toInt)
val move=map(s).split(" ").map(_.toInt)
println((move(0)+1)+" "+(move(1)+1))
arr(move(0))=move(1)
s=arr.mkString(" ")
}
}
}
相关主题
好记(但不是最优)的combination算法One C++ question
one C++ questionone C++ question
C++ object size一问发个题目给大家复习一下marco
进入JobHunting版参与讨论
p*****2
发帖数: 21240
21

backtrack主要是节省空间。

【在 o******3 的大作中提到】
: 不用backtrack, BFS存着路径就好了。
: 问题是要30分钟把这个无bug写出来。
: 我跪了。。。
: 这有点儿难度啊。。。

p*****2
发帖数: 21240
22
哪里是这题的链接呢?
p*****2
发帖数: 21240
23
找到了。过了test case了。把题解放到博客里了。
http://blog.sina.com.cn/s/blog_b9285de20101jdr3.html
6/6 testcases passed
TestCase #0
Status: Passed
Your output:
3
1 3
1 2
3 2
TestCase #1
Status: Passed
Your output:
5
3 1
4 3
4 1
2 1
3 1
TestCase #2 (Hidden)
Status: Passed
TestCase #3 (Hidden)
Status: Passed
TestCase #4 (Hidden)
Status: Passed
TestCase #5 (Hidden)
Status: Passed
o******3
发帖数: 91
24
二爷威武 等我回头好好拜读一下

【在 p*****2 的大作中提到】
: 找到了。过了test case了。把题解放到博客里了。
: http://blog.sina.com.cn/s/blog_b9285de20101jdr3.html
: 6/6 testcases passed
: TestCase #0
: Status: Passed
: Your output:
: 3
: 1 3
: 1 2
: 3 2

o******3
发帖数: 91
25
嗯 有道理

【在 p*****2 的大作中提到】
: 找到了。过了test case了。把题解放到博客里了。
: http://blog.sina.com.cn/s/blog_b9285de20101jdr3.html
: 6/6 testcases passed
: TestCase #0
: Status: Passed
: Your output:
: 3
: 1 3
: 1 2
: 3 2

o******3
发帖数: 91
26
找人Refer了F家
HR写信来让先做一个programming puzzle
有人做过么?难度怎么样?求指导
我刚才在网上做了一下facebook programming puzzle的Sample
就是那个hanoi的题目
我思路是用BFS
不过没能在规定时间内调好程序啊。。。汗
我先熟悉了一下做题环境,输入输出,然后慢慢悠悠开始做,开始还没看到有时间限制
,剩下5分钟他开始提示我的时候 我才意识到还有时间限制。。。然后然后最后五分钟
狂写 也没来得及。。。
r*********n
发帖数: 4553
27
hanoi tower?
这个复杂度是指数级吧
o******3
发帖数: 91
28
他只需要最短路径 而且规定了小于等于7步
所以就还好

【在 r*********n 的大作中提到】
: hanoi tower?
: 这个复杂度是指数级吧

p*****2
发帖数: 21240
29
跟我一起做hackerrank不久没事了吗?
o******3
发帖数: 91
30
好的啊 怎么跟你一起做?

【在 p*****2 的大作中提到】
: 跟我一起做hackerrank不久没事了吗?
相关主题
Why I can't compile this function successfully汉若塔问题
C++: what is the output? How to interpret it?FB面试题一道 求解
[提议]算法coding题目需要太傻那样的黑宝书snapchat面经,已挂
进入JobHunting版参与讨论
o******3
发帖数: 91
31
决定把题目跟我写的很挫的代码贴上来,有需要的同学可以来看。。。
代码可以过sample test
但是目前还不能过所有的Test
题目:
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
代码
写的很挫,主要思想就是BFS
#include
#include
#include
#include
#include
#include
using namespace std;
struct aStatus{
vector > pegs;
vector path;

aStatus(vector > pe, vector pa)
{
pegs = pe;
path = pa;
}
};
bool isDone(vector > pegs, int finalpos, int N)
{
if(pegs[finalpos].size() return false;
for(int i=0; i {
int curr = pegs[finalpos].top();
pegs[finalpos].pop();
if(curr> pegs[finalpos].top())
return false;
}
return true;
}
void printRes(vector path)
{
cout<
for(int i=0; i {
if(i%2==0)
cout<
cout< }
cout< }
void move(vector >& pegs, int fir, int sec)
{
int tmp = pegs[fir].top();
pegs[fir].pop();
pegs[sec].push(tmp);
}
bool checkMove(vector > pegs, int i, int j)
{
if(i==j)
return false;
if((pegs[i].size()>0)&&(pegs[j].size()>0)&&(pegs[i].top() return true;
if((pegs[i].size()>0)&&(pegs[j].size()==0))
return true;
return false;
}
bool bfs(vector > pegs, int N, int K, int finalpos, vector
path)
{
queue myqueue;
set > > mymap;
myqueue.push(new aStatus(pegs, path));

while(!myqueue.empty())
{
aStatus* currStatus = myqueue.front();
pegs = currStatus->pegs;
path = currStatus->path;
mymap.insert(currStatus->pegs);
myqueue.pop();

for(int i=1; i<=K; i++)
{
for(int j=1; j<=K; j++)
{
if(checkMove(pegs, i, j))
{
move(pegs, i, j);
path.push_back(i);
path.push_back(j);

if(isDone(pegs, finalpos, N))
{
printRes(path);
return true;
}

aStatus* newStatus = new aStatus(pegs, path);
if(mymap.count(newStatus->pegs)==0)
{
myqueue.push(new aStatus(pegs, path));
}
move(pegs, j, i);
path.pop_back();
path.pop_back();
}

}
}
}

return false;
}
int main()
{
int N;
int K;
cin>>N;
cin>>K;

vector > pegs;
pegs.resize(K+1);
vector line2;

int pos = 0;
for(int i=0; i {
cin>>pos;
line2.push_back(pos);
}

for(int i=N-1; i>=0; i--)
{
pegs[line2[i]].push(i+1);
}

int finalpos = 0;
for(int i=0; i {
cin>>finalpos;
}

vector path;
bool status = bfs(pegs, N, K, finalpos, path);
return 0;
}
r*********n
发帖数: 4553
32
this is what i wrote a while ago to simulate hanoi tower. not sure if it
solves this problem, but probably yes with some modifications
typedef stack ST;
class TowerHanoi{
void mvtower(ST& ls, ST& ms, ST& rs, int sz){
int level = rs.size();
while( sz-- > 0 ) mvdisk(ls, ms, rs, level);
}
void mvdisk(ST& ls, ST& ms, ST& rs, int level){
ms.push(ls.top());
ls.pop();
int sz = int(rs.size())-level;
mvtower(rs, ms, ls, sz);
rs.push(ms.top());
ms.pop();
mvtower(ls, ms, rs, sz);
}
public:
void play(ST& ls, ST& ms, ST& rs){
mvtower(ls, ms, rs, ls.size());
}
};
game starts with all disks on left tower, empty middle and right towers.
game ends when all disks are on the right tower
p*****2
发帖数: 21240
33

去hackerrank.com
F的OJ就是他们提供的。做熟了hackerrank做F和startup OJ问题不大了。

【在 o******3 的大作中提到】
: 好的啊 怎么跟你一起做?
o******3
发帖数: 91
34
哥们,你这是C++书上的hanoi, 跟题目完全不一样啊。
你看我写了那么长的。。。

【在 r*********n 的大作中提到】
: this is what i wrote a while ago to simulate hanoi tower. not sure if it
: solves this problem, but probably yes with some modifications
: typedef stack ST;
: class TowerHanoi{
: void mvtower(ST& ls, ST& ms, ST& rs, int sz){
: int level = rs.size();
: while( sz-- > 0 ) mvdisk(ls, ms, rs, level);
: }
: void mvdisk(ST& ls, ST& ms, ST& rs, int level){
: ms.push(ls.top());

r*********n
发帖数: 4553
35
恩,我就是来打酱油了 :)

【在 o******3 的大作中提到】
: 哥们,你这是C++书上的hanoi, 跟题目完全不一样啊。
: 你看我写了那么长的。。。

p*****2
发帖数: 21240
36
看了一下。这题BF行吗?
o******3
发帖数: 91
37
弱问BF跟BFS的区别?

【在 p*****2 的大作中提到】
: 看了一下。这题BF行吗?
o******3
发帖数: 91
38
好的 我也去做做

【在 p*****2 的大作中提到】
: 看了一下。这题BF行吗?
o******3
发帖数: 91
39
谢谢二爷指点

【在 p*****2 的大作中提到】
: 看了一下。这题BF行吗?
p*****2
发帖数: 21240
40

想了一下 最短路径 确实bfs + backtrack

【在 o******3 的大作中提到】
: 弱问BF跟BFS的区别?
相关主题
snapchat面经,已挂上个啰嗦的2西格玛失败面经吧
FB coding challenge sample question大家刷题怎么debug
请教一道题amazon的那道题目
进入JobHunting版参与讨论
d****o
发帖数: 2
41
抛砖引玉
#include
#include
#include
const int kMaxK = 5;
const int kMaxN = 8;
const int kMaxS = 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5;
#define DEBUG(v) std::cerr << #v << " = " << (v) << "\n"
int N, K, max_state;
short top[kMaxS][kMaxK];
int G[kMaxS][kMaxK * (kMaxK - 1) + 1];
int src = 0, dst = 0;
int f[kMaxS];
int pow(int base, int power) {
int r = 1;
for (int i = 0; i < power; r *= base, ++i);
return r;
}
int get(int num, int d) {
for (int i = 0; i < d; num /= K, ++i);
return num % K;
}
int set(int num, int d, int v) {
int co = 1, r;
for (int i = 0; i < d; co *= K, ++i);
r = num % co;
return ((num / (co * K)) * K + v) * co + r;
}
int init() {
max_state = pow(K, N);
//DEBUG(max_state);
for (int state = 0; state < pow(K, N); ++state) {
//for (int disc = 0; disc < N; ++disc) { std::cout << get(state, disc); }
//std::cout << std::endl;
for (int peg = 0; peg < K; ++peg) {
top[state][peg] = -1;
for (int disc = 0; disc < N; ++disc) {
if (get(state, disc) == peg) {
top[state][peg] = disc;
break;
}
}
//std::cout << top[state][peg] << " ";
}
//std::cout << std::endl;
}
return 0;
}
int BuildGraph() {
memset(G, 0, sizeof(G));
for (int state = 0; state < max_state; ++state) {
for (int pfr = 0; pfr < K; ++pfr) {
for (int pto = 0; pto < K; ++pto) {
if (pfr != pto && top[state][pfr] != -1 &&
(top[state][pto] == -1 || top[state][pfr] < top[state][pto])) {
//std::cout << state << " " << pfr << " " << pto << std::endl;
//for (int i = 0; i < N; ++i) { std::cout << get(state, i); }
//std::cout << "--(" << pfr << ", " << pto << ")-->";
G[state][++G[state][0]] = set(state, top[state][pfr], pto);
//int tmp = G[state][G[state][0]];
//for (int i = 0; i < N; ++i) { std::cout << get(tmp, i); }
//std::cout << std::endl;
//std::cout << G[state][G[state][0]] << std::endl;
}
}
}
}
return 0;
}
void print(int v) {
if (f[v] != v) {
print(f[v]);
for (int i = 0; i < K; ++i) {
int pfr = get(f[v], i), pto = get(v, i);
if (pfr != pto) {
std::cout << pfr + 1 << " " << pto + 1 << std::endl;
break;
}
}
}
}
int solve() {
std::cin >> N >> K;
init();
BuildGraph();
int co = 1;
for (int i = 0; i < N; ++i, co *= K) {
int peg;
std::cin >> peg;
src = src + (peg - 1) * co;
}
co = 1;
for (int i = 0; i < N; ++i, co *= K) {
int peg;
std::cin >> peg;
dst = dst + (peg - 1) * co;
}
//DEBUG(src);
//DEBUG(dst);
for (int state = 0; state < max_state; f[state++] = -1);
f[src] = src;
std::vector queue;
queue.push_back(src);
int top = 0;
while (f[dst] == -1) {
int v = queue[top++];
for (int i = 1; i <= G[v][0]; ++i) {
if (f[G[v][i]] == -1) {
//for (int ii = 0; ii < N; ++ii) {std::cout << get(v, ii); } std::
cout << "->";
//for (int ii = 0; ii < N; ++ii) {std::cout << get(G[v][i], ii); }
std::cout << std::endl;
f[G[v][i]] = v;
queue.push_back(G[v][i]);
}
}
}
print(dst);
return 0;
}
int main(int argc, char *argv[]) {
solve();
return 0;
}
o******3
发帖数: 91
42
不用backtrack, BFS存着路径就好了。
问题是要30分钟把这个无bug写出来。
我跪了。。。
这有点儿难度啊。。。

【在 p*****2 的大作中提到】
:
: 想了一下 最短路径 确实bfs + backtrack

o******3
发帖数: 91
43
稍微解释一下可以不?

【在 d****o 的大作中提到】
: 抛砖引玉
: #include
: #include
: #include
: const int kMaxK = 5;
: const int kMaxN = 8;
: const int kMaxS = 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5;
: #define DEBUG(v) std::cerr << #v << " = " << (v) << "\n"
: int N, K, max_state;
: short top[kMaxS][kMaxK];

x*****0
发帖数: 452
44
mark
p*****2
发帖数: 21240
45
我写了一个。看看有没有问题?
import collection.mutable.{Map, Queue}
object test2 extends App {
val Array(n,k)=readLine.split(" ").map(_.toInt)
val start=readLine.split(" ").map(_.toInt-1).mkString(" ") //pegs and
discs start from 0
val end=readLine.split(" ").map(_.toInt-1).mkString(" ")

val queue=new Queue[String]()
val map=Map[String,String]()

var distance=0
var count=1
queue+=end
map(end)=null
while(count>0){
distance+=1
while(count>0){
val curr=queue.dequeue
move(curr)
if(map.contains(start))
{
result()
exit(0)
}
count-=1
}
count=queue.size
}

def move(s:String)={
val arr=s.split(" ").map(_.toInt)
val pegs=Array.fill[Int](k)(-1)
for(i<-n-1 to 0 by -1) pegs(arr(i))=i
for(i<-0 until k if pegs(i)>=0){
for(j<-0 until k if i!=j && (pegs(j)== -1 || pegs(j)>pegs(i))){
val tmp=arr(pegs(i))
arr(pegs(i))=j
val key=arr.mkString(" ")
if(!map.contains(key)){
map(key)=pegs(i)+" "+i+" "+j //disc pegs(i) from peg i
to j
queue+=key
}
arr(pegs(i))=tmp;
}
}
}

def result()={
println(distance)
var s=start
while(map(s)!=null){
val arr=s.split(" ").map(_.toInt)
val move=map(s).split(" ").map(_.toInt)
println((move(0)+1)+" "+(move(1)+1))
arr(move(0))=move(1)
s=arr.mkString(" ")
}
}
}
p*****2
发帖数: 21240
46

backtrack主要是节省空间。

【在 o******3 的大作中提到】
: 不用backtrack, BFS存着路径就好了。
: 问题是要30分钟把这个无bug写出来。
: 我跪了。。。
: 这有点儿难度啊。。。

p*****2
发帖数: 21240
47
哪里是这题的链接呢?
p*****2
发帖数: 21240
48
找到了。过了test case了。把题解放到博客里了。
http://blog.sina.com.cn/s/blog_b9285de20101jdr3.html
6/6 testcases passed
TestCase #0
Status: Passed
Your output:
3
1 3
1 2
3 2
TestCase #1
Status: Passed
Your output:
5
3 1
4 3
4 1
2 1
3 1
TestCase #2 (Hidden)
Status: Passed
TestCase #3 (Hidden)
Status: Passed
TestCase #4 (Hidden)
Status: Passed
TestCase #5 (Hidden)
Status: Passed
o******3
发帖数: 91
49
二爷威武 等我回头好好拜读一下

【在 p*****2 的大作中提到】
: 找到了。过了test case了。把题解放到博客里了。
: http://blog.sina.com.cn/s/blog_b9285de20101jdr3.html
: 6/6 testcases passed
: TestCase #0
: Status: Passed
: Your output:
: 3
: 1 3
: 1 2
: 3 2

o******3
发帖数: 91
50
嗯 有道理

【在 p*****2 的大作中提到】
: 找到了。过了test case了。把题解放到博客里了。
: http://blog.sina.com.cn/s/blog_b9285de20101jdr3.html
: 6/6 testcases passed
: TestCase #0
: Status: Passed
: Your output:
: 3
: 1 3
: 1 2
: 3 2

相关主题
Interview questions, Bloombergone C++ question
一个容易记忆的permutation算法C++ object size一问
好记(但不是最优)的combination算法One C++ question
进入JobHunting版参与讨论
o******3
发帖数: 91
51
我今天又写了一下
现在这个可以过所有数据集合了
方法还是BFS
区别的 这次我没有模拟stack
直接在Array上操作 这样程序更简单 写的可以快一点
#include
#include
#include
#include
#include
using namespace std;
struct step
{
int step1;
int step2;
step(int s1, int s2)
{
step1 = s1;
step2 = s2;
}
};
int main()
{
int N; int K;
cin>>N;
cin>>K;

vector IniArray;

int currpeg;
for(int i=1; i<=N; i++)
{
cin>>currpeg;
IniArray.push_back(currpeg);
}

vector FinalArray;

for(int i=1; i<=N; i++)
{
cin>>currpeg;
FinalArray.push_back(currpeg);
}

queue< vector > myqueue;
queue< vector > paths;
map, bool> mymap;
myqueue.push(IniArray);
mymap[IniArray]= true;
vector Inistep;
Inistep.push_back(step(0,0));
paths.push(Inistep);
while(!myqueue.empty())
{
vector currStatus = myqueue.front();
myqueue.pop();
vector currPath = paths.front();
paths.pop();
for(int i=0; i for(int j=1; j<=K; j++)
{
bool changeAble = true;
for(int m=0; m {
if(currStatus[m]==j || currStatus[m] == currStatus[i])
changeAble = false;
}
if(changeAble && currStatus[i]!=j)
{
vector tmpStatus = currStatus;
tmpStatus[i]=j;
if(!mymap[tmpStatus])
{
myqueue.push(tmpStatus);
currPath.push_back(step(currStatus[i], j));
if(tmpStatus == FinalArray)
{
cout< for(int i=1; i cout< step2<
return 0;
}
paths.push(currPath);
currPath.pop_back();
mymap[tmpStatus] = true;
}

}
}
}

return 0;
}
d********e
发帖数: 13
52
遇到过。
你得注意下不是所有题都是30分钟。我当时看sample是30分钟,但是做的题是60分钟的
。难度与时间近似线性递增。

【在 o******3 的大作中提到】
: 找人Refer了F家
: HR写信来让先做一个programming puzzle
: 有人做过么?难度怎么样?求指导
: 我刚才在网上做了一下facebook programming puzzle的Sample
: 就是那个hanoi的题目
: 我思路是用BFS
: 不过没能在规定时间内调好程序啊。。。汗
: 我先熟悉了一下做题环境,输入输出,然后慢慢悠悠开始做,开始还没看到有时间限制
: ,剩下5分钟他开始提示我的时候 我才意识到还有时间限制。。。然后然后最后五分钟
: 狂写 也没来得及。。。

1 (共1页)
进入JobHunting版参与讨论
相关主题
one C++ questionFB coding challenge sample question
发个题目给大家复习一下marco请教一道题
Why I can't compile this function successfully上个啰嗦的2西格玛失败面经吧
C++: what is the output? How to interpret it?大家刷题怎么debug
[提议]算法coding题目需要太傻那样的黑宝书amazon的那道题目
汉若塔问题Interview questions, Bloomberg
FB面试题一道 求解一个容易记忆的permutation算法
snapchat面经,已挂好记(但不是最优)的combination算法
相关话题的讨论汇总
话题: int话题: pegs话题: std话题: vector话题: peg