The following problem is from the Google code jam 2014 qualification round. Head-over there to read the complete problem description.
The problem description is as follows.
A 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.
We have 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).
This problem is simply finding the intersection elements of the rows chosen by the volunteer in the first, and second arrangements. If the intersection contains a single element, then magician can correctly guess the element. If the intersection contains zero elements, volunteer has cheated. If it contains more than one element, then the magician is bad at arranging the cards.
Here is the C++ code to solve this problem.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <set> | |
#include <vector> | |
using namespace std; | |
int main() | |
{ | |
int t,T; | |
cin >> T; | |
int board[4][4]; | |
set<int> s; | |
int i,j; | |
for( t = 0; t < T; t++) | |
{ | |
int row; | |
s.clear(); //start fresh | |
//read the first selected row and board | |
cin >> row; | |
for( i = 0; i < 4; i++ ) | |
{ | |
for( j = 0; j < 4; j++ ) | |
{ | |
cin >> board[i][j]; | |
} | |
} | |
//insert the the selected row elements into a set | |
for( i = 0; i < 4; i++ ) | |
{ | |
s.insert(board[row-1][i]); | |
} | |
//read the second selected row and board | |
cin >> row; | |
for( i = 0; i < 4; i++) | |
{ | |
for( j = 0; j < 4; j++ ) | |
{ | |
cin >> board[i][j]; | |
} | |
} | |
//find the intersection results into a vector | |
vector<int> result; | |
result.clear(); | |
for( i = 0; i < 4; i++ ) | |
{ | |
if( s.find(board[row-1][i]) != s.end() ) | |
{ | |
result.push_back(board[row-1][i]); | |
} | |
} | |
cout << "Case #" << (t+1) << ": "; | |
//determine the results | |
if( result.size() == 1 ) | |
cout << result[0]; | |
else if( result.size() > 1 ) | |
cout << "Bad magician!"; | |
else | |
cout << "Volunteer cheated!"; | |
cout << endl; | |
} | |
return 0; | |
} |