Code Jam Problem : Magic Trick - Solution (C#)

This question was one among the 4 I had to face at the Google Code Jam 2014 Qualification round. I only managed to solve two - Magic Trick (this one) & Cookie Clicker Alpha (read). Although I managed to get qualified with 25 points in total, finishing at 13063 :). This one was the easiest to understand. You can read the problem Statement here. If you are interested you can also refer to Cookie clicker Alpha.


The Problem:

This problem is about a magic trick. It goes like this...
The magician will start with arranging 16 cards in a 4x4 grid. Each card will have a number from 1 to 16 written on them, it must be noted that no number comes twice. Then the magician calls on a volunteer and asks him to select a card and tell the row in which it is located. The magician now rearranges the grid and asks the volunteer to tell the row in which the previously selected card is situated now. By these info the magician will be able to figure out which card the volunteer selected. You can guess how the magician does that - he will reshuffle the grid in such a way that the new arrangement has each of the cards from the row that the volunteer selected in 4 different rows now when the volunteer tells the row the second time he just have to check which card from the previous row is in it. Now the magician in our scenario is not always so tricky - he may not always follow the trick to successfully tell the number the volunteer selected, in this case we will have to output "Bad magician!". And sometimes the volunteer is trickier and he may select a different row that may confuse the magician, in this case we have to output "Volunteer cheated!". We'll just have to check how many of the cards in the previously selected row is in the new selection. If it is just one then  that's our answer. If there are more than one the magician did a pretty bad job. If the newly selected row has none of the cards from the previous selection then it's the volunteer that's bad. So let's get coding...

The Code:

Share this:

, , , , , , , ,

CONVERSATION

1 comments :

  1. Thanks for sharing such a informative post.. from this blog i know about Cookie clicker Alpha. nice blog :-)

    ReplyDelete