1616)
1717class Solution {
1818
19- private SimplePoint [][] grid ;
19+ private Point [][] grid ;
2020 private int rows , cols ;
2121
2222 private void reset (int [][] grid ) {
2323 rows = grid .length ;
2424 cols = grid [0 ].length ;
25- this .grid = new SimplePoint [rows ][cols ];
25+ this .grid = new Point [rows ][cols ];
2626 for (int row = 0 ; row < rows ; row ++) {
2727 for (int col = 0 ; col < cols ; col ++) {
28- this .grid [row ][col ] = new SimplePoint ( grid [row ][col ]);
28+ this .grid [row ][col ] = Point . of ( row , col , grid [row ][col ]);
2929 }
3030 }
3131 }
3232
3333 private int forward () {
34+
3435 for (int row = 1 ; row < rows && !grid [row ][0 ].isBlock (); row ++)
3536 grid [row ][0 ].addPrev (grid [row - 1 ][0 ]);
3637
@@ -43,12 +44,13 @@ private int forward() {
4344 if (grid [row ][col ].isBlock ())
4445 continue ;
4546
46- final SimplePoint up = grid [row - 1 ][col ];
47- final SimplePoint left = grid [row ][col - 1 ];
47+ final Point up = grid [row - 1 ][col ];
48+ final Point left = grid [row ][col - 1 ];
4849
4950 if (up .isBlock ()) {
50- if (!left .isBlock ())
51+ if (!left .isBlock ()) {
5152 grid [row ][col ].addPrev (left );
53+ }
5254 } else if (left .isBlock ()) {
5355 grid [row ][col ].addPrev (up );
5456 } else if (up .value () > left .value ()) {
@@ -59,53 +61,57 @@ private int forward() {
5961 }
6062 }
6163
62- SimplePoint point = grid [rows - 1 ][cols - 1 ];
63- int value = point .pathSum ();
64- point .resetAll ();
65- return value ;
64+ Point point = grid [rows - 1 ][cols - 1 ];
65+ if (point .isValid ()) {
66+ int value = point .pathSum ();
67+ point .erase ();
68+ return value ;
69+ }
70+
71+ return -1 ;
6672 }
6773
6874 private int backward () {
6975
70- for (int row = rows - 2 ; row >= 0 && !grid [row ][0 ].isBlock (); row --)
71- grid [row ][0 ].addPrev (grid [row + 1 ][0 ]);
76+ for (int row = rows - 2 ; row >= 0 && !grid [row ][cols - 1 ].isBlock (); row --)
77+ grid [row ][cols - 1 ].addPrev (grid [row + 1 ][cols - 1 ]);
7278
73- for (int col = cols - 2 ; col >= 0 && !grid [0 ][col ].isBlock (); col --)
74- grid [0 ][col ].addPrev (grid [0 ][col + 1 ]);
79+ for (int col = cols - 2 ; col >= 0 && !grid [rows - 1 ][col ].isBlock (); col --)
80+ grid [rows - 1 ][col ].addPrev (grid [rows - 1 ][col + 1 ]);
7581
7682 for (int row = rows - 2 ; row >= 0 ; row --) {
7783 for (int col = cols - 2 ; col >= 0 ; col --) {
78- if (grid [row ][col ].isBlock ())
84+ Point current = grid [row ][col ];
85+ current .reset ();
86+ if (current .isBlock ())
7987 continue ;
8088
81- SimplePoint right = grid [row ][col + 1 ];
82- SimplePoint down = grid [row + 1 ][col ];
89+ Point right = grid [row ][col + 1 ];
90+ Point down = grid [row + 1 ][col ];
8391 if (right .isBlock ()) {
8492 if (!down .isBlock ())
85- grid [ row ][ col ] .addPrev (down );
93+ current .addPrev (down );
8694 } else if (down .isBlock ()) {
87- grid [ row ][ col ] .addPrev (right );
95+ current .addPrev (right );
8896 } else if (right .value () > down .value ()) {
89- grid [ row ][ col ] .addPrev (right );
97+ current .addPrev (right );
9098 } else {
91- grid [ row ][ col ] .addPrev (down );
99+ current .addPrev (down );
92100 }
93101 }
94102 }
95103
96- SimplePoint point = grid [0 ][0 ];
97- int value = point .pathSum ();
98- point .resetAll ();
99- return value ;
104+ return grid [0 ][0 ].pathSum ();
100105 }
101106
102107 public int cherryPickup (int [][] intsGrid ) {
103108 reset (intsGrid );
104109
105- int forward = forward ();
106- int backward = backward ();
110+ final int forward = forward ();
111+ if (forward == -1 )
112+ return 0 ;
107113
108- return forward + backward ;
114+ return forward + backward () ;
109115 }
110116
111117}
0 commit comments