## Monday, August 8, 2016

Given a point in k dimension vector, find all adjacent points.
For example:
(0, 0) is a point in 2-dimension, then the adjacent points are
[(0, -1), (0, 1), (1, -1), (1, 0), (1, 1), (-1, -1), (-1, 0), (-1, 1)]
[Solution]

`  ``public` `List<List<Integer>> adjacentPoints(List<Integer> point) {`
`    ``List<List<Integer>> result = ``new` `LinkedList<>();`
`    ``if` `(point == ``null` `|| point.isEmpty()) {`
`      ``return` `result;`
`    ``}`

`    ``List<Integer> tmp = ``new` `ArrayList<>(point);`
`    ``dfs(point, tmp, ``0``, result);`
`    ``return` `result;`
`  ``}`

`  ``private` `boolean` `isSame(List<Integer> a, List<Integer> b) {`
`    ``if` `(a.size() != b.size()) {`
`      ``return` `false``;`
`    ``}`
`    ``for` `(``int` `i = ``0``; i < a.size(); i++) {`
`      ``if` `(a.get(i) != b.get(i)) {`
`        ``return` `false``;`
`      ``}`
`    ``}`
`    ``return` `true``;`
`  ``}`

`  ``private` `void` `dfs(List<Integer> point, List<Integer> tmp, ``int` `pos, List<List<Integer>> result) {`
`    ``if` `(pos == tmp.size()) {`
`      ``if` `(!isSame(point, tmp)) {`
`        ``result.add(``new` `ArrayList<>(tmp));`
`      ``}`
`      ``return``;`
`    ``}`

`    ``// 0`
`    ``dfs(point, tmp, pos + ``1``, result);`

`    ``int` `save = tmp.get(pos);`

`    ``// -1`
`    ``tmp.set(pos, save - ``1``);`
`    ``dfs(point, tmp, pos + ``1``, result);`

`    ``// +1`
`    ``tmp.set(pos, save + ``1``);`
`    ``dfs(point, tmp, pos + ``1``, result);`

`    ``// recover`
`    ``tmp.set(pos, save);`
`  ``}`