@@ -1752,6 +1752,100 @@ public static <T> List<List<T>> createPermutationWithRepetition(final List<T> li
17521752 return result ;
17531753 }
17541754
1755+ public static class Status {
1756+ private final int key ;
1757+ private final int x ;
1758+ private final int y ;
1759+ public Status (int key , int x , int y ) {
1760+ this .key = key ;
1761+ this .x = x ;
1762+ this .y = y ;
1763+ }
1764+ public int getKey () {
1765+ return key ;
1766+ }
1767+ public int getX () {
1768+ return x ;
1769+ }
1770+ public int getY () {
1771+ return y ;
1772+ }
1773+ }
1774+
1775+ public static List <Status > shortestPathAllKeys (String [] grid ) {
1776+ int success = 0 ;
1777+ int startI = 0 ;
1778+ int startJ = 0 ;
1779+ int rows = grid .length ;
1780+ int cols = grid [0 ].length ();
1781+ List <Status > result = newArrayList ();
1782+ for (int i = 0 ; i < rows ; i ++) {
1783+ for (int j = 0 ; j < cols ; j ++) {
1784+ char c = grid [i ].charAt (j );
1785+ if (c >= 'A' && c <= 'F' ) {
1786+ success |= 1 << (c - 'A' );
1787+ }
1788+ if (c == '@' ) {
1789+ startI = i ;
1790+ startJ = j ;
1791+ }
1792+ }
1793+ }
1794+ int [][][] dist = new int [1 << 6 ][rows ][cols ];
1795+ for (int i = 0 ; i < dist .length ; i ++) {
1796+ for (int j = 0 ; j < dist [0 ].length ; j ++) {
1797+ for (int k = 0 ; k < dist [0 ][0 ].length ; k ++) {
1798+ dist [i ][j ][k ] = Integer .MAX_VALUE ;
1799+ }
1800+ }
1801+ }
1802+ Queue <Status > queue = new LinkedList <Status >();
1803+ queue .offer (new Status (0 , startI , startJ ));
1804+ dist [0 ][startI ][startJ ] = 0 ;
1805+ return scanDirs (queue , success , result , rows , cols , grid , dist );
1806+ }
1807+
1808+ private static List <Status > scanDirs (Queue <Status > queue , int success , List <Status > result ,
1809+ int rows , int cols , String [] grid , int [][][] dist ) {
1810+ int path = 0 ;
1811+ int [][] dirs = {{-1 , 0 }, {0 , 1 }, {1 , 0 }, {0 , -1 }};
1812+ while (!queue .isEmpty ()) {
1813+ int size = queue .size ();
1814+ while (size -- > 0 ) {
1815+ Status status = queue .poll ();
1816+ int key = status .getKey ();
1817+ int x = status .getX ();
1818+ int y = status .getY ();
1819+ if (key == success ) {
1820+ return result ;
1821+ }
1822+ for (int [] dir : dirs ) {
1823+ int xx = x + dir [0 ];
1824+ int yy = y + dir [1 ];
1825+ if (xx >= 0 && xx < rows && yy >= 0 && yy < cols && grid [xx ].charAt (yy ) != '#' ) {
1826+ int nextKey = key ;
1827+ char c = grid [xx ].charAt (yy );
1828+ if (c >= 'a' && c <= 'f' ) {
1829+ nextKey = key | (1 << (c - 'a' ));
1830+ }
1831+ if (c >= 'A' && c <= 'F' && (nextKey & (1 << (c - 'A' ))) == 0 ) {
1832+ continue ;
1833+ }
1834+ if (path + 1 < dist [nextKey ][xx ][yy ]) {
1835+ dist [nextKey ][xx ][yy ] = path + 1 ;
1836+ queue .offer (new Status (nextKey , xx , yy ));
1837+ }
1838+ }
1839+ }
1840+ }
1841+ path ++;
1842+ if (!queue .isEmpty ()) {
1843+ result .add (queue .element ());
1844+ }
1845+ }
1846+ return result ;
1847+ }
1848+
17551849 @ SuppressWarnings ("unchecked" )
17561850 public List <List <T >> createPermutationWithRepetition (final int permutationLength ) {
17571851 return createPermutationWithRepetition ((List <T >) value (), permutationLength );
0 commit comments