## ACM PKU Eight Puzzle

Post here if you don't find any other place for your post. But please, stay on-topic: algorithms, programming or something related to this web site and its services.

Moderator: Board moderators

### ACM PKU Eight Puzzle

http://acm.pku.edu.cn/JudgeOnline/problem?id=1077
For anyone that has got accepted in this problem, please help me! I got WAs so many... what else I might miss? I just used A* with priority_queue. It works for all TC I created myself....

fushar
New poster

Posts: 26
Joined: Fri Apr 03, 2009 12:09 pm
Location: Indonesia

### Re: ACM PKU Eight Puzzle

Just do a simple breadth-first search.

You can find some I/O from the original ACM contest here:
http://www.acm.inf.ethz.ch/ProblemSetAr ... hCen/1998/
mf
Guru

Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland

### Re: ACM PKU Eight Puzzle

DId you mean that I should do BFS from final state to ALL reachable states?
Oh one more, how did you mark a state as visited?

fushar
New poster

Posts: 26
Joined: Fri Apr 03, 2009 12:09 pm
Location: Indonesia

### Re: ACM PKU Eight Puzzle

fushar wrote:DId you mean that I should do BFS from final state to ALL reachable states?

There's only about 9!/2 = 181440 reachable states. 1 second is more than enough time to check them all.

Oh one more, how did you mark a state as visited?

Convert permutation into an integer in any way, and store them in std::set<int>.
(Or a boolean array, if you're not lazy, and the range of integers is small enough)
mf
Guru

Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland

### Re: ACM PKU Eight Puzzle

Oh, I see. So if the queried state is not visited, then I should output "unsolvable". Right?
Anyway thanks for your help. I'll try to recode my solution.

fushar
New poster

Posts: 26
Joined: Fri Apr 03, 2009 12:09 pm
Location: Indonesia

### Re: ACM PKU Eight Puzzle

Uh, I just used plain BFS without heuristic, and got TLE....

fushar
New poster

Posts: 26
Joined: Fri Apr 03, 2009 12:09 pm
Location: Indonesia

### Re: ACM PKU Eight Puzzle

My old BFS solution for UVa's problem 652 gets accepted at pku, running 0.438 seconds.

It doesn't use STL, though as I was coding in C - it converts each permutation into its sequential number (from 0 to 9!-1) and uses a boolean array to mark visited states.
mf
Guru

Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland

### Re: ACM PKU Eight Puzzle

How did you do the conversion?

fushar
New poster

Posts: 26
Joined: Fri Apr 03, 2009 12:09 pm
Location: Indonesia

### Re: ACM PKU Eight Puzzle

Like this:
Code: Select all
`int fact[10];  // a table of factorialsint encode(int a[9]) {   int c[10], i, k, r;   for (i = 0; i < 9; i++) c[i] = i;   for (r = 0, k = 0; k < 9; k++) {      r += c[a[k]] * fact[8 - k];      for (i = a[k]; i < 9; i++) c[i]--;   }   return r + 1;}void decode(int a[9], int r) {   int c[10], i, k;   for (i = 0; i < 9; i++) c[i] = i;   for (r--, k = 0; k < 9; k++) {      i = r / fact[8 - k];      r %= fact[8 - k];      a[k] = c[i];      for (; i < 9; i++) c[i] = c[i + 1];   }}`
mf
Guru

Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland

### Re: ACM PKU Eight Puzzle

Hey thank you so much.......!!!!!

fushar
New poster

Posts: 26
Joined: Fri Apr 03, 2009 12:09 pm
Location: Indonesia

### Re: ACM PKU Eight Puzzle

I'm so tired getting WAs, what's wrong with my code?
Code: Select all
`Deleted AC code`
Last edited by fushar on Tue Apr 21, 2009 5:23 pm, edited 1 time in total.

fushar
New poster

Posts: 26
Joined: Fri Apr 03, 2009 12:09 pm
Location: Indonesia

### Re: ACM PKU Eight Puzzle

Your program prints \0 character at the beginning of output:

Code: Select all
`user@localhost:/tmp\$ g++ -o p p.cc && ./p >output2 3 4 1 5 x 7 6 8user@localhost:/tmp\$ hexdump -C output 00000000  00 75 6c 6c 64 64 72 75  72 64 6c 6c 75 72 64 72  |.ullddrurdllurdr|00000010  75 6c 64 72 0a                                    |uldr.|00000015user@localhost:/tmp\$`
mf
Guru

Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland

### Re: ACM PKU Eight Puzzle

What a silly bug..
Just changed
Code: Select all
`if (x != -2)   {      trace(P[x]);      printf("%c", M[x]);   }`

to
Code: Select all
`if (P[x] != -2)   {      trace(P[x]);      printf("%c", M[x]);   }`

and got AC!!!!!!!
Thanks mf!!

fushar
New poster

Posts: 26
Joined: Fri Apr 03, 2009 12:09 pm
Location: Indonesia

### Re: ACM PKU Eight Puzzle

mf wrote:Like this:
Code: Select all
`int fact[10];  // a table of factorialsint encode(int a[9]) {   int c[10], i, k, r;   for (i = 0; i < 9; i++) c[i] = i;   for (r = 0, k = 0; k < 9; k++) {      r += c[a[k]] * fact[8 - k];      for (i = a[k]; i < 9; i++) c[i]--;   }   return r + 1;}void decode(int a[9], int r) {   int c[10], i, k;   for (i = 0; i < 9; i++) c[i] = i;   for (r--, k = 0; k < 9; k++) {      i = r / fact[8 - k];      r %= fact[8 - k];      a[k] = c[i];      for (; i < 9; i++) c[i] = c[i + 1];   }}`

is this a Known Algorithm? can you explain more why to code like this ?:)
Last edited by Angeh on Wed Sep 02, 2009 12:17 am, edited 1 time in total.
>>>>>>>>> A2
Beliefs are not facts, believe what you need to believe;)
Angeh
Experienced poster

Posts: 108
Joined: Sat Aug 08, 2009 2:53 pm

### Re: ACM PKU Eight Puzzle

It's just a simple algorithm to convert a permutation into its index (from 0 to 9!-1) and vice versa.

fushar
New poster

Posts: 26
Joined: Fri Apr 03, 2009 12:09 pm
Location: Indonesia

Next