algorithmstudy's Introduction
algorithmstudy's People
algorithmstudy's Issues
[medium] Red Knight's Shortest Path
In ordinary chess, the pieces are only of two colors, black and white. In our version of chess, we are including new pieces with unique movements. One of the most powerful pieces in this version is the red knight.
The red knight can move to six different positions based on its current position (UpperLeft, UpperRight, Right, LowerRight, LowerLeft, Left) as shown in the figure below.
image
The board is a grid of size . Each cell is identified with a pair of coordinates , where is the row number and is the column number, both zero-indexed. Thus, is the upper-left corner and is the bottom-right corner.
Complete the function printShortestPath, which takes as input the grid size , and the coordinates of the starting and ending position and respectively, as input. The function does not return anything.
Given the coordinates of the starting position of the red knight and the coordinates of the destination, print the minimum number of moves that the red knight has to make in order to reach the destination and after that, print the order of the moves that must be followed to reach the destination in the shortest way. If the destination cannot be reached, print only the word "Impossible".
Note: There may be multiple shortest paths leading to the destination. Hence, assume that the red knight considers its possible neighbor locations in the following order of priority: UL, UR, R, LR, LL, L. In other words, if there are multiple possible options, the red knight prioritizes the first move in this list, as long as the shortest path is still achievable. Check sample input for an illustration.
Input Format
The first line of input contains a single integer . The second line contains four space-separated integers . denotes the coordinates of the starting position and denotes the coordinates of the final position.
Constraints
the starting and the ending positions are different
Output Format
If the destination can be reached, print two lines. In the first line, print a single integer denoting the minimum number of moves that the red knight has to make in order to reach the destination. In the second line, print the space-separated sequence of moves.
If the destination cannot be reached, print a single line containing only the word Impossible.
Sample Input 0
7
6 6 0 1
Sample Output 0
4
UL UL UL L
Explanation 0
image
Sample Input 1
6
5 1 0 5
Sample Output 1
Impossible
Explanation 1
image
Sample Input 2
7
0 3 4 3
Sample Output 2
2
LR LL
Explanation 2
image
[medium] Gena Playing Hanoi
url : https://www.hackerrank.com/challenges/gena/problem
The Tower of Hanoi is a famous game consisting of rods and a number of discs of incrementally different diameters. The puzzle starts with the discs neatly stacked on one rod, ordered by ascending size with the smallest disc at the top. The game's objective is to move the entire stack to another rod, obeying the following rules:
Only one disc can be moved at a time.
Each move consists of taking the topmost disc from a stack and moving it to the top of another stack.
No disc may be placed on top of a smaller disc.
Gena has a modified version of the Tower of Hanoi. His Hanoi has rods and discs ordered by ascending size. He made a few moves (following the rules above), but stopped and lost his place. He wants to restore the tower to its original state by making valid moves. Given the state of Gena's Hanoi, help him calculate the minimum number of moves needed to restore the tower to its original state.
Note: Gena's rods are numbered from to . All discs are initially located on rod .
Input Format
The first line contains a single integer, , denoting the number of discs.
The second line contains space-separated integers, where the integer is the index of the rod where the disk with diameter is located.
Constraints
Output Format
Print the minimum number of moves Gena must make to restore the tower to its initial, ordered state on the first rod.
Sample Input
3
1 4 1
Sample Output
3
Explanation
moves are enough to build the tower. Here is one possible solution:
gena1.png
gena2.png
[medium] Cut the Tree
url: https://www.hackerrank.com/challenges/cut-the-tree/problem
Anna loves graph theory! She has a tree where each vertex is numbered from to , and each contains a data value.
The sum of a tree is the sum of all its nodes' data values. If she cuts an edge in her tree, she forms two smaller trees. The difference between two trees is the absolute value between their sums.
Given a tree, determine which edge to cut so that the resulting trees have a minimal difference between them, then return that difference.
For example, your tree's nodes have weights of [1,2,3,4,5,6]. In this case, node numbers match their weights for convenience. In the diagram below, you have the following edges: [(1,2),(1,3),(2,6),(3,4),(3,5)].
The values are calculated as follows:
Edge Tree 1 Tree 2 Absolute
Cut Sum Sum Difference
1 8 13 5
2 9 12 3
3 6 15 9
4 4 17 13
5 5 16 11
The minimum absolute difference is 3.
Note: The tree is always rooted at vertex 1.
Function Description
Complete the cutTheTree function in the editor below. Return an integer that represents the minimal absolute difference achievable between the resultant two trees.
cutTheTree has the following parameter(s):
data: an array of integers that represent node values
edges: an 2 dimensional array of integer pairs where each pair represents an edge in the graph
Input Format
The first line contains an integer n, the number of vertices in the tree.
The second line contains n space-separated integers, where each integer u denotes the value of data[u].
Each of the n-1 subsequent lines contains two space-separated integers u and v describing edge u <-> v in tree t.
Constraints
Output Format
A single line containing the minimum difference possible for tree t.
Sample Input
6
100 200 100 500 100 600
1 2
2 3
2 5
4 5
5 6
Sample Output
400
[medium] Count Luck
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.