CHCUBE - Editorial

Hi, Can you check what’s wrong with this code CodeChef: Practical coding for everyone… Please help me with this one …

What’s wrong with my code? I was getting wrong answer during submission…Please Help!!

#include <bits/stdc++.h>
#include <vector>
using namespace std;
class Cube
{
public:
    void getColors();
    void Test();

private:
    string ptr[6];
};
void Cube::getColors()
{
    cin>>ptr[0]>>ptr[1]>>ptr[2]>>ptr[3]>>ptr[4]>>ptr[5];
    for(int i=0;i<6;++i)
    {
        if(ptr[i]=="black"||ptr[i]=="blue"||ptr[i]=="red"||ptr[i]=="green"||ptr[i]=="yellow"||ptr[i]=="orange");
        else
        {
            cout<<"Wrong Colour, Exiting....";
            exit(1);
        }
    }
}
void Cube::Test()
{
    if((ptr[0]==ptr[2])||(ptr[0]==ptr[3]))
    {
        if((ptr[0]==ptr[4])||(ptr[0]==ptr[5]))
            cout<<"YES";
    }
    else if((ptr[1]==ptr[2])||(ptr[1]==ptr[3]))
    {
        if((ptr[1]==ptr[4])||(ptr[1]==ptr[5]))
            cout<<"YES";
    }
    else
            cout<<"NO";
}
int main()
{
    int T; vector<Cube> ch(1); Cube a;
    cin>>T;
    if(T<1 || T>50000)
    {
        cout<<"Number of test cases are out of available limits, exiting....";
        exit(0);
    }
    for(int i=0;i<T;++i)
    {
        if(i>0)
            ch.push_back(a);
        ch[i].getColors();
    }
    for(int j=0;j<T;++j)
    {
        cout<<endl;
        ch[j].Test();
    }
    return 0;
}

Help , I still can’t figure out why my solution still WA even I have already tested it on ideone and the output is same with the problem statement

Here is the link to the code : http://ideone.com/mDoLO0

@harry30

You have written check = false outside the test cases loop. So once the check flag is set to true (in some test case), there is no way it can go false and will always be true for all the test cases afterwards.

So your output would be something like:

NO
NO
NO
.
.
.
YES
YES
YES
YES
.
.
.
YES

Write that line inside the test cases loop. That would solve the problem.

@apurva_121

Try this test case-

1
blue blue green blue black green

@sinha_u use strcmp to compare strings…

@kislaya123 In your second for loop you try to read six lines, one for each side of the cube. However the problem provides this information on a single line. This is causing an exception to be thrown. Try something like:

String input;

input = inp.readLine();

String[] Sa = input.split(" ");

Also I think you will then get WA because I believe the following case will fail for your code. This should return NO.
1
blue blue green green blue blue

@jaksparo string comparison is perfectly fine.
@sinha_u for the last four conditions replace s[0] by s[1]

Thanks arcticblue …
I changed my code … Could you help me to figure out (why I am getting runtime error as now I am not using string …)Please…

CODE IS AS :---->
/…Above same as previous…/

if(count==3){
if(sum==420||sum==421||sum==430||sum==431||sum==520||sum==521||sum==530||sum==531)
System.out.println(“YES”);
else
System.out.println(“NO”);

@kislaya123 I’m not certain how your code matches up with the original code. Have a look at my submission, it gets accepted. It’s based off your code with my suggested change. It also includes a change to fix the bug I mentioned.

http://www.codechef.com/viewsolution/7478436

If the code still doesn’t make sense then can you post a link to your failing submission and I can have a look at it.

Link are as :–>

Please check it out…
http://www.codechef.com/viewsolution/7363393

I think my code is quite same as yours …Please Check it out…

Your code is different in one important place. You are still assuming each face is input on a separate line. See your code below, this is trying to read a whole line for each face.

String[] Sa=new String[6];

int count=0,sum=0,j;

for(int i=0;i<Sa.length;i++){

Sa[i]=inp.readLine();

}

You need to read in a single line and then split the line into the six faces. Replace the four lines above with the four lines below.

int count=0,sum=0,j;

String input;

input = inp.readLine();

String[] Sa = input.split(" ");

This should fix your run time error.

No. It is possible for there to be three faces with the same colour that aren’t pairwise adjacent. Try both of the test cases below.

2

blue blue green green blue black

blue blue green green blue blue

You can have the top, bottom and front the same colour but then the top and bottom aren’t pairwise adjacent. Every pair must be adjacent and there are three pairs.

Unfortunately there are a number of things wrong with your code. Probably the most confusing one for you is that the example test cases don’t work. The following code is incorrect. You need to account for the null terminator at the end of the string (\0).

char color_array[6],first_color[6],second_color[6];

This is causing the testcases variable to be overwritten with a zero. So when I tried to run the two example test cases your code stopped after the first one.

The second issue is that you only sometimes print out a “YES” or “NO” every line test case needs a yes or no.

Also your logic is incorrect. It’s not enough to check there are three or less colours. The following two test cases have less than three colours but both should give NO as the correct answer.

2

blue blue green green blue black

blue blue green green blue blue

You need to check that the adjacent faces are the same colour.

Have a look at my answer. CodeChef: Practical coding for everyone If the front is one colour then the top and one side need to be the same colour or the bottom and one side need to be the same colour. If this is true then print YES. The same for back.

Hi articblue, Thank you for your reponse… How about this one… CodeChef: Practical coding for everyone This runs fine for the two testcases that you gave. I understand the null terminator issue but isn’t the logic correct for this one? What you have said I have tried to program the same logic i.e

“If the front is one colour then the top and one side need to be the same colour or the bottom and one side need to be the same colour. If this is true then print YES. The same for back.”

Try:

1

blue blue orange blue blue orange

You code gives NO when the correct answer is YES.

Thanks for that. I got the problem. Thanks very much again :slight_smile:

I am getting WA. please tell me the mistake.
#include <bits/stdc++.h>
using namespace std;

int main() {

int t;
cin>>t;
while(t--)
{
    string s[6];
    int i,j,co=0,c1=0,flag=0;
    for(i=0;i<6;i++)
    {
        cin>>s[i];
    }
    for(i=0;i<6;i+=2)
    {
        if(!s[i].compare(s[i+1]))
        c1++;
    }
     for(i=0;i<6;i+=1)
    {  if(i+1<6)
    {
        if(!s[i].compare(s[i+1]))
        flag++;
    }
    }
   for(i=0;i<6;i++)
   {
       for(j=i+1;j<6;j++)
        { 
            if(!s[i].compare(s[j]))
       {
           co++;
        }
            
        }
    }
    if(co>=3)
    {
    if(c1>2 && flag==5)
    cout<<"YES"<<endl;
   else if(c1<2)
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;
    }
    else
    cout<<"NO"<<endl;
}
return 0;

}