Reputation: 306
I m trying to do a online problem about finding adjacent sides of a cube .The full question is at http://www.codechef.com/JULY15/problems/CHCUBE. it gives correct answer to me, but when i submit it will get wrong answer.
heres my java code
import java.util.*;
class Cube {
public static void main(String[] args)
{
long T;
int blue = 0,black = 0,red=0,green=0,orange=0,yellow=0;
Scanner input=new Scanner(System.in);
T=input.nextLong();
int pos=0,checked=0,answer=0;
String colors[]=new String[6];
while(T>0){
for(int temp=0;temp<6;temp++)
{
colors[temp]=input.next();
if(colors[temp].equals("blue"))
{
blue++;
if(blue>2)
pos=temp;
}
else if(colors[temp].equals("black"))
{black++;
if(black>2)
pos=temp;
}
else if(colors[temp].equals("yellow"))
{yellow++;
if(yellow>2)
pos=temp;
}
else if(colors[temp].equals("orange"))
{orange++;
if(orange>2)
pos=temp;
}
else if(colors[temp].equals("green"))
{green++;
if(green>2)
pos=temp;
}
else if(colors[temp].equals("red"))
{red++;
if(red>2)
pos=temp;
}
}
if(blue>2||black>2||green>2||yellow>2||red>2||orange>2)
{ if(pos%2==0)
{
checked=(pos+2)%6;
}
else{
checked=(pos+1)%6;
}
if(colors[pos].equals(colors[checked] )||colors[pos].equals(colors[(checked+1)%6]) )
{
if(colors[pos].equals(colors[(checked+3)%6]) ||colors[pos].equals(colors[(checked+2)%6]) )
{
answer++;
}
}
}
if(answer==1)
System.out.println("YES");
else
System.out.println("NO");
T--;
}
}
}
Upvotes: 0
Views: 512
Reputation: 559
I suggest you to model the whole thing as a graph.
A graph is a really flexible data structure, in this case you can also use all algorithms, because the problem size is so little.
The nodes of your graph are the sides, each node has to have
an attribute color
which is representing its color.
Each node also has a list of the adjacent sides( so we are implementing a graph with adjacent lists, not an adjacent matrix).
If you have built the graph you can begin to count adjacent sides with the same color. There are many different approaches, I think in your case it could be good to delete all nodes between not equally colored sides.
After that, you can count all the edges that are left in your graph. Since the graph is an undirected one, you have to divide the result by 2.
Note that this approach will not lead to huge main methods - you can solve and debug the problem much faster.
Upvotes: 1