Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const readline = require('readline');
- const rl = readline.createInterface({
- input: process.stdin
- });
- const rast = (p1, p2) => {
- return Math.abs(p2[0] - p1[0]) + Math.abs(p2[1] - p1[1]);
- }
- let lines = [];
- rl.on('line', (line) => {
- lines.push(line);
- }).on('close', () => {
- const n = +lines[0];
- const points = lines.slice(1, n+1).map(el => el.split(' ').map(l => +l));
- const k = +lines[n+1];
- const [start, end] = lines[n+2].split(' ').map(el => +el);
- const graph = Array(n).fill(-1).map(el => []);
- for(let i = 0; i < n; i++) {
- for(let j = 0; j < n; j++) {
- if(i == j) continue;
- if(rast(points[i], points[j]) <= k) {
- graph[i].push(j);
- graph[j].push(i);
- }
- }
- }
- let res = -1;
- const visited = Array(n).fill(false);
- const q = [];
- q.push([start-1, 0]);
- while(q.length > 0) {
- const cur = q.shift();
- if(visited[cur[0]])
- continue;
- if(cur[0] == end-1) {
- res = cur[1];
- break;
- }
- graph[cur[0]].forEach(el => q.push([el, cur[1]+1]));
- }
- process.stdout.write(res.toString());
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement