public class MovingCandies {
int h, w;
boolean [ ] [ ] [ ] [ ] vis;
void dfs( int row, int col, int len, int ones) {
if ( ! order( 0 , row, h - 1 ) || ! order( 0 , col, w - 1 ) )
return ;
++ len;
if ( input[ row] .charAt ( col) == '#' )
++ ones;
if ( len > h * w || vis[ row] [ col] [ len] [ ones] )
return ;
vis[ row] [ col] [ len] [ ones] = true ;
dfs( row + 1 , col, len, ones) ;
dfs( row, col + 1 , len, ones) ;
dfs( row - 1 , col, len, ones) ;
dfs( row, col - 1 , len, ones) ;
}
public int minMoved
( String [ ] t
) { input = t;
h = t.length ;
w = t[ 0 ] .length ( ) ;
vis = new boolean [ h] [ w] [ h* w+ 1 ] [ h* w+ 1 ] ;
dfs( 0 , 0 , 0 , 0 ) ;
int allowed = 0 ; // the total number of ones in the grid
for ( int i = 0 ; i < row.length ( ) ; ++ i)
if ( row.charAt ( i) == '#' )
++ allowed;
final int INF = 1000123 ;
int ans = INF;
for ( int len = 0 ; len <= allowed; ++ len)
for ( int ones = 0 ; ones <= h * w; ++ ones)
if ( vis[ h- 1 ] [ w- 1 ] [ len] [ ones] )
ans = min( ans, len - ones) ;
if ( ans == INF)
ans = - 1 ;
return ans;
}
}
cHVibGljIGNsYXNzIE1vdmluZ0NhbmRpZXMgewoJaW50IGgsIHc7CglTdHJpbmdbXSBpbnB1dDsKCWJvb2xlYW5bXVtdW11bXSB2aXM7Cgl2b2lkIGRmcyhpbnQgcm93LCBpbnQgY29sLCBpbnQgbGVuLCBpbnQgb25lcykgewoJCWlmKCFvcmRlcigwLCByb3csIGggLSAxKSB8fCAhb3JkZXIoMCwgY29sLCB3IC0gMSkpCgkJCXJldHVybjsKCQkrK2xlbjsKCQlpZihpbnB1dFtyb3ddLmNoYXJBdChjb2wpID09ICcjJykKCQkJKytvbmVzOwoJCWlmKGxlbiA+IGggKiB3IHx8IHZpc1tyb3ddW2NvbF1bbGVuXVtvbmVzXSkKCQkJcmV0dXJuOwoJCXZpc1tyb3ddW2NvbF1bbGVuXVtvbmVzXSA9IHRydWU7CgkJZGZzKHJvdyArIDEsIGNvbCwgbGVuLCBvbmVzKTsKCQlkZnMocm93LCBjb2wgKyAxLCBsZW4sIG9uZXMpOwoJCWRmcyhyb3cgLSAxLCBjb2wsIGxlbiwgb25lcyk7CgkJZGZzKHJvdywgY29sIC0gMSwgbGVuLCBvbmVzKTsKCX0KCXB1YmxpYyBpbnQgbWluTW92ZWQoU3RyaW5nW10gdCkgewoJCWlucHV0ID0gdDsKCQloID0gdC5sZW5ndGg7CgkJdyA9IHRbMF0ubGVuZ3RoKCk7CgkJdmlzID0gbmV3IGJvb2xlYW5baF1bd11baCp3KzFdW2gqdysxXTsKCQlkZnMoMCwgMCwgMCwgMCk7CgkJaW50IGFsbG93ZWQgPSAwOyAvLyB0aGUgdG90YWwgbnVtYmVyIG9mIG9uZXMgaW4gdGhlIGdyaWQKCQlmb3IoU3RyaW5nIHJvdyA6IHQpCgkJCWZvcihpbnQgaSA9IDA7IGkgPCByb3cubGVuZ3RoKCk7ICsraSkKCQkJCWlmKHJvdy5jaGFyQXQoaSkgPT0gJyMnKQoJCQkJCSsrYWxsb3dlZDsKCQlTeXN0ZW0ub3V0LnByaW50bG4oYWxsb3dlZCk7CgkJZmluYWwgaW50IElORiA9IDEwMDAxMjM7CgkJaW50IGFucyA9IElORjsKCQlmb3IoaW50IGxlbiA9IDA7IGxlbiA8PSBhbGxvd2VkOyArK2xlbikKCQkJZm9yKGludCBvbmVzID0gMDsgb25lcyA8PSBoICogdzsgKytvbmVzKQoJCQkJaWYodmlzW2gtMV1bdy0xXVtsZW5dW29uZXNdKQoJCQkJCWFucyA9IG1pbihhbnMsIGxlbiAtIG9uZXMpOwoJCWlmKGFucyA9PSBJTkYpCgkJCWFucyA9IC0xOwoJCXJldHVybiBhbnM7Cgl9Cn0K
compilation info
Main.java:1: error: class MovingCandies is public, should be declared in a file named MovingCandies.java
public class MovingCandies {
^
Main.java:6: error: cannot find symbol
if(!order(0, row, h - 1) || !order(0, col, w - 1))
^
symbol: method order(int,int,int)
location: class MovingCandies
Main.java:6: error: cannot find symbol
if(!order(0, row, h - 1) || !order(0, col, w - 1))
^
symbol: method order(int,int,int)
location: class MovingCandies
Main.java:36: error: cannot find symbol
ans = min(ans, len - ones);
^
symbol: method min(int,int)
location: class MovingCandies
4 errors
stdout