Problem : Ball Passing Game
Problem : Ball Passing Game
TCS India has organized a blind folded ball passing game for two players.
They have a special kind of ground where playe B can throw the ball in any four directions (up, down, right,
left).
The ground consist of some slant poles on which.when the ball strikes the ball changes its direction by 90
degrees as shown below.
If suppose y
ou have thrown a ball in a direction in which there is no pole then the ball goes out of the
stadium in t hat direction
The player B has the ball initially. The objective is to output the minimum steps in which B can pass the ball
to A and also the count of poles the ball has struck. If he can never pass the ball to A, then your output
should be -1.
If you get multiple minimum steps then output those steps in which the ball hits minimum no of barriers
For example,
A 0 0
0 0
0 B 0
4 3 and 4 1 but you have to print 4 1 as output as this path has minimum no of barriers .
Input Format:
First line containing the integer K indicating the size of the field
Next two lines each having a pair of integers separated by space giving the row and column numbers of the
positions of A and B respectively K lines of K integers each, separated by space, giving the positions of the
barriers in the field - 0 indicating no barrier, 1 indicating a / barrier and 2 indicating a barrier.
Output Format:
Constraints:
3 K 20
Example 1
Input
4
0 3
3 2
0 0 1 0
1 0 1 1
0 0 0 0
2 0 0 0
Output
4 2
Explanation
Shown in the figure above.
Example 2
Input
3
2 2
0 0
0 0 1
0 0 0
0 0 0
Output
-1
Explanation
The ball goes out of the field after hitting the barrier at 1 3
Roco is an island near Africa which is very prone to forest fire. Forest fire is such that it destroys the
complete forest. Not a single tree is left.
This island has been cursed by God , and the curse is that whenever a tree catches fire, it passes the fire to
all its adjacent tree in all 8 directions,North, South, East, West, North-East, North-West, South-East, and
South-West.
And it is given that the fire is spreading every minute in the given manner, i.e every tree is passing fire to
its adjacent tree.
Suppose that the forest layout is as follows where T denotes tree and W denotes water.
As you can see in the figure below, the tree at (3,3) will pass the fire to its adjacent 5 trees. Other 3
adjacent places have water and hence will not be impacted.
If suppose the tree that catches the fire is at (3,3) , then the fire from 3,3 will be passed on to
(2,3),(3,4),(3,2), (4,2),(2,4) . Which will happen in 2nd minute.
Thus from the figure you can see in the 4th minute whole forest will be on fire.
Your task is that given the location of the first tree that catches fire, determine how long w ould it take for
the entire forest to be on fire. You may assume that the lay out of the forest is such that t he whole forest
will catch fire for sure and that there will be at least one tree in the forest.
Input Format:
First line contains two integers, M, N, space separated, giving the size of the forest in terms of the number
of rows and columns respectively
The next line contains two integers X,Y, space separated, giving the coordinates of the first tree that catches
the fire
The next M lines, where ith line containing N characters each of which is either T or W, giving the position of
the Tree and Water in the ith row of the forest.
Output Format:
Single integer indicating the number of minutes taken for the entire forest to catch fire
Constraints:
3
M 20
3 N 20
Example 1
Input
3 3
1 3
W T T
T W W
W T T
Output
5
Explanation
In the second minute, tree at (1,2) catches fire. In the third minute, the tree at (2,1) catches fire, fourth
minute tree at (3,2) catches fire and in the fifth minute the last tree at (3,3) catches fire.
Example 1
Example 2
Input
6 6
1 6
W T T T T T
T W W W W W
W T T T T T
W W W W W T
T T T T T T
T W W W W W
Output
16
Explanation
The minute at which each tree catches fire is given below:
W 5 4 3 2 1
6 W W W W W
W 7 8 9 10 11
W W W W W 11
16 15 14 13 12 11
16 W W W W W W
Caesar wishes to lay siege to the indomitable Gauls, and moves his catapults (hurling huge rocks) to all
point in a line to the Gaulish village. The warriors of Gaul, Asterix and Obelix, are very good at
counterattack, and can be stopped only if the catapults are all in line towards the Gaulish village (towards
North). Also, the catapults are so heavy that he needs many men and horses to move one, and he can move
only one catapult at a time. Also he knows that the counterattack is coming soon, and he needs to align
them quickly.
For simplicity, we model the situation as a n x n grid of squares with the initial position of the n catapults
given. We need to advise Caesar on the minimum number of moves to align them in a vertical (northward)
direction. A catapult can move North-South (up-down) or East West (left-right) but not diagonally.
Input Format:
There are then a set of n lines, each corresponding to an East West row in the grid, consisting of a set of
comma separated numbers (1 or 0) representing the presence or absence of a catapult in that position of
the grid in that row.
Output Format:
One line containg a non-negative integer indicationg the number of moves necessary to align the catapults
in a vertical direction.
Constraints:
5n50
Example 1
Input
5
0,1,0,0,0
0,0,0,1,0
0,0,0,1,0
0,0,0,0,0
1,0,1,0,0
Output
6
Explanation
One set of minimal moves required are (1,2)-(1,3), (2,4)-(2,3), (3,4)-(3,3), (5,1)-(4,1)-(4,2)-(4,3)
Example 2
Input
5
1,1,0,0,0
0,0,0,0,0
0,1,0,0,0
0,1,0,0,0
0,0,1,0,0
Output
3
Explanation
One set of moves is (1,1)-(2,1)-(2,2), (5,3)-(5,2)
Problem : Spiral
The prime numbers are written in a spiral form staring at (0,0) and moving as shown in the diagram below.
The numbers shown on the right column and the bottom row are the column numbers and row numbers
respectively (y and x coordinate frames).
Input Format:
Output Format:
Constraints:
N10
Each prime < 1000000
Example 1
Input
2
3
7
Output
1 0
0 1
Explanation
There are 2 primes in this test case (N=2). The primes are 3 and 7. The coordinates of these in the spiral is
(1,0) and (0,1). The output hence has these in space separated form.
Example 2
Input
3
5
11
13
Output
1 1
-1 1
-1 0
Explanation
There are 3 primes in this test case (N=2). The primes a
re 5, 11 and 13. The coordinates of these in the
spiral is (1,1), (-1,1) and (-1,0). The output hence has t hese in space separated form.
The parcel section of the Head Post Office is in a mess. The parcels that need to be loaded to the vans have
been lined up in a row in an arbitrary order of weights. The Head Post Master wants them to be sorted in the
increasing order of the weights of the parcels, with one exception. He wants the heaviest (and presumably
the most valuable) parcel kept nearest his office.
You and your friend try to sort these boxes and you decide to sort them by interchanging two boxes at a
time. Such an interchange needs effort equals to the product of the weights of the two boxes.
Input Format:
The first line consists of two space separated positive integers giving the number of boxes (N) and the
position of the Head Post Master's office (k) where the heaviest box must be.
Output Format:
The output is one line giving the total effort taken to get the boxes in sorted order, and the heaviest in
position k.
Constraints:
N50
Weights 1000
Example 1
Input
5 2
20 50 30 80 70
Output
3600
Explanation
There are 5 boxes (N=5) and the heaviest box must be in position 2 (k=2). If we look at the final order
(sorted, with the heaviest at position 2), it should be 20 80 30 50 70. If we look at this, we notice that only
the 50 and the 80 parcels need to be exchanged. As this takes effort of the product of the weights, the effort
is 3600.
Further reduction can be obtained if we use the smallest package (20) as an intermediary. If we exchange
20 with 50 (effort 1000), then with 80 (effort 1600) and back with 50 again (effort 1000), the effect is the
same, with a total effort of 3600 (less than the effort obtained by the direct move) an the effort
50 2
0 3 0 8 0 7 0
50 8 0 3 0 2 0 7 0
20 8 0 3 0 8 0 7 0
Example 2
Input
6 3
30 20 40 80 70 60
Output
7600
Explanation
There are 6 parcels, and the heaviest should be at position 3. Hence the final order needs to be 20 30 80 40
60 70. If we look at the initial position, we see that 20 and 30 need to be exchanged (effort 600), 40 and 80
need to be exchanged (effort 3200) and 60 and 70 need to be exchanged (effort 4200). Hence the total
effort is 600+3200+4200=8000.
(600) 20 30 40 80 70 60
(3200) 20 30 80 40 70 60
(1200) 60 30 80 40 70 20
(1400) 60 30 80 40 20 70
(1200) 20 30 80 40 60 70
A total effort of 7600 is obtained rather than an effort of 8000, which is the output.
Problem : Solitaire
A deck of alphabet cards contains cards marked with letters of the alphabet (A,B,C,..). Each letter appears
on one or more cards. The pack is shuffled and the cards dealt out one by one into a number of stacks. Any
number of stacks may be used, but after all the cards have been dealt, you must get all the cards into
alphabetical order, with all cards showing A first and then all cards showing B and so on. Since they are
stacked, only the top card of any stack can be retrieved.
Given the order of the cards, the objective is to determine the minimum number of stacks which enables the
final operation of getting them into alphabetic order.
For example, if the order of the cards is ABNJJBBA, the stacks after dealing could be
It is obvious that this can be achieved during the dealing process. During the retrieval process, all the A's
can be picked up from stack 1 one at a time, by accessing only the top of the stack, all the B's can be
obtained in the same way, and then the J's. Once the J's have been picked up, the N's are on top of the
stack and can be picked up.
Input Format:
One line containing an integer N that defines how many sequences appear in this test case.
After the first line, N lines, each containing a sequence of space separated upper case letters. Note that not
all letters need to be present, and that a letter may appear many times
Output Format:
N lines, each containing one integer representing the number of stacks needed for the corresponding
sequence of cards in the input
Constraints:
1N20
Numbers of cards in each sequence 100
Example 1
Input
2
G K L C A J E I
G L J B N J F A M B
Output
3
3
Explanation
There are two sequences (N=2). For the first sequence, one possible set of stacks is GCA, KI, LJE.
With these 3 stacks, it is possible to get the cards in alphabetic order. Similarly for the second sequence, a
possible set of stacks is GGB, NM, JJA,LF. Hence the output is 3 for the first sequence and 3 for the second
sequence.
Example 2
Input
2
E G D E B H C G H D
G C D E B H D N C J
Output
4
5
Explanation
There are two sequences (N=2). For the first sequence, one possible set of stacks is EEB, GDC, HH, GD. For
the second, possible set of stacks is GC, DDC, EB, H, NJ. As there are 4 and 5 stacks respectively, the
output is 4 and 5.