Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace task1
- {
- class Tree : IComparable<Tree>
- {
- public int x;
- public int h;
- public Tree(int x, int h)
- {
- this.x = x;
- this.h = h;
- }
- public int CompareTo(Tree other)
- {
- return this.x.CompareTo(other.x);
- }
- }
- class Node
- {
- public int i;
- public int d;
- public Node(int i, int d)
- {
- this.i = i;
- this.d = d;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var line = Console.ReadLine().Split(' ');
- int n = int.Parse(line[0]);
- int mj = int.Parse(line[1]);
- int mh = int.Parse(line[2]);
- var trees = new Tree[n];
- for (int i = 0; i < n; i++)
- {
- line = Console.ReadLine().Split(' ');
- trees[i] = new Tree(int.Parse(line[0]), int.Parse(line[1]));
- }
- Array.Sort(trees);
- var visited = new bool[n];
- var q = new Queue<Node>();
- visited[0] = true;
- q.Enqueue(new Node(0, 0));
- while (q.Count != 0)
- {
- var p = q.Dequeue();
- for (int i = p.i + 1; i < n; i++)
- {
- if (visited[i] == true)
- {
- continue;
- }
- if (Math.Abs(trees[p.i].x - trees[i].x) >= mj)
- {
- break;
- }
- if (Math.Abs(trees[p.i].h - trees[i].h) >= mh)
- {
- continue;
- }
- if (i == n - 1)
- {
- Console.WriteLine(p.d + 1);
- return;
- }
- q.Enqueue(new Node(i, p.d + 1));
- visited[i] = true;
- }
- }
- Console.WriteLine(-1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement