Below is the problem statement for 'Magic Trick' problem from the Google Code Jam - 2014:

Recently you went to a magic show. You were very impressed by one of the tricks, so you decided to try to figure out the secret behind it!

The magician starts by arranging 16 cards in a square grid: 4 rows of cards, with 4 cards in each row. Each card has a different number from 1 to 16 written on the side that is showing. Next, the magician asks a volunteer to choose a card, and to tell him which row that card is in.

Finally, the magician arranges the 16 cards in a square grid again, possibly in a different order. Once again, he asks the volunteer which row her card is in. With only the answers to these two questions, the magician then correctly determines which card the volunteer chose. Amazing, right?

You decide to write a program to help you understand the magician's technique. The program will be given the two arrangements of the cards, and the volunteer's answers to the two questions: the row number of the selected card in the first arrangement, and the row number of the selected card in the second arrangement. The rows are numbered 1 to 4 from top to bottom.

Your program should determine which card the volunteer chose; or if there is more than one card the volunteer might have chosen (the magician did a bad job); or if there's no card consistent with the volunteer's answers (the volunteer cheated).

Solving this problem

Usually, Google Code Jam problems have 1 Small input and 1 Large input. This problem has only 1 Small input. Once you have solved the Small input, you have finished solving this problem.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing an integer: the answer to the first question. The next 4 lines represent the first arrangement of the cards: each contains 4 integers, separated by a single space. The next line contains the answer to the second question, and the following four lines contain the second arrangement in the same format.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1).

If there is a single card the volunteer could have chosen, y should be the number on the card. If there are multiple cards the volunteer could have chosen, y should be "Bad magician!", without the quotes. If there are no cards consistent with the volunteer's answers, y should be "Volunteer cheated!", without the quotes. The text needs to be exactly right, so consider copying/pasting it from here.

Limits

1 ≤ T ≤ 100.

1 ≤ both answers ≤ 4.

Each number from 1 to 16 will appear exactly once in each arrangement.

Sample Input

------------

3

2

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

3

1 2 5 4

3 11 6 15

9 10 7 12

13 14 8 16

2

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

2

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

2

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

3

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Sample Ouput

------------

Case #1: 7

Case #2: Bad magician!

Case #3: Volunteer cheated!

For the above problem statement, my Java program parses only the input rows that are required. These 2 rows are stored in an ArrayList

Below is my solution in Java:

And the link to the Github Java Gist: GCJ 2014 - Magic Trick Problem

- GCJ 2014 - 'Deceitful War'
- GCJ 2014 - 'Cookie Clicker Alpha' Problem
- GCJ 2014 - Magic Trick
- GCJ 2013 - Fair And Square
- GCJ 2013 - Tic-Tac-Toe-Tomek
- GCJ 2012 - Recycled Numbers
- GCJ 2012 - Dancing With Googlers
- GCJ 2012 - Speaking In Tongues
- GCJ 2011 - Magicka
- GCJ 2011 - Bot Trust
- GCJ 2010 - Fair Warning
- GCJ 2010 - Snapper Chain
- Odd Man Out - GCJ 2010 (Africa)
- Store Credit - GCJ 2010 (Africa)
- T9Spelling - GCJ 2010 (Africa)
- Reverse Words - GCJ 2010 (Africa)
- All Your Base - GCJ 2009
- Alien Language - GCJ 2009
- Google Code Jam 2008 - Minimum Scalar Product
- GCJ 2008 - Alien Numbers
- Playing With Googol
- Google Billboard Puzzle
- Euler's Number (e) to 10000 digits
- Spiral Walking

- GCJ 2014 - 'Deceitful War'
- GCJ 2014 - 'Cookie Clicker Alpha' Problem
- GCJ 2014 - Magic Trick
- GCJ 2013 - Fair And Square
- GCJ 2013 - Tic-Tac-Toe-Tomek
- GCJ 2012 - Recycled Numbers
- GCJ 2012 - Dancing With Googlers
- GCJ 2012 - Speaking In Tongues
- GCJ 2011 - Magicka
- GCJ 2011 - Bot Trust
- GCJ 2010 - Fair Warning
- GCJ 2010 - Snapper Chain
- Odd Man Out - GCJ 2010 (Africa)
- Store Credit - GCJ 2010 (Africa)
- T9Spelling - GCJ 2010 (Africa)
- Reverse Words - GCJ 2010 (Africa)
- All Your Base - GCJ 2009
- Alien Language - GCJ 2009
- Google Code Jam 2008 - Minimum Scalar Product
- GCJ 2008 - Alien Numbers
- Playing With Googol
- Google Billboard Puzzle
- Euler's Number (e) to 10000 digits
- Spiral Walking

- Recursion Depth - Java
- RESTFul Web Service - Seam Component Injection
- Micro-benchmarking Java Code With Caliper
- JSF & Highcharts (Javascript Chart Library)
- Add 'Syntactic Sugar' With 'Fluent Interface'
- Priority Queue in Java
- Compile Java Files At Runtime
- Missing Number
- Guice - AOP (Aspect Oriented Programming)
- Creating Executable Jar File With Maven Shade Plugin
- Java - Looping Through A Range of BigInteger Values
- Create Your Own Dependency Injection Framework
- Google Guice - Example
- Guice Grapher - Example
- Using Richfaces 'Suggestion Box' As Combo Box
- Hashcode Of A String In Java
- Richfaces - Modal Panel As A Wizard
- Jasper Reports - Thermometer Report
- Jasper Reports - Gantt Chart
- Jasper Reports - Example
- Seam - Load i18n Messages From Database
- Autoboxing In Java & Bug Patterns
- Dont Smack The Stack (Deal With Stack Overflow Exceptions)

The views expressed on this blog are my personal views and do not reflect the views of
my employer or campaigns I am supporting.

All sample code is provided for illustrative purposes only. These examples have not been thoroughly tested under all conditions. The writer therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.

All programs contained herein are provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.

All sample code is provided for illustrative purposes only. These examples have not been thoroughly tested under all conditions. The writer therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.

All programs contained herein are provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.