PROBLEM LINK:
Author: Antoniuk Vasyl
Tester: Mugurel Ionut Andreica
Editorialist: Lalit Kundu
DIFFICULTY:
CakeWalk
PREREQUISITES:
implementation, basic visualisation
PROBLEM:
One day Chef found a cube which has its each side painted in some color out of black, blue, red, green, yellow or orange colors. Now he asks you to check if he can choose three sides such that they are pairwise adjacent and painted in the same color.
EXPLANATION:
================
Most basic thing to be observed is that three sides in a cube can pairwise share an edge if they also share an corner. For example, in the following image sides 1, 2 and 3 are pairwise adjacent because they are sharing a corner.
So, there are only 8 possible triplets which can give us our answer. Now, easiest way is to hard code these triplets in your program. So, if we follow the indexing method where \textrm{front}: 0, \textrm{back}: 1, \textrm{left}: 2, \textrm{right}: 3, \textrm{top}: 4, \textrm{botton}: 5(note that we follow the indexing method given in problem so that coding is simpler), following 8 triplets denote the corners. Three elements in a triplet denotes the indexes of sides that share this corner.
int adj[8][3] = { {0, 2, 4}, {0, 3, 4}, {0, 2, 5}, {0, 3, 5}, {1, 2, 4}, {1, 3, 4}, {1, 2, 5}, {1, 3, 5}, };
Now, for each query of cube, we just have to take input and then check for all 8 corners if there exists a triplet in which all these indexes have same color.
Following pseudo code provides the solution:
ans = "NO" //initialise answer to NO first color[6]; //color[i] denotes the color of the side denoted by index i for i = 0 to 5: scan color[i] for i=0 to 7: if color[adj[i][0]] == color[adj[i][1]] == color[adj[i][2]]: ans = "YES" print ans
ALTERNATE SOLUTION:
You can also notice that if we follow the indexing as defined above, then faces i, j and k share a corner for all 0 \le i \lt 2 and 2 \le j \lt 4 and 4 \le k \lt 6. Now, this is even more easy to code.
COMPLEXITY:
For each test case, complexity is O(8 + string comparison$)$ which can be assumed as constant time.