Advertisement
Pearlfromsu

yandex solved

Aug 18th, 2023
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. const readline = require('readline');
  2.  
  3. const rl = readline.createInterface({
  4.     input: process.stdin
  5. });
  6.  
  7. const rast = (p1, p2) => {
  8.     return Math.abs(p2[0] - p1[0]) + Math.abs(p2[1] - p1[1]);
  9. }
  10.  
  11. let lines = [];
  12. rl.on('line', (line) => {
  13.     lines.push(line);
  14. }).on('close', () => {
  15.     const n = +lines[0];
  16.     const points = lines.slice(1, n+1).map(el => el.split(' ').map(l => +l));
  17.     const k = +lines[n+1];
  18.     const [start, end] = lines[n+2].split(' ').map(el => +el);
  19.    
  20.     const graph = Array(n).fill(-1).map(el => []);
  21.     for(let i = 0; i < n; i++) {
  22.         for(let j = 0; j < n; j++) {
  23.             if(i == j) continue;
  24.             if(rast(points[i], points[j]) <= k) {
  25.                 graph[i].push(j);
  26.                 graph[j].push(i);
  27.             }
  28.         }
  29.     }
  30.    
  31.     let res = -1;
  32.     const visited = Array(n).fill(false);
  33.     const q = [];
  34.     q.push([start-1, 0]);
  35.     while(q.length > 0) {
  36.         const cur = q.shift();
  37.         if(visited[cur[0]])
  38.             continue;
  39.         if(cur[0] == end-1) {
  40.             res = cur[1];
  41.             break;
  42.         }
  43.         graph[cur[0]].forEach(el => q.push([el, cur[1]+1]));
  44.     }
  45.    
  46.     process.stdout.write(res.toString());
  47. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement