### 10913 - Walking on a Grid

Posted:

**Sat Sep 24, 2005 5:13 am**dp?

Page **1** of **2**

Posted: **Sat Sep 24, 2005 5:13 am**

dp?

Posted: **Sat Sep 24, 2005 5:21 am**

yes, it's DP

time complexity is about o(k * n^2)

Hint :

think when there is no rule that :

You can step on at most k negative integers from source to destination.

then you'll get DP algorithm,

and add this rule to your algorithm.

it will be not difficult.

time complexity is about o(k * n^2)

Hint :

think when there is no rule that :

You can step on at most k negative integers from source to destination.

then you'll get DP algorithm,

and add this rule to your algorithm.

it will be not difficult.

Posted: **Sat Sep 24, 2005 5:13 pm**

ac now

Thank you

Thank you

Posted: **Sun Sep 25, 2005 2:40 pm**

Crazy autologin

Previous post is mine.

Previous post is mine.

Posted: **Sun Sep 25, 2005 10:58 pm**

My time complexity is O(kn^2), too. And the memory complexity is O(kn).Anonymous wrote:Are you sure about o(k * n^2) ?wook wrote:yes, it's DP

time complexity is about o(k * n^2)

My algo takes o(k * n^3). And work fast enough.

Posted: **Mon Sep 26, 2005 1:05 am**

Thx, I optimized my algo. Now it's O(k * n^2) too.

However running time didn't improved much.

However running time didn't improved much.

Posted: **Mon Sep 26, 2005 2:16 am**

It would have been more significiant if n would be much bigger than 75.kp wrote:Thx, I optimized my algo. Now it's O(k * n^2) too.

However running time didn't improved much.

Posted: **Tue Oct 04, 2005 10:14 am**

Can someone post some example cases? Thanks!

Posted: **Sat Dec 10, 2005 10:43 pm**

I'm lazy to generate any inputs , but if you post some here I can generate the correct answers for you.Larry wrote:Can someone post some example cases? Thanks!

Posted: **Tue Jan 03, 2006 8:52 pm**

hi,

i cannot make a way to use DP in this problem. if you anyone describe how to implement DP here, i will be greatly helped. btw, can this problem be solved within time limit using dfs?

thanks in advance...

i cannot make a way to use DP in this problem. if you anyone describe how to implement DP here, i will be greatly helped. btw, can this problem be solved within time limit using dfs?

thanks in advance...

Posted: **Wed Jan 04, 2006 11:57 am**

I used following DP-algorithm:ayon wrote:hi,

i cannot make a way to use DP in this problem. if you anyone describe how to implement DP here, i will be greatly helped. btw, can this problem be solved within time limit using dfs?

thanks in advance...

In cell t[i,j,g] save the maximum sum of integers of the path to cell(i,j), g is a count negative integer in the sum.

1.) As we start at cell (1,1), in all cells of first row we can arrive from left only. So, t[1,i,g] = t[1,i-1,g]+m[1,i] if m[1,i] >= 0 else t[1,i,g+1] = t[1,i-1,g]+m[1,i].

2.) Then for all row from 2 to n do:

- move down from row (i-1) to row i

if m[i,j] >= 0 then t[i,j,g] = t[i-1,j,g]+m[i,j]

else t[i,j,g] = t[i-1,j,g-1]+m[i,j]

- buf1[j,g] = buf2[j,g] = t[i,j,g]

- move from cell(i,1) to cell (i,n) try to maximize the result in buf1:

if (m[i,j] >= 0) then buf1[j,g] = max(buf1[j,g],buf1[j-1,g]+m[i,j]) else buf1[j,g] = max(buf1[j,g],buf1[j-1,g-1]+m[i,j])

- also move from cell(i,n) to cell (1,n) correct buf2

- t[i,j,g] = max(t[i,j,g],buf1[j,g],buf2[j,g]);

3.) the maximum sum of integers of the path is max(t[n,n,g]) where 0<=g<=k.

Posted: **Wed Jan 04, 2006 2:34 pm**

thank you very much Andrey, now i understand clearly how to use DP here. your explaination is very clear and fine. thanks again for the help

Posted: **Wed Jan 04, 2006 10:18 pm**

Someone pls send some critical data, i m getting WA again and again. i use DP.

Thx in Advace.

L I M O N

Thx in Advace.

L I M O N

Posted: **Wed Jan 04, 2006 11:31 pm**

Try this input:L I M O N wrote:Someone pls send some critical data, i m getting WA again and again. i use DP.

Thx in Advace.

L I M O N

Code: Select all

```
1 0
1
1 0
-1
1 1
-1
3 0
1 1 1
1 1 1
1 1 1
4 0
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
4 1
1 -1 1 1
-1 1 1 1
10 -1 5 1
1 1 1 1
3 5
-1 -1 -1
-1 -1 -1
-1 -1 -1
3 4
-1 -1 -1
-1 -1 -1
-1 -1 -1
5 2
2 -1 10 3 13
5 -4 3 -2 1
-100 2 3 43 17
24 92 40 14 40
100 100 -1 -1 1
0 0
```

Code: Select all

```
Case 1: 1
Case 2: impossible
Case 3: -1
Case 4: 9
Case 5: 13
Case 6: 14
Case 7: -5
Case 8: impossible
Case 9: 280
```

Posted: **Wed Jan 04, 2006 11:45 pm**

my outputs :

Case 1: 1

Case 2: impossible

Case 3: -1

Case 4: 9

Case 5: 13

Case 6: 14

Case 7: -5

Case 8: impossible

Case 9: 280

but Same result, WA.

do u use "long long" data type ? i also use this type.

pls send more data

L I M O N

Case 1: 1

Case 2: impossible

Case 3: -1

Case 4: 9

Case 5: 13

Case 6: 14

Case 7: -5

Case 8: impossible

Case 9: 280

but Same result, WA.

do u use "long long" data type ? i also use this type.

pls send more data

L I M O N