SWC01.
Grid Acid 3
SWC02. Rock climbing [SWC 18/05/2016] 7
SWC03. Shooting Game (SWC 27/04/2016) 8
SWC04. Components (SWC 25/05/2016) 9
SWC05. Dobara businessman 10
SWC06. Marathon (SWC 22/06/2016) 11
SWC07. BusinessMan Chobara [Part 4 of BusinessMan Series] 13
SWC08. Plane Game (SWC 15/06/2016) 15
SWC09. FoneZone (SWC) 16
SWC10. Robot 17
SWC11. Businessman toll 18
SWC12. Pipes (TBD) 19
SWC13. Bipartition the cities (Graph is Bipartite or not) (BFS) 21
SWC14. Find cycle in a tree (DFS) 21
SWC01. Grid Acid
A rectangular grid has N x M size. Each cell is either made of a special metal (type A) or made of
stone (type B). If acid is poured on a cell of grid, it can either melt it(for type A: special metal cell)
and spread further or do not pass through it(for type B: stone cell).
The cell made with special metal (type A) can melt with acid in 1 second and thus allow acid to
spread further.
The cell made with stone (type B) does not react with acid and hence neither melt nor allow acid to
pass through it.
There is a third type of a cell that is empty (type C), but has a boundary (all 4 sides) covered with a
protective metal.
Fig. A grid of 7 x 8 size as example
If all 4 sides of it (type C: empty cell) come into contact with acid at any given time, then in that
case boundary of it (all 4 sides)melt and allow acid to pass through it. In that case it (type C: empty
cell) get filled with acid.
There is only one and only one such cell in a given grid.
Acid is poured on one of the cell of grid, the cell being made of special metal that can melt with
acid.
It is guaranteed that acid will be poured on only one cell made up of special metal (type A), not on
the stone (type B) or the empty cell (type C).
Acid is poured continuously until all the grid cells (except stone - type B) melt completely.
You have to tell when the empty cell with special protective boundary will get filled with acid and
when whole grid will get filled with acid (except the cells made up of stones).
It takes 1sec for acid to dissolve special metal cell and after that it can spread to its 4 orthogonal
neighbors (Left, Right, Up, Down).
Input:
First entry is number of test cases; rest is each test caseinput.
For each test case first row contains N and M as twointegers separated by a space.
Next row contains the location of cell (row and columnnumber separated by space) where acid will
be poured continuously until wholegrid (except stone cells) melt.
Next N rows contain M integer values each containing thecell type.
Cell type value is of 3 types:
- 0 : cell is stone (type B)
- 1: cell is made special metal (type A)
- 2: cell is special empty cell, having a special boundary (type C)
Note: There is always one and only one cell of typeC (value 2) in a given grid.
Output:
Output should contain 2 lines for each test case.
1st line: Case#
2nd line: Count1 Count2
Where:
Count1: time in seconds when special empty cell (type C) will get filled.
Count2: time in seconds when whole grid will get filled with water (note: stone cell can not be filled
with acid).
Note:
1. Count2 will be -1if all cells of the grid (except stone cells) cannot be dissolved. Whole grid
dissolving term means that all cells except stone get filled with Acid.
2. Count1 will be -1if empty cell cannot be filled. If empty cell cannot be filled the #1 is also
applicable, i.e. then Count2 = -1.
3. Once acid enters special cell, it accumulates there for 1 second. After that the acid starts leaking
to neighboring (left, right, up, down) cells.
4. The terms “dissolve”, “melt”, “leaking” are used to express similar meaning that the cell starts
leaking the acid to its neighbor cells (left, right, up, down).
5. Acid is poured continuously so once a cell starts leaking acid, it may spread further to other cells
in further course of time.
6. The maximum number of rows or columns of grid is 3000.
Sample Input:
9 ---->Number of test cases.
4 5 -------> N=4: number of rows, M= 5: number of columns
2 4 -----> Location of cell(row col) where acid is poured
1 0 1 0 1 ------> Grid 1st row with M cell entries
1 0 1 1 1 ----> Acid is poured on 4thcell of this (2nd) row.
1 1 2 1 1 -----> Rows contains empty cell (type C: value: 2)
1 0 1 0 1 -------> Last row of the grid with cell M values
3 3 ------> 2ndtest case starts, N=3, M=3
1 2 ---> Acid is pouring location
1 1 0 ---> 1strow of grid; Acid poured on 2nd cell of this (1st) row of the grid.
121
011
33
11
111
121
011
33
33
111
121
011
44
23
0010
0111
1121
1011
33
13
011
121
111
45
13
10101
10111
11121
10011
45
35
10101
10111
11121
10011
45
24
10101
11111
11211
10111
Sample Output:
Case #1
-1 -1 --->Count1 Count2 (refer to output description)
Case #2
-1 -1
Case #3
66
Case #4
66
Case #5
55
Case #6
66
Case #7
77
Case #8
59
Case #9
46
SWC02. Rock climbing [SWC 18/05/2016]
There is a man who wants to climb a rock from a starting point to the destination point.
Given a map of the rock mountain which N = height, M = width.
In the map, character '-' is the possible foot place spot (where can climb)
He can freely move up/down at vertical spots which '-' exists sequentially.
It's impossible to move horizontally in case there is more than one space between '-' in the same
height level
Depending on how high/low he moves towards the upper or lower direction at one time, the level of
difficulty of rock climbing gets determined
The maximum height of moving from the starting point to destination point is the level of difficulty
of rock climbing
The total distance of movement is not important -> focus on the level of difficulty of rock climbing.
There are more than one path from the starting point to destination point.
=> Output: The minimum level of difficulty of all rock climbing paths level.
case #1
1
case #2
2
case #3
1
case #4
1
case #5
3
case #6
1
case #7
1
case #8
2
case #9
5
case #10
2
SWC03. Shooting Game (SWC 27/04/2016)
Po our Kung Fu Panda is training with Master Shi-fu at Jade Palace. To prove his awesomeness he
has to pass a Test. Being a martial art expert he has been asked to break N tiles, which is a child's
play for him. But wait, he can't just break the tiles just like that. For each tile he breaks he would be
given a score which is computed according to the instructions in the secret Dragon Scroll.
There will be a N Tiles marked with value Vi (where V(i…N)). When Po breaks any of tile, he is
given a score for the move. That score is computed using the instructions mentioned in the dragon
scroll.
Below are the instructions to calculate the score.
1. When Tile Vi Explodes then score will be a product of Vi-1 & Vi+1 (score = Vi-I * Vi+1).
2. When Tile Vi Explodes and there is only left Tile present then score will be Vi-1.
3. When Tile Vi Explodes and there is only right Tile present then score will be Vi+1.
4. When Tile Vi explodes and there is no left and right Tile present then score will be Vi.
Whenever any of the tile breaks, its adjacent Tiles become next to each other.
Po is competing with the Furious Five, and to prove that he is worthy of being called the Dragon
Warrior he has to score highest points. You have to help Po score the maximum score in the
competition. Write a program to help Po score maximum points.
Conditions:
a) Execution time limits 3 seconds. b) No of Tiles N, where 1 <= N <= 10
c) Vi value of the Tile 1 <= Vi <= 1000. d) No two Tiles explode at same time.
Input:
Consists of TC (1 <= TC <= 50).
N – No of Tiles.
V0…..VN N Tiles with their values .
Output:
#TC //first line
SCORE //second line
Sample I/O
5
4
1234
5
3 10 1 2 5
7
12 48 28 21 67 75 85
8
245 108 162 400 274 358 366 166
10
866 919 840 944 761 895 701 912 848 799
#1
20
#2
100
#3
16057
#4
561630
#5
6455522
SWC04. Components (SWC 25/05/2016)
In a warehouse
D number of CPUs,
E number of memory chips
F number of boards
Each unused CPU and memory components can be sold as,
d - selling price of CPUs
e – selling price of memory
Unused boards can not be sold.
Given a set of N number of models out of these components, we can choose max of 3 models only.
Making each module requires,
ai CPUs
bi memories
ci boards
pi selling price of this model
Constraints
N = 1 to 8
D,E,F = 1 to 100
d, e = 1 to 10
ai, bi, ci = 1 to 5
pi = 1 to 100
sample inputs
5 => T
10 10 10 2 1 => D E F d e
1 => N
1 2 2 3 => ai bi ci pi
5 10 10 1 1
2
2118
1116
46421
4
2429
1317
2118
1226
40 80 60 1 3
7
3 2 2 56
5 4 2 12
3 5 3 65
1 2 5 78
5 5 2 85
4 2 3 76
5 5 1 48
SWC05. Dobara businessman
The businessman returns to his city after trading multiple cities. He's now left with 3 types of fruits
(apple, orange, mango) which he wants to sell to get maximum profit. The local fruit market is
regulated by the authority and the prices of different fruits and fruit-chats is fixed. Authority also
allows a single businessman to sell only 3 kinds of chats.
Also, there is over production of mangoes in the kingdom and authority has set its price to 0.
You need to find out the maximum profit businessman can get.
[Input]
First number will be number of test cases present in file
For each test case, count of apples, oranges and mangoes is given along with per unit price of apples
and oranges.
Then the count N of different fruit-chat combinations is given.
In next N lines, for each fruit-chat, count of apples, oranges and mangoes is given, along with price
of the chat
[Output]
For each test case, print "Case #" followed by test case number. In next line, print the maximum
profit that can be achieved.
[Limits]
0<N<9
Number of each type fruits can be upto 100
[Sample Input]
2
2 2 2 35 1
2
5 7 1 63
5 2 6 82
4 2 3 92 96
7
3545
3 2 5 83
2 5 3 96
3 7 6 39
5 1 6 100
1 7 6 12
3 6 4 65
[Sample Output]
Case #1
72
Case #2
560
SWC06. Marathon (SWC 22/06/2016)
There are 5 phases of running given.
For each space, it will take certain amount of time and energy to run for one kilometre.
For the given Distance and the maximum energy spend, find out the optimal time.
Input
Max_energy target_km
P1_time_min P1_time_sec P1_energy
P2_time_min P2_time_sec P2_energy
P3_time_min P3_time_sec P3_energy
P4_time_min P4_time_sec P4_energy
P5_time_min P5_time_sec P5_energy
BusinessMan Tibara [Part 3 of BusinessMan Series]
The BusinessMan has got huge benefits after the sale of all his fruits as chats. As the Mangoes are
over produced in the city, he is still left with a huge collection. He got to know that there is almost
NIL production of mangoes in a nearby city, so he plans to visit there and sell his mangoes to get
more profit. Its summer (as we know mangoes are being produced) and the weather is very harsh,
so he decides to hire a transport vehicle. As he has to take a big pile of mangoes, he needs a big
vehicle.
Luckily, his friend Mr. Jugadu, was working on a new carrier vehicle (which he called Pushpak
Truck) that can run on 5 different fuels as LPG, CNG, PETROL, DIESEL and HYDROGEN, each
with unique speed performance and heating side-effect per kilometer.
As the vehicle is in experimental phase, it breaks down after reaching at a particular heating level.
The businessman decides to take the Pushpak truck and try his luck to reach the destination. Given
the heat bearing capacity of the vehicle, distance to the nearby city and speed / heating parameters
for all the 5 mentioned fuels, you need to find out whether the businessman can reach his
destination or not. If he can reach the destination, print the required time in minutes and seconds. If
he cannot reach the destination, print -1.
You can safely assume that the fuel tanks hold enough fuel to complete the journey using either of
the fuels.
[Input]
First number will be number of test cases present in file. For each test case, the heat bearing
capacity [H] and distance [D] of destination is given. In next 5 lines, the speed in terms of time
required in minutes and seconds for each kilometer, and heating impact per kilometer is given for
five different fuels.
[Output]
For each test case, print "Case #" followed by test case number. In next line, print the minimum
time (in minutes and seconds) required to reach the destination. If there is no way to reach the
destination, print -1.
[Limits]
4 < D < 41
9 < H < 501
[Sample Input]
3
10 5
215
1 54 5
1 36 3
1 42 1
2 13 2
20 6
3 46 3
194
194
2 52 4
3 13 6
5 10
101
101
101
101
101
[Sample Output]
Case #1
8 18
Case #2
17 22
Case #3
-1
[Explanation]
Case #1
Use PETROL for 2 kms and DIESEL for 3 kms
Case #2
Here CNG and PETROL have same performance, so multiple ways are possible.
Use LPG for 4 kms and CNG for 2 kms
Use LPG for 4 kms and CNG for 1 km and PETROL for 1 km
Use LPG for 4 kms and PETROL for 2 kms
Case #3
There is no way to cover the distance with given heat bearing capacity
SWC07. BusinessMan Chobara [Part 4 of BusinessMan Series]
The BusinessMan went to the neighbouring city to sell the Mangoes using the Pushpak Truck given
by his friend Mr Jugadu. He reaped huge profits in the trade and decided to return back to his city.
The Pushpak truck is not in a condition to support his journey back home. It is hot summer and the
journey requires travelling through deserts where demons called Rakshasas are struggling for means
to survive. These Rakshasas have magnificent power to smell any living being as one enters their
territory. The BusinessMan knows that the Rakshasas will kill him if he goes unprepared.
He gets to know about a special fruit called Sanjeevani Booti that can grow in the desert during
these days. So, if he is able to acquire some fruits, he can use them to save his life when he
encounters a Rakshasa.
He met a renowned sage Baba Bhokal and requested his help. Pleased with him, the baba gave him
a special drink called kadha. Drinking the kadha will suppress his body smell and will make him
practically invisible to the Rakshasas. But the limitation is that the Kadha can be taken only once
during the journey and the effect of Kadha can sustain only for 5 days after drinking.
You are given a matrix of N rows and 5 columns denoting the desert area. The BusinessMan is
standing at Row N+1 and Column 3 in the starting. He can go to either of the 3 cells in next row
[Nth Row and so on] each day namely, front, front left, front right. There is NO overlap between the
first and last column of the matrix. The BusinessMan will pluck the Booti as he founds one on this
path but is also forced to give one Fruit each time he encounters a Rakshasa (if not Rakshasa will
kill and eat him). You need to find out if the BusinessMan can return back to his city by following
the optimum approach, or not. If he can return back, print the maximum number of fruits he could
possibly save. If he cannot return back by following any approach, print -1.
[Input]
First number will be number of test cases present in file
For each test case, the height [H] of matrix is given. Width is constant at 5.
In next H lines, matrix is given. 'S' represents Sanjeevani Booti, 'R' represents Rakshasas and '-'
represents empty region.
[Output]
For each test case, print "Case #" followed by test case number.
In next line, print the maximum fruits the BusinessMan can bring back home. If there is no way to
reach home, print -1.
[Limits]
4 < H < 13
[Sample Input]
3
7
-S-R-
RS-S-
--S--
---S-
S--RR
--RR-
-R-S-
7
RR-R-
S-R--
-R--S
R-SR-
R---R
R-R--
SRR--
SWC08. Plane Game (SWC 15/06/2016)
There is a NxM matrix.
N is 5~12
M is fixed as 5
At a give time only 5 rows can be active.
Each cell can be,
0 Empty
1 Coin
2 Enemy
When we collect a coin, coins count will be increased to one.
If we face an Enemy, coins count will be decreased by one. If we meet the enemy without any coin
(ie -1) Plane will explode and game is over.
Plane is located in at N+1 row and center position of the board. It can be in current position or move
1 position to left or right.
There is only one Bomb available and can be used at any time of the game. The bomb will kill all
the Enemies in the active row.
SWC09. FoneZone (SWC)
Samsung is to start a phone exchange program to replace all old S Series phones with S6. There is a
map in matrix form showing the most sold S phone in each block. 0 denotes the blocks where a
competitor phone is best seller.
10101
22001
Samsung wants to setup exchange centers so that each exchange center covers maximum region for
a particular S series phone. Each such region can overlap competitor regions (shown by 0), but can't
overlap other S series phone region.
Like a map with 110001 requires a single exchange center for S1 phone. A map with 110022 needs
two exchange centers for S1 and S2.
While optimizing for number of the regions, the 0 regions will be grouped with other regions
abiding the following rules -
1) It will group to (multiple) neighbour regions with max size for a S phone
2) If more than one regions with max size, the newer phone will be chosen S5>S4
3) The original map will be used for checking regions sizes every time
Like for first example, there will be 3 centers required, 2 for S1, 1 for S2
You have to find the minimum exchange centers that need to be setup.
[Input]
There can be more than one test case in the input file. The first line has T, the number of test cases.
Then the T test cases are provided in the following lines (T ≤ 10 )
In each test case, in the first line, the size of the 2D view is given in the format N M where (1 ≤ N ≤
100) and (1 ≤ M ≤ 100) is given. N represents number of rows and M represents number of columns
in the array.
The next N lines represents the map in matrix form. Valid values are 1-5. 0 means the non Samsung
brand region.
[Output]
For each test case, in a separate line, print the minimum number of exchange centers Samsung will
require.
[I/O Example]
Input
2
25
10101
22001
67
0110030
0110033
0005040
2205540
2220040
0005540
Output
3
5
SWC10. Robot
Samsung is making a new algorithm for robotic vacuum cleaner. Robot will be given a set of
commands in the form of directions and distances. The robot will move in tracks according to the
given commands. You need to find if there is any intersection between various paths. If any
intersection exists, you need to find the track during which intersections occurs for the first time.
[Input]
First number will be number of test cases present in input file
For each test case, a number denoting number of commands N (1-100) is given
In next N lines, space separated direction and distance values for each command are given. 1/2/3/4
represents North/South/East/West direction respectively. The distance for each individual track can
be from 1 to 100000.
[Output]
For each test case, print "Case #" followed by test case number. In next line, print the track number
during which first intersection happens. If there is no intersection, print -1.
[Sample Input]
2
4
34
12
42
24
5
32
13
44
25
37
[Sample Output]
Case #1
4
Case #2
-1
SWC11. Businessman toll
There is a businessman going through multiple cities doing trade. He starts from his hometown with
only money and does the buying / selling of fruits or pays entry tax throughout his journey. He has
to pass through multiple cities (max 20) during his journey in the given order. At each city he has 3
options -
(1) He pays the entry tax and continue his journey with the existing fruits
(2) He purchases local fruits of quantity Q at double price of entry tax
(3) He trades Q of his exising fruits in lieu of entry tax
He keeps his fruits sealed in a (very big) single sealed bag. As he opens his bag to take out the fruits
to trade for entry tax, all the fruits are affected by the different weather of the city and start to rot. A
fruit can withstand at max 3 adverse weathers exposures. Any fruit left out after 3 exposures will be
a waste and cannot be accepted as a settlement for entry tax at 4th city.
It should be noted that such exposure will not happen when fresh fruits are bought and dropped to
the sealed bag.
You need to find the minimum amount of money the businessman needs to start with from his
home, in order to pass through all the given cities
[Input]
First number will be number of test cases present in file
For each test case, a number denoting count of cities N (2-20) is given
In next N lines, space separated Quantity and Tax values (max 10000) are given
[Output]
For each test case, print "Case #" followed by test case number. In next line, print the amount
required in the beginning of journey.
[Sample Input]
2
2
45 43
42 64
4
46 61
58 14
64 14
21 23
[Sample Output]
Case #1
86
Case #2
103
SWC12. Pipes (TBD)
There is a network of pipes in a city. These pipes need to be checked. To check this, we have an
endoscope, which have a given length. The location from where we have to start with endoscope is
given. We have to put the endoscope from given starting point and find out the maximum number of
pipes which can be checked, using the endoscope. There are 7 types of pipe. Below are the type of
pipes and an example of pipe network:
--
--
-
Input:
1st line will contain number of test cases T; T<=50
M and N – number of Rows and columns of the matrix; 3<M,N<=50
Starting point R(Row index),C(column index); 0<=R,C<=M,N
Length of endoscope L; 1<=L
Starting point will always have some pipe.
Matrix will contain 0 if there is no pipe; else pipe type number will be there
T
MNRCL
Matrix
Example:
1
66226
200000
300000
121224
303003
301226
000000
Output:
You have to print the maximum number of pipes that can be checked
#Case1: 15
#Case2: 13
#Case3: 10
#Case4: 27
#Case5: 23
SWC13. Bipartition the cities (Graph is Bipartite or not) (BFS)
SWC14. Find cycle in a tree (DFS)