← 返回 pinterest 的题目列表Determine Relatedness and Shortest Distance Between Pins
类型:online_judge
Given a system composed of boards and pins, where each board contains several pins. Pins might have direct distances between them. Implement the following features:
Given two pins, determine if they are related. Related means they are in the same or connected boards.
Given two pins, calculate the shortest distance between them. Return -1 if no path exists.
Given two boards, calculate the shortest distance using any pins. Return -1 if no path exists.
Input format:
Data structure for boards and pins
Distances between pins
Output format:
For function 1, return a boolean value
For functions 2 and 3, return an integer
Provide a time and space complexity explanation.
Example case:
Board 1: Travel, contains pins: Mountain, Lake, Inn
Board 2: Decoration, contains pins: House, Stone Pond, Inn
Board 3: Graffiti, contains pins: Picasso, Lake
Board 4: Car, contains pins: BYD, Xiaopeng
Determine if pins "Mountain" and "Lake" are related.
Calculate the shortest distance between pins "Mountain" and "House".
Calculate the shortest distance between boards "Travel" and "Car".
Example
Input
Create a board system with boards and pins and test are_related and shortest distance functions.