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 » December 2011 Long Contest » Spinning Wheels

Spinning Wheels

Problem code: SPIN

  • All Submissions

All submissions for this problem are available.

Spinning Wheels is a game for one player. It is played on a square grid consisting of N rows and N columns. Each cell contains a wheel, which is colored into one of five colors -- red, orange, yellow, blue or violet (for convenience, we'll number the colors from 1 to 5, correspondingly). Each wheel also has two needles, and the needles are placed in one of the following positions (for convenience, we'll number the positions from 1 to 4, correspondingly):


Basically, position 1 means that the needles are directed up and right, position 2 means that they're directed down and right down, position 3 -- down and left, and position 4 -- up and left.

The goal of the game is to make all the wheels colored into the same color through a sequence of moves. A move consists of choosing a wheel and rotating it 90 degrees clockwise (for example, if the wheel was in position 1, it would change to position 2). After the rotation of some wheel, the state of other wheels (as well as the state of the wheel rotated first) may change.

More formally. Let's denote the color of the wheel chosen on this move by C. We'll say that two wheels are matching if they are situated in side-by-side neighboring cells and either needle of both wheels is directed to the other wheel. The process of changing the grid is separated into iterations. We will maintain set S consisting of the wheels rotated at the last iteration. Before the first iteration, set S contains only the starting wheel. The following algorithm is executed then repeatedly:

  • If set S is empty, stop the process.
  • Rotate each wheel in set S 90 degrees clockwise.
  • Assign color C to each wheel in set S.
  • Form set Q consisting of wheels which have at least one matching wheel in set S.
  • Replace set S with set Q and clear set Q.

It's possible to prove that this process will definitely stop. Note that a wheel may get rotated more than once during the move.

Your task is to win the game. More precisely, for each grid configuration given in input you should either output a winning sequence of moves (no longer than 1000 moves) or output -1 meaning that you'd like to skip this test case (it's essentially the same as outputting a configuration with 1001 moves).

Note. This might help (though the rules are not exactly the same): http://www.cesmes.fi/#boxSpin.

Input

The first line of the input file contains an integer T -- the number of test cases. Then T test cases follow. The first line of each test case contains an integer N. The following N lines contain N integers (between 1 and 4, inclusive) each, giving the initial positions of the wheels in the corresponding cells. The following N give the information about the starting colors (each color between 1 and 5, inclusive) in the same format.

Note. The constraints are listed in the 'Test Case Generation' part of the problem statement.

Output

For each test case output -1 if you would like to skip this test case. Otherwise, output a line containing just one integer K (0 <= K <= 1000) and then K lines each containing a pair of integers r c, describing the corresponding move (1 <= r, c <= N; r is the row ID, c is the column ID).

Scoring

If at least one of the constraints in the 'Output' section is broken, you'll get 'Wrong Answer'. If at least one of your sequences of moves doesn't lead to winning in the corresponding test case (not all wheels are of the same color after performing all the moves), you'll get 'Wrong Answer'. Otherwise, for each test case you'll get a score of 1001 if you output -1, and a score of K if you output a valid sequence of moves. Your score for the input file is the sum of your scores for the particular test cases. Finally, there are 10 input files in this problem, and your overall score is the average of your scores on the input files. Of course, your goal is to minimize your overall score.

Important! You must solve at least one test case in each file, otherwise you'll get 'Wrong Answer'.

Example

Input:
2
3
1 1 1
4 2 4
1 4 2
3 3 2
3 2 1
4 5 4
4
2 2 2 3
1 4 4 3
3 1 3 1
3 2 3 1
3 4 2 3
4 2 1 1
4 3 2 3
4 4 2 2

Output:
3
3 3
2 1
1 3
-1

Explanation:

Your score for this input file is 3+1001 = 1004.

Let's see what happens in the first test case if we apply the rotations from the output. The first two rotations change the state of only one wheel each, as no wheel matches the chosen one after the rotation:


The third rotation starts a process consisting of 14 iterations, as shown below (if we look at the pictures from left to right, from top to bottom; the rotated wheels at each iteration are highlighted):


Now all wheels are of the same color, so we've achieved our goal.

Test Case Generation

There are 10 official input files. Each input file contains exactly 19 test cases. The values of N in the corresponding test cases are listed in the table below:

Test case12345678910111213141516171819
N234567891011121520253035404550

The initial positions and colors of the wheels are chosen randomly and uniformly in the corresponding ranges.


Author: gennady.korotkevich
Date Added: 10-11-2011
Time Limit: 5 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, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

The link helps. Though it is

pragrame @ 1 Dec 2011 06:00 PM
The link helps. Though it is stated that the rules may differ, they seem identical. How exactly do the rules differ? (except possibly for the size of the grid, or number of starting colours...)

ok one difference is, Here,

pragrame @ 1 Dec 2011 06:04 PM
ok one difference is, Here, neighboring means "side-by-side" whereas in the link version, it wraps about the sides (topologically like a cylinder). Any more differences?

i bruteforced every 2x2

cyberax @ 9 Dec 2011 03:03 AM
i bruteforced every 2x2 possible configuration. at least to solve each first cases. still getting WA. is it normal ? or did i miss something obvious in the problem statement ?

why cant i open the link = =

hiphip @ 9 Dec 2011 08:08 PM
why cant i open the link = =

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