Advertisement
Pearlfromsu

yandex contest 1808231656

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