QTNOEL - Christmas of a school couple
Christmas is coming and DB want to give TN a wonderful gift. But he don’t have much money, so he asks his brother for help. His brother accepts his request, but he’s very like quizs, so he asks DB a question :
DB and TN’s school has n classroom rows, each of them has m classrooms. The j-th classroom of i-th row called (i, j) room. DB’s brother has put a rose on each room, and DB’s task is collect them to give TN. DB start from (a, b) room, go over classrooms to TN at (x, y) room (1 ≤ a, x ≤ n, 1 ≤ b, y ≤ m).
In each class, there are one door to come in (in – door) and one to come out (out – door) from next room. The in – door has two lock with the depth is aij – 1 and aij + 1. And there is a key in a room with the length is aij. The key has length L can unlock a lock with depth L. You only need to open one of two lock in a door, and the other will open automatically. But you can only use a key one time!
Of course, DB want to give TN maximum of roses at possible, so he will find a best path. But he can’t find this path because the school is too big, so he want you to help him!
Input
First line : two positive integer n, m (n, m ≤ 1000)
Next n lines : each line has m number aij (|aij |≤ 1000)
Output
First line : k – the maximum number of roses that DB can collect
Next k lines : each line has two number (u, v), is the best path. Of course first line is (a, b) and last line is (x, y)
If there are more than one result, print any of them
Example
Input:3 3
1 5 3
2 3 2
3 2 1
Output:7
1 1
2 1
2 2
2 3
3 3
3 2
3 1
hide comments
Mitch Schwartz:
2014-12-02 06:17:25
Unless I've misunderstood something, this problem is NP-hard as a consequence of the NP-completeness theorem in "Hamilton Paths in Grid Graphs" by Itai, Papadimitriou, and Szwarcfiter (1982). We can construct grid graphs by e.g. labeling included vertices 1 or 2 according to parity, and excluded vertices 4 or 6 according to parity. The problem is hidden. Last edit: 2014-01-07 19:29:12 |
|
Jacob Plachta:
2014-12-02 06:17:25
Hmm, this certainly seems to be the Longest Path problem, which is NP-hard... |
Added by: | Coder nhà mình |
Date: | 2013-12-23 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |