## 10422 - Knights in FEN

Moderator: Board moderators

ditrix
New poster
Posts: 33
Joined: Sat Mar 01, 2003 12:38 am
Location: Paris

### 10422 - Knights in FEN

What is the best algorithm for this problem? I tried a backtracking and it ran for 2 seconds, but there are more rapid solutions, say instant...
@+!
DitriX

saiqbal
New poster
Posts: 36
Joined: Wed Aug 07, 2002 4:52 pm
Contact:
i used backtracking a with a little bound. i just checked the last move
not to be repeated.my soln. is not fast enough though!
but i think the best approach is to use BFS. some ppl used IDS also.

thanx
-sohel

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong
Hello.

I've just solved the problem by DFS. However, my program runs pretty slowly (>> 7 sec)!!!! >_<

I know that there are some much faster methods. Could anyone plz explain them to me??? Many thanks!

Note: after some simple modification, my program gets ACC in <<1 sec !
However, I'm still looking for faster algorithm...
Last edited by Observer on Mon Aug 04, 2003 12:31 pm, edited 2 times in total.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong
Say, how should I solve this problem within reasonable time if n <= 20???
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany
if N<=20, try bfs starting from starting and final position and check when they meet at some position.

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong
Could you explain more clearly pls??

By the way, it would be great if the path can be obtained as well!
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

CelebiX
New poster
Posts: 5
Joined: Tue Jun 18, 2002 11:05 am

### bidirectional bfs

i used IDA* and solved in 0.000sec.

if n<=20 try bfs from both the start and goal
the bfs tree grown from the start will meet the one grown from the goal at some node x
then start ~> x ~> goal is the path

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

### Re: bidirectional bfs

CelebiX wrote:i used IDA* and solved in 0.000sec.
Wow!!!!!!! Really great!!!!!!!!!
CelebiX wrote:if n<=20 try bfs from both the start and goal
the bfs tree grown from the start will meet the one grown from the goal at some node x
then start ~> x ~> goal is the path
This method sounds feasible! But I'm still not so familiar with BFS......
But why would this method be efficient? Could you explain plz?
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

route
New poster
Posts: 39
Joined: Sat Dec 21, 2002 1:25 am

### 10422

Would anyone tell me how to solve the problem ?

angga888
Experienced poster
Posts: 143
Joined: Sat Dec 21, 2002 11:41 am
Location: Indonesia
Hi route

You can use backtracking to solve this problem.

angga888

route
New poster
Posts: 39
Joined: Sat Dec 21, 2002 1:25 am
Can you tell me some more details ?
Since there is a restriction that it can only move by exchanging the position with the blank square .

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong
Hello! It's me again.

While doing BFS we need to keep a boolean array to see whether a "node" is visited, is that correct?

But in this question, possible configurations = 25!/(12! 12!) = 67603900
So keeping such a huge array is definitely impossible!! (MLE for sure!!!)

So what should I do? Plz help!!!!
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

Whinii F.
Experienced poster
Posts: 151
Joined: Wed Aug 21, 2002 12:07 am
Location: Seoul, Korea
Contact:
I came to see this post eventually and did not solve this problem.

Anyway.. as I understand the post, the size of the state space is pretty large, but by using the method posted above, the number of nodes that you actually have to traverse can become VERY small. So you can apply some hashing or clever data structures to check if you have visited a node already or not.

It's a late night in Korea and I'm going to bed now, but when I get an AC I'll come back
JongMan @ Yonsei

Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong
I see that the number of "nodes" visited will never exceed 60000......

But how to implement??
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org

Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:
I hashed/precalc from the end position to 11 moves away, and while it's not one of the 0.0 solutions, it's at ~ 0.57 seconds.. so ah well.. good enough for me.. =)