Shifting Gravity

4

1 votes
Hard
Problem

Sheldon likes to play video games.Recently he was playing Limbo. One day he thought of making a video game like it.He being a physicist wants to make a game in which given a grid of elements [ Here numbers ] a gravitational field will be applied along a particular direction and all the elements will fall along the applied field's direction. But he doesn't know how to code it. You are a Good Programmer. so Sheldon asked for your help to code the game for him.
He will give you particular states of the game and will provide you the direction of the field [ "w" - > up , "a" - >left , "s" - > down , "d" - > right ]. A particular state of the game is described by a 2D array of numbers in the range [ 0 , 99 ] . while sliding if two similar numbers collide they will merge and become one number having double the value [ like 2 and 2 will collide and make 4 ] . zero means no elements.

Input Format:
----------------------------------
T
{
N dir
Game grid
}

Input Description:
You are given T Game states range of T is [ 1 , 200 ] dir is direction which could be anything of [ w , a , s , d ] size of each Game state is N x N range of N is [ 1 , 50 ]
your output should be in the format shown below
#TestCaseNumber
the grid of numbers
output the grid numbers in the format shown
for C the print in the format printf("%4d ",grid[i][j])
Here 4 denotes the total width
For other languages use that language specific syntax. for more than 2 occurrences of adjacent same numbers like in [ 0 2 2 2 2] , when moved right the row should become [0 0 0 4 4] as first the 4th and 5th one combines and becomes 4 and then 2nd and 3rd one combines and becomes 4 . And the joining should happen only once for any element

Time Limit: 0.2
Memory Limit: 128
Source Limit:
Editor Image

?