Skip to content

Commit 001d912

Browse files
author
liwentian
committed
fd
1 parent 7106274 commit 001d912

File tree

7 files changed

+145
-12
lines changed

7 files changed

+145
-12
lines changed

ebook/graph/Graph.aux

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@
6666
\@writefile{toc}{\contentsline {subsubsection}{Description}{34}{section*.43}}
6767
\@writefile{toc}{\contentsline {subsubsection}{Solution I}{35}{section*.44}}
6868
\@writefile{toc}{\contentsline {subsubsection}{Solution II}{36}{section*.45}}
69+
\@writefile{toc}{\contentsline {section}{\numberline {1.19}Shortest Distance from All Buildings}{37}{section.1.19}}
70+
\@writefile{toc}{\contentsline {subsubsection}{Description}{37}{section*.46}}
71+
\@writefile{toc}{\contentsline {subsubsection}{Analysis}{37}{section*.47}}
72+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{38}{section*.48}}
73+
\FN@pp@footnotehinttrue
6974
\@setckpt{Graph}{
70-
\setcounter{page}{37}
75+
\setcounter{page}{40}
7176
\setcounter{equation}{0}
7277
\setcounter{enumi}{0}
7378
\setcounter{enumii}{0}
@@ -77,20 +82,20 @@
7782
\setcounter{mpfootnote}{0}
7883
\setcounter{part}{0}
7984
\setcounter{chapter}{1}
80-
\setcounter{section}{18}
85+
\setcounter{section}{19}
8186
\setcounter{subsection}{0}
8287
\setcounter{subsubsection}{0}
8388
\setcounter{paragraph}{0}
8489
\setcounter{subparagraph}{0}
8590
\setcounter{figure}{0}
8691
\setcounter{table}{0}
87-
\setcounter{FancyVerbLine}{50}
92+
\setcounter{FancyVerbLine}{39}
8893
\setcounter{pp@next@reset}{1}
8994
\setcounter{@fnserial}{0}
9095
\setcounter{Item}{0}
9196
\setcounter{Hfootnote}{0}
9297
\setcounter{Hy@AnnotLevel}{0}
93-
\setcounter{bookmark@seq@number}{19}
98+
\setcounter{bookmark@seq@number}{20}
9499
\setcounter{parentequation}{0}
95100
\setcounter{section@level}{3}
96101
}

ebook/graph/Graph.tex

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,4 +1689,127 @@ \subsubsection{Solution II}
16891689
}
16901690
\end{Code}
16911691

1692-
\newpage
1692+
\newpage
1693+
1694+
\section{Shortest Distance from All Buildings} %%%%%%%%%%%%%%%%%%%%%%
1695+
1696+
\subsubsection{Description}
1697+
1698+
You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where:
1699+
1700+
Each 0 marks an empty land which you can pass by freely.
1701+
1702+
Each 1 marks a building which you cannot pass through.
1703+
1704+
Each 2 marks an obstacle which you cannot pass through.
1705+
1706+
For example, given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2):
1707+
1708+
\begin{Code}
1709+
1 - 0 - 2 - 0 - 1
1710+
| | | | |
1711+
0 - 0 - 0 - 0 - 0
1712+
| | | | |
1713+
0 - 0 - 1 - 0 - 0
1714+
\end{Code}
1715+
1716+
The point (1,2) is an ideal empty land to build a house, as the total travel distance of 3+3+1=7 is minimal. So return 7.
1717+
1718+
\textbf{Note:}
1719+
1720+
There will be at least one building. If it is not possible to build such house according to the above rules, return -1.
1721+
1722+
\subsubsection{Analysis}
1723+
1724+
这道题思路是以所有建筑为根开始BFS,对所有覆盖到的点计算距离,
1725+
1726+
每个空白点可能会同时被好几个建筑覆盖,所以其距离是叠加的,表示该点到那几个联通建筑的距离之和
1727+
1728+
最后遍历所有空白点,求距离和最小的,同时能联通所有建筑的
1729+
1730+
\newpage
1731+
1732+
\subsubsection{Solution}
1733+
1734+
\begin{Code}
1735+
/**
1736+
* 1, 注意当没有结果时返回-1,而不是INT_MAX
1737+
* 2, 要保证参考点能通往所有建筑,所以要统计建筑数
1738+
* 3, bfs时要注意边界且别重复访问了
1739+
*/
1740+
public int shortestDistance(int[][] grid) {
1741+
if (grid.length == 0) {
1742+
return -1;
1743+
}
1744+
int row = grid.length, col = grid[0].length;
1745+
1746+
int[][] dis = new int[row][col];
1747+
int[][] building = new int[row][col];
1748+
int buildings = 0;
1749+
1750+
for (int i = 0; i < row; i++) {
1751+
for (int j = 0; j < col; j++) {
1752+
if (grid[i][j] != 1) {
1753+
continue;
1754+
}
1755+
1756+
buildings++;
1757+
bfs(grid, dis, building, i, j);
1758+
}
1759+
}
1760+
1761+
int shortest = Integer.MAX_VALUE;
1762+
for (int i = 0; i < row; i++) {
1763+
for (int j = 0; j < col; j++) {
1764+
if (grid[i][j] == 0 && building[i][j] == buildings) {
1765+
shortest = Math.min(shortest, dis[i][j]);
1766+
}
1767+
}
1768+
}
1769+
return shortest == Integer.MAX_VALUE ? -1 : shortest;
1770+
}
1771+
\end{Code}
1772+
1773+
\newpage
1774+
1775+
\begin{Code}
1776+
private void bfs(int[][] grid, int[][] dis, int[][] building, int i, int j) {
1777+
Queue<int[]> queue = new LinkedList<>();
1778+
queue.add(new int[] {i, j});
1779+
1780+
Queue<int[]> next = new LinkedList<>();
1781+
1782+
boolean[][] visited = new boolean[grid.length][grid[0].length];
1783+
1784+
int level = 0;
1785+
1786+
while (!queue.isEmpty()) {
1787+
int[] pos = queue.poll();
1788+
int x0 = pos[0], y0 = pos[1];
1789+
1790+
dis[x0][y0] += level;
1791+
building[x0][y0]++;
1792+
1793+
int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
1794+
1795+
for (int m = 0; m < dx.length; m++) {
1796+
int x = x0 + dx[m], y = y0 + dy[m];
1797+
1798+
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) {
1799+
continue;
1800+
}
1801+
1802+
if (grid[x][y] == 0 && !visited[x][y]) {
1803+
visited[x][y] = true;
1804+
next.offer(new int[]{x, y});
1805+
}
1806+
}
1807+
1808+
if (queue.isEmpty()) {
1809+
queue.addAll(next);
1810+
next.clear();
1811+
level++;
1812+
}
1813+
}
1814+
}
1815+
\end{Code}

ebook/graph/leetcode-graph.log

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 7 SEP 2017 22:12
1+
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 7 SEP 2017 22:17
22
entering extended mode
33
restricted \write18 enabled.
44
%&-line parsing enabled.
@@ -1929,7 +1929,7 @@ up above has ended a previous alignment prematurely,
19291929
you're probably due for more error messages, and you
19301930
might try typing `S' now just to see what is salvageable.
19311931

1932-
[32] [33] [34] [35] [36])
1932+
[32] [33] [34] [35] [36] [37] [38]) [39]
19331933
No file leetcode-graph.ind.
19341934
Package atveryend Info: Empty hook `BeforeClearDocument' on input line 44.
19351935
Package atveryend Info: Empty hook `AfterLastShipout' on input line 44.
@@ -1939,12 +1939,12 @@ Package atveryend Info: Empty hook `AtEndAfterFileList' on input line 44.
19391939
Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 44.
19401940
)
19411941
Here is how much of TeX's memory you used:
1942-
29194 strings out of 493591
1943-
554153 string characters out of 6143547
1944-
576144 words of memory out of 5000000
1945-
32225 multiletter control sequences out of 15000+600000
1942+
29204 strings out of 493591
1943+
554870 string characters out of 6143547
1944+
579531 words of memory out of 5000000
1945+
32235 multiletter control sequences out of 15000+600000
19461946
5554 words of font info for 58 fonts, out of 8000000 for 9000
19471947
1347 hyphenation exceptions out of 8191
19481948
65i,11n,77p,10420b,474s stack positions out of 5000i,500n,10000p,200000b,80000s
19491949

1950-
Output written on leetcode-graph.pdf (38 pages).
1950+
Output written on leetcode-graph.pdf (41 pages).

ebook/graph/leetcode-graph.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
\BOOKMARK [1][-]{section.1.16}{\376\377\0001\000.\0001\0006\000\040\000M\000i\000n\000i\000m\000u\000m\000\040\000H\000e\000i\000g\000h\000t\000\040\000T\000r\000e\000e\000s}{chapter.1}% 17
1818
\BOOKMARK [1][-]{section.1.17}{\376\377\0001\000.\0001\0007\000\040\000E\000v\000a\000l\000u\000a\000t\000e\000\040\000D\000i\000v\000i\000s\000i\000o\000n}{chapter.1}% 18
1919
\BOOKMARK [1][-]{section.1.18}{\376\377\0001\000.\0001\0008\000\040\000R\000e\000m\000o\000v\000e\000\040\000I\000n\000v\000a\000l\000i\000d\000\040\000P\000a\000r\000e\000n\000t\000h\000e\000s\000e\000s}{chapter.1}% 19
20+
\BOOKMARK [1][-]{section.1.19}{\376\377\0001\000.\0001\0009\000\040\000S\000h\000o\000r\000t\000e\000s\000t\000\040\000D\000i\000s\000t\000a\000n\000c\000e\000\040\000f\000r\000o\000m\000\040\000A\000l\000l\000\040\000B\000u\000i\000l\000d\000i\000n\000g\000s}{chapter.1}% 20

ebook/graph/leetcode-graph.pdf

20.8 KB
Binary file not shown.
9.01 KB
Binary file not shown.

ebook/graph/leetcode-graph.toc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@
6161
\contentsline {subsubsection}{Description}{34}{section*.43}
6262
\contentsline {subsubsection}{Solution I}{35}{section*.44}
6363
\contentsline {subsubsection}{Solution II}{36}{section*.45}
64+
\contentsline {section}{\numberline {1.19}Shortest Distance from All Buildings}{37}{section.1.19}
65+
\contentsline {subsubsection}{Description}{37}{section*.46}
66+
\contentsline {subsubsection}{Analysis}{37}{section*.47}
67+
\contentsline {subsubsection}{Solution}{38}{section*.48}

0 commit comments

Comments
 (0)