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

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

avatar
p*2
4
跟我一起做hackerrank不久没事了吗?
avatar
o*3
5
好的啊 怎么跟你一起做?

【在 p*****2 的大作中提到】
: 跟我一起做hackerrank不久没事了吗?
avatar
o*3
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;
}
avatar
r*n
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
avatar
p*2
8

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

【在 o******3 的大作中提到】
: 好的啊 怎么跟你一起做?
avatar
o*3
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());

avatar
r*n
10
恩,我就是来打酱油了 :)

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

avatar
p*2
11
看了一下。这题BF行吗?
avatar
o*3
12
弱问BF跟BFS的区别?

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

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

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

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

【在 o******3 的大作中提到】
: 弱问BF跟BFS的区别?
avatar
d*o
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;
}
avatar
o*3
17
不用backtrack, BFS存着路径就好了。
问题是要30分钟把这个无bug写出来。
我跪了。。。
这有点儿难度啊。。。

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

avatar
o*3
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];

avatar
x*0
19
mark
avatar
p*2
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(ifor(i=0){
for(jpegs(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(" ")
}
}
}
avatar
p*2
21

backtrack主要是节省空间。

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

avatar
p*2
22
哪里是这题的链接呢?
avatar
p*2
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
avatar
o*3
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

avatar
o*3
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

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

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

avatar
p*2
29
跟我一起做hackerrank不久没事了吗?
avatar
o*3
30
好的啊 怎么跟你一起做?

【在 p*****2 的大作中提到】
: 跟我一起做hackerrank不久没事了吗?
avatar
o*3
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;
}
avatar
r*n
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
avatar
p*2
33

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

【在 o******3 的大作中提到】
: 好的啊 怎么跟你一起做?
avatar
o*3
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());

avatar
r*n
35
恩,我就是来打酱油了 :)

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

avatar
p*2
36
看了一下。这题BF行吗?
avatar
o*3
37
弱问BF跟BFS的区别?

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

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

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

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

【在 o******3 的大作中提到】
: 弱问BF跟BFS的区别?
avatar
d*o
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;
}
avatar
o*3
42
不用backtrack, BFS存着路径就好了。
问题是要30分钟把这个无bug写出来。
我跪了。。。
这有点儿难度啊。。。

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

avatar
o*3
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];

avatar
x*0
44
mark
avatar
p*2
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(ifor(i=0){
for(jpegs(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(" ")
}
}
}
avatar
p*2
46

backtrack主要是节省空间。

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

avatar
p*2
47
哪里是这题的链接呢?
avatar
p*2
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
avatar
o*3
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

avatar
o*3
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

avatar
o*3
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; ifor(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; icout<step2<
return 0;
}
paths.push(currPath);
currPath.pop_back();
mymap[tmpStatus] = true;
}

}
}
}

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

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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。