
10314 - Three Pigs
Moderator: Board moderators
-
- Learning poster
- Posts: 60
- Joined: Tue Aug 13, 2002 2:39 am
- Location: Mexico
10314 time exceeded
i use dfs to search all possible combination of the graph....any way to reduce time without coding again?
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
Try this test case:Repeat it 3 times and every time update the map, what's wrong??
Code: Select all
1
8
1 0 0 1 0 0 0 1
1 2 0 2 1 2 2 0
1 2 1 2 1 2 2 0
1 2 0 0 1 1 1 1
1 2 0 2 2 2 2 1
1 2 1 0 0 0 0 1
1 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1
But correct solution is 28.
First step:
Code: Select all
x 0 0 1 0 0 0 1
x 2 0 2 1 2 2 0
x 2 1 2 1 2 2 0
x 2 0 0 1 1 1 1
x 2 0 2 2 2 2 1
x 2 1 0 0 0 0 1
x 2 2 2 2 2 2 1
x x x x x x x x
Second step:
Code: Select all
x x x x x x x x
0 2 0 2 1 2 2 x
0 2 1 2 1 2 2 x
0 2 x x x x x x
0 2 x 2 2 2 2 1
0 2 x x x x x x
0 2 2 2 2 2 2 x
0 0 0 0 0 0 0 x
Third step:
Code: Select all
x x x 0 x x x x
0 2 x 2 x 2 2 x
0 2 x 2 x 2 2 x
0 2 x x x 0 0 x
0 2 0 2 2 2 2 x
0 2 0 0 0 0 0 x
0 2 2 2 2 2 2 x
0 0 0 0 0 0 0 x
15 + 9 + 4 = 28
Re: 10314 - Three Pigs
I got TLE using DFS and backtracking.. I don't know how to optimize my code. How can I know if one path has been taken and so that another pigs will not follow the exactly same path ? some say using DP.. but I don't know how because I am bad at DP.. Can anyone give me hint ?