Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function allCellsDistOrder(rows: number, cols: number, rCenter: number, cCenter: number): number[][] {
- const getKey = ([x, y]: [number, number]) => `${x}_${y}`;
- const inRange = ([x, y]: [number, number]) => x < rows && y < cols && x >= 0 && y >= 0;
- const getNeighbors = ([x, y]: [number, number]): [number, number][] => ([ [x+1, y], [x-1, y], [x, y+1], [x, y-1] ]);
- // add at the end, iterate from the front, which means we'll end up in order
- const all: [number, number][] = [ [rCenter, cCenter ] ];
- const seen = new Set<string>([getKey(all[0])]);
- for(let i=0; i<all.length; i++) {
- for(const n of getNeighbors(all[i])) {
- if (!inRange(n)) continue;
- const key = getKey(n);
- if (seen.has(key)) continue;
- seen.add(key);
- all.push(n);
- }
- }
- return all;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement