Shifting Gravity
Tag(s):

## Hard

Problem
Editorial
Analytics

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

SAMPLE INPUT
2
3 a
32     3     3
0     0     1
10    10     8
3 w
10     3    32
10     0    96
5    32    96

SAMPLE OUTPUT
#1
32    6    0
1    0    0
20    8    0
#2
20    3   32
5   32  192
0    0    0

Time Limit: 0,2 sec(s) for each input file.
Memory Limit: 128 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

## CODE EDITOR

Initializing Code Editor...

## This Problem was Asked in

Challenge Name

ALGOMANIAC PRELIMS

OTHER PROBLEMS OF THIS CHALLENGE
• Data Structures > Advanced Data Structures
• Math > Number Theory
• Math > Basic Math
Уведомления

?