F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Manipulating the Power Square

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 308    Accepted Submission(s): 128


Problem Description
Rosalina finds Super Mario puzzling over the Power Square and gives him another hint on how to unlock its power. ĦħKeep swapping 0 with one of its neighbors! IĦŻll tell you which neighbor,Ħħ Rosalina says to Mario. Rosalina then gives Mario a sequence of directions on which neighbor to swap with 0.
 

Input
Input is a description of of the Power Square, followed by a number of commands. The first line is the size of the Power Square n. You may assume n<=100. The second line contains the n2 values in the Power Square, separated by spaces. Values start from the top left corner and move from left to right, moving down one row to the leftmost position when a row is filled.
Following the Power Square description are a number of commands, with each command on a separate line. Each command begins with the name of the command, followed by any additional command parameters.
There will no more than 100 commands.
 

Output
The command ĦħSHOWĦħ causes the current state of the Power Square to be displayed in n ĦÁ n
form (each row of n values on a single line, separated by spaces), followed by a blank line.
The command ĦħMOVEĦħ is followed by one more more moves: ĦħupĦħ, ĦħdownĦħ, ĦħleftĦħ, or ĦħrightĦħ.
Each move is executed as follows:
¨C For ĦħupĦħ, swap 0 with its neighbor above.
¨C For ĦħleftĦħ, swap 0 with its left neighbor.
¨C For ĦħrightĦħ, swap 0 with its right neighbor.
¨C For ĦħdownĦħ, swap 0 with its neighbor below.
If move attempts to swap 0 with a non-existent neighbor, then output ĦħFAILEDĦħ on a single line and stop attempting the remaining moves. If the move succeeds, ĦħMOVEDĦħ is output on a single line. In either case, the state of the Power Square is changed to reflect the moves made.
 

Sample Input
3 8 7 6 5 4 3 2 1 0 SHOW MOVE up SHOW 3 8 7 6 5 4 3 2 1 0 SHOW MOVE up right
 

Sample Output
8 7 6 5 4 3 2 1 0 MOVED 8 7 6 5 4 0 2 1 3 8 7 6 5 4 3 2 1 0 FAILED
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 22:06:59, Gzip enabled