Skip to content

Commit d75130b

Browse files
committed
Add U.shortestPathAllKeys(string[]).
1 parent 4f4e768 commit d75130b

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ matrix:
1515
- ln -s /etc/ssl/certs/java/cacerts "${JAVA_HOME}/lib/security/cacerts"
1616

1717
script:
18-
- mvn clean install jacoco:report coveralls:report
18+
- mvn clean install jacoco:report
1919
- mvn -f examples/pom.xml clean package
2020

2121
addons:

src/main/java/com/github/underscore/lodash/U.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/test/java/com/github/underscore/lodash/MathTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ public void createPermutationWithRepetition() {
244244
+ " [orange, orange, orange]]", resultChain.toString());
245245
}
246246

247+
@SuppressWarnings("unchecked")
248+
@Test
249+
public void shortestPathAllKeys() {
250+
List<U.Status> statuses = U.shortestPathAllKeys(new String[] {"@.a.#", "###.#", "b.A.B"});
251+
assertEquals(8, statuses.size());
252+
assertEquals(0, statuses.get(0).getX());
253+
assertEquals(1, statuses.get(0).getY());
254+
assertEquals(2, statuses.get(7).getX());
255+
assertEquals(0, statuses.get(7).getY());
256+
assertEquals(6, U.shortestPathAllKeys(new String[] {"@..aA", "..B#.", "....b"}).size());
257+
assertEquals(5, U.shortestPathAllKeys(new String[] {"g...#", "###.#", "..A.B"}).size());
258+
assertEquals(0, U.shortestPathAllKeys(new String[] {"#####", "#####", "#####"}).size());
259+
}
260+
247261
@SuppressWarnings("unchecked")
248262
@Test
249263
public void main() {

0 commit comments

Comments
 (0)