CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » May 2010 Contest » Rotation Puzzle

Rotation Puzzle

Problem code: STORO

  • All Submissions

All submissions for this problem are available.

The sample Input/Output in the problem statement has been updated.

The constraints on M, N have been updated.

There was a problem with the scoring. The scoring has been updated (from K*/K to K*/K+1) and solutions have been re-judged.

Recently, with the release of the ultimate computing device bytePad, a platform game with the simple name "Rotation Puzzle" immediately became a phenomenon among Bytelandians. The game is extremely simple, yet quite additive. Here's the rule:

Given a MxN rectangular grid in which each cell contains a unique number from 1 to MxN. In each step, the player can pick any 2x2 subgrid and perform a rotation (whether clockwise or counterclockwise).

The task is to transform from the initial grid to the final configuration, using as few steps as possible.

The final configuration is the configuration

1 2 ... n
n+1 n+2 ... 2n
... ... ... ...
(m-1)n+1 (m-1)n+2 ... mn

You may have guessed why this game is addictive: it requires a tremendous visualization skill!

Input

The first line contains a number T (about 5000), which is the number of test cases. Each test case has the following form.

The first line contains two numbers M and N (2 <= M, N <= 34).

The next M lines contains the description of the grid.

Each test case's input is separated by a blank line.

It is guaranteed that each input data has a solution.

Output

For each test case, output the result in the following format.

The first line contains a number K, the number of steps you need to solve the puzzle. K must not exceed 10000.

Each line of the next K lines contain three numbers c, i, j (c=0 or c=1, 1<=I < M, 1 <= J < N). (i,j) is the top-left coordinate of the 2x2 square that is need to be rotated. c=1 if the rotation if clockwise and c=0 if the rotation is counter-clockwise.

Prints a blank link after each test case's output.

Scoring

Given m and n, we pick a random positive integer K* and starting from the final configuration, we perform a random operation K* times to generate the test case. For each test case, your score is K*/K+1.

Example

Input:
2

2 3
1 5 2
4 6 3

2 3
5 6 2
1 4 3

Output:
1
0 1 2

2
1 1 1
0 1 2


Author: duc_admin
Date Added: 6-04-2010
Time Limit: 7 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

If M comes before N as

zxf_sheep @ 3 May 2010 02:43 PM

If M comes before N as mentioned in input description, why are there 2 lines following in the sample but not 3 lines?

The sample input/output is

admin @ 3 May 2010 09:20 PM

The sample input/output is incorrect. But the Test data is fine. The sample input output will be updated shortly. Sorry of the inconvenience caused.

if the output of a test case,

pigoneand @ 5 May 2010 02:01 PM

if the output of a test case,  K exceed 10000, will it be judged as wrong answer ?

There was an issue with the

admin @ 6 May 2010 09:57 PM

There was an issue with the size of the test cases. The problem has been re-judged. For those who do not have yet got an accepted solution please re-submit your solutions. Inconvenience is deeply regretted.

Should the scoring read

pieguy @ 6 May 2010 11:30 PM

Should the scoring read K*/(K+1)?

@David: Yes. It should read,

kesh_adm @ 6 May 2010 11:43 PM

@David: Yes. It should read, K*/(K+1)

Why I kept recieving Runtime

TMALBONPH @ 7 May 2010 12:40 AM

Why I kept recieving Runtime Error?

umm... can i get a

shettynamit @ 11 May 2010 04:58 PM

umm... can i get a clarification. If the value of K that we output is GREATER than 10000 for some test case, will it result in wrong answer or Runtime Error ?

Two beautiful simple problems

sppraveen @ 12 May 2010 03:09 PM

Two beautiful simple problems - Rotation puzzle and lights out and I absolutely dont have any clue in solving them. Editorial pls

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com