Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5fb0a12

Browse files
committed
feat: 127. 骑士的攻击增加js解法
1 parent 0b7924f commit 5fb0a12

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

‎problems/kamacoder/0126.骑士的攻击astar.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,131 @@ for _ in range(n):
375375

376376
### Javascript
377377

378+
```js
379+
class MinHeap {
380+
constructor() {
381+
this.val = []
382+
}
383+
push(val) {
384+
this.val.push(val)
385+
if (this.val.length > 1) {
386+
this.bubbleUp()
387+
}
388+
}
389+
bubbleUp() {
390+
let pi = this.val.length - 1
391+
let pp = Math.floor((pi - 1) / 2)
392+
while (pi > 0 && this.val[pp][0] > this.val[pi][0]) {
393+
;[this.val[pi], this.val[pp]] = [this.val[pp], this.val[pi]]
394+
pi = pp
395+
pp = Math.floor((pi - 1) / 2)
396+
}
397+
}
398+
pop() {
399+
if (this.val.length > 1) {
400+
let pp = 0
401+
let pi = this.val.length - 1
402+
;[this.val[pi], this.val[pp]] = [this.val[pp], this.val[pi]]
403+
const min = this.val.pop()
404+
if (this.val.length > 1) {
405+
this.sinkDown(0)
406+
}
407+
return min
408+
} else if (this.val.length == 1) {
409+
return this.val.pop()
410+
}
411+
412+
}
413+
sinkDown(parentIdx) {
414+
let pp = parentIdx
415+
let plc = pp * 2 + 1
416+
let prc = pp * 2 + 2
417+
let pt = pp // temp pointer
418+
if (plc < this.val.length && this.val[pp][0] > this.val[plc][0]) {
419+
pt = plc
420+
}
421+
if (prc < this.val.length && this.val[pt][0] > this.val[prc][0]) {
422+
pt = prc
423+
}
424+
if (pt != pp) {
425+
;[this.val[pp], this.val[pt]] = [this.val[pt], this.val[pp]]
426+
this.sinkDown(pt)
427+
}
428+
}
429+
}
430+
431+
const moves = [
432+
[1, 2],
433+
[2, 1],
434+
[-1, -2],
435+
[-2, -1],
436+
[-1, 2],
437+
[-2, 1],
438+
[1, -2],
439+
[2, -1]
440+
]
441+
442+
function dist(a, b) {
443+
return ((a[0] - b[0])**2 + (a[1] - b[1])**2)**0.5
444+
}
445+
446+
function isValid(x, y) {
447+
return x >= 1 && y >= 1 && x < 1001 && y < 1001
448+
}
449+
450+
function bfs(start, end) {
451+
const step = new Map()
452+
step.set(start.join(" "), 0)
453+
const q = new MinHeap()
454+
q.push([dist(start, end), start[0], start[1]])
455+
456+
while(q.val.length) {
457+
const [d, x, y] = q.pop()
458+
// if x and y correspond to end position output result
459+
if (x == end[0] && y == end[1]) {
460+
console.log(step.get(end.join(" ")))
461+
break;
462+
}
463+
for (const [dx, dy] of moves) {
464+
const nx = dx + x
465+
const ny = dy + y
466+
if (isValid(nx, ny)) {
467+
const newStep = step.get([x, y].join(" ")) + 1
468+
const newDist = dist([nx, ny], [...end])
469+
const s = step.get([nx, ny].join(" ")) ?
470+
step.get([nx, ny]) :
471+
Number.MAX_VALUE
472+
if (newStep < s) {
473+
q.push(
474+
[
475+
newStep + newDist,
476+
nx,
477+
ny
478+
]
479+
)
480+
step.set([nx, ny].join(" "), newStep)
481+
}
482+
}
483+
}
484+
}
485+
}
486+
487+
async function main() {
488+
const rl = require('readline').createInterface({ input: process.stdin })
489+
const iter = rl[Symbol.asyncIterator]()
490+
const readline = async () => (await iter.next()).value
491+
const n = Number((await readline()))
492+
493+
// find min step
494+
for (let i = 0 ; i < n ; i++) {
495+
const [s1, s2, t1, t2] = (await readline()).split(" ").map(Number)
496+
bfs([s1, s2], [t1, t2])
497+
}
498+
}
499+
500+
main()
501+
```
502+
378503
### TypeScript
379504

380505
### PhP

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /