@@ -1926,100 +1926,6 @@ public static <T> List<List<T>> createPermutationWithRepetition(final List<T> li
19261926 return result ;
19271927 }
19281928
1929- public static class Status {
1930- private final int key ;
1931- private final int x ;
1932- private final int y ;
1933- public Status (int key , int x , int y ) {
1934- this .key = key ;
1935- this .x = x ;
1936- this .y = y ;
1937- }
1938- public int getKey () {
1939- return key ;
1940- }
1941- public int getX () {
1942- return x ;
1943- }
1944- public int getY () {
1945- return y ;
1946- }
1947- }
1948-
1949- public static List <Status > shortestPathAllKeys (String [] grid ) {
1950- int success = 0 ;
1951- int startI = 0 ;
1952- int startJ = 0 ;
1953- int rows = grid .length ;
1954- int cols = grid [0 ].length ();
1955- List <Status > result = newArrayList ();
1956- for (int i = 0 ; i < rows ; i ++) {
1957- for (int j = 0 ; j < cols ; j ++) {
1958- char c = grid [i ].charAt (j );
1959- if (c >= 'A' && c <= 'F' ) {
1960- success |= 1 << (c - 'A' );
1961- }
1962- if (c == '@' ) {
1963- startI = i ;
1964- startJ = j ;
1965- }
1966- }
1967- }
1968- int [][][] dist = new int [1 << 6 ][rows ][cols ];
1969- for (int i = 0 ; i < dist .length ; i ++) {
1970- for (int j = 0 ; j < dist [0 ].length ; j ++) {
1971- for (int k = 0 ; k < dist [0 ][0 ].length ; k ++) {
1972- dist [i ][j ][k ] = Integer .MAX_VALUE ;
1973- }
1974- }
1975- }
1976- Queue <Status > queue = new LinkedList <Status >();
1977- queue .offer (new Status (0 , startI , startJ ));
1978- dist [0 ][startI ][startJ ] = 0 ;
1979- return scanDirs (queue , success , result , rows , cols , grid , dist );
1980- }
1981-
1982- private static List <Status > scanDirs (Queue <Status > queue , int success , List <Status > result ,
1983- int rows , int cols , String [] grid , int [][][] dist ) {
1984- int path = 0 ;
1985- int [][] dirs = {{-1 , 0 }, {0 , 1 }, {1 , 0 }, {0 , -1 }};
1986- while (!queue .isEmpty ()) {
1987- int size = queue .size ();
1988- while (size -- > 0 ) {
1989- Status status = queue .poll ();
1990- int key = status .getKey ();
1991- int x = status .getX ();
1992- int y = status .getY ();
1993- if (key == success ) {
1994- return result ;
1995- }
1996- for (int [] dir : dirs ) {
1997- int xx = x + dir [0 ];
1998- int yy = y + dir [1 ];
1999- if (xx >= 0 && xx < rows && yy >= 0 && yy < cols && grid [xx ].charAt (yy ) != '#' ) {
2000- int nextKey = key ;
2001- char c = grid [xx ].charAt (yy );
2002- if (c >= 'a' && c <= 'f' ) {
2003- nextKey = key | (1 << (c - 'a' ));
2004- }
2005- if (c >= 'A' && c <= 'F' && (nextKey & (1 << (c - 'A' ))) == 0 ) {
2006- continue ;
2007- }
2008- if (path + 1 < dist [nextKey ][xx ][yy ]) {
2009- dist [nextKey ][xx ][yy ] = path + 1 ;
2010- queue .offer (new Status (nextKey , xx , yy ));
2011- }
2012- }
2013- }
2014- }
2015- path ++;
2016- if (!queue .isEmpty ()) {
2017- result .add (queue .element ());
2018- }
2019- }
2020- return result ;
2021- }
2022-
20231929 public List <List <T >> createPermutationWithRepetition (final int permutationLength ) {
20241930 return createPermutationWithRepetition ((List <T >) value (), permutationLength );
20251931 }
0 commit comments