Created
May 22, 2019 09:59
-
-
Save t-katsushima/2cb67c2c06be8327614a9926c701621e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function solve() { | |
| const board = document.getElementsByClassName('board')[0].children | |
| const side = board.length | |
| document.dispatchEvent(new KeyboardEvent( 'keypress', {key: "w"})) | |
| var table = new Array(side); | |
| for (let y = 0; y < side; y++) { | |
| table[y] = new Array(side).fill(false); | |
| for (let x = 0; x < side; x++) { | |
| // sisisin is true | |
| table[y][x] = board[y].children[x].src === "https://cdn.glitch.com/c6d28c74-b5a2-40e7-b00f-2547f0352974%2Fsisisin.jpg?1532338150490" | |
| } | |
| }; | |
| var ans = [] | |
| var memo = new Array(side); | |
| for (let y = 0; y < side; y++) { | |
| memo[y] = new Array(side).fill(1e9); | |
| }; | |
| var sx; | |
| for (let x = 0; x < side; x++) { | |
| if (!table[side-1][x]) { | |
| sx = x; | |
| break | |
| } | |
| }; | |
| table[side-2][sx] = false; | |
| var gx; | |
| for (let x = 0; x < side; x++) { | |
| if (!table[0][x]) { | |
| gx = x; | |
| break | |
| } | |
| }; | |
| // bfs! bfs! | |
| queue = [[sx, side-2]] | |
| memo[side-2][sx] = 0 | |
| while (queue[0]) { | |
| const [x, y] = queue.shift() | |
| const now = memo[y][x] | |
| for (let p of mk4Dir([x, y])) { | |
| [nx, ny] = p | |
| if (isMovable(nx, ny)) | |
| if (now + 1 < memo[ny][nx]) { | |
| memo[ny][nx] = now + 1 | |
| queue.push([nx, ny]) | |
| } | |
| } | |
| } | |
| function dfs(p) { | |
| const [x, y] = p | |
| var ax; var ay; | |
| if (memo[y][x] > 0) { | |
| for (let p of mk4Dir([x, y])) { | |
| [nx, ny] = p | |
| if (isMovable(nx, ny)) | |
| if (memo[ny][nx] == memo[y][x] - 1) { | |
| ax = nx | |
| ay = ny | |
| } | |
| } | |
| ans.push(mkDirection([x, y], [ax, ay])) | |
| dfs([ax, ay]) | |
| } | |
| } | |
| dfs([gx, 0]) | |
| ans.reverse() | |
| for (let i = 0; i < ans.length; i++) { | |
| setTimeout(() => | |
| document.dispatchEvent(new KeyboardEvent( 'keypress', {key: ans[i]})), 20*i | |
| ) | |
| } | |
| function isMovable(x, y) { | |
| return 0 <= x && x < side && 0 <= y && y < side && !table[y][x] | |
| } | |
| function mk4Dir(p) { | |
| const [x, y] = p | |
| const d = [[1, 0], [-1, 0], [0, 1], [0, -1]] | |
| return d.map(dxy => [x + dxy[0], y + dxy[1]]) | |
| } | |
| function mkDirection(now, next) { | |
| const [x, y] = now | |
| const [nx, ny] = next | |
| if (nx < x) { return "d" } | |
| else if (nx > x){ return "a" } | |
| else if (ny < y) { return "s" } | |
| else { return "w" } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment