Reputation: 1226
So I was trying to solve uva 11959 Dice. But the third example the question gave give me the wrong answer. Then I found out if I change
cin >> d1 >> d2;
to
scanf("%lx %lx", &d1, &d2);
and it works but I don't know why. However when I submit my code it shows that my answer is wrong. Can anyone help me with both questions?
My code:
#include <iostream>
#include <cstdio>
using namespace std;
//rotate and flip the dice
#define R(d) ( ((d) & 0x000F00) << 12 | ((d) & 0x00000F) << 16 | ((d) & 0x00F0F0)
| ((d) & 0x0F0000) >> 8 | ((d) & 0xF00000) >> 20 )
#define F(d) (((d) & 0x000F0F) | ((d) & 0x00F000) << 8 | ((d) & 0x0000F0) << 12
| ((d) & 0x0F0000) >> 4 | ((d) & 0xF00000) >> 16)
bool E(long d1, long d2)
{
return ( (d1) == (d2) || (d1) == R(d2) || (d1) == R(R(d2)) || (d1) == R(R(R(d2))) );
}
int main()
{
long d1, d2;
long counter;
bool equal;
cin >> counter;
for( int i = 0 ; i < counter ; i++ )
{
scanf("%lx %lx", &d1, &d2);
// cin >> d1 >> d2;
equal = E(d1, d2) || E(d1, F(d2)) || E(d1, F(F(d2)))
|| E(d1,F(F(F(d2)))) ;
if(equal)
cout << "Equal";
else
cout << "Not Equal";
cout << endl;
}
return 0;
}
ADD: I found out in my for loop I need to add two more condition to determine "equal". So it should looks like this:
equal = E(d1, d2) || E(d1, F(d2)) || E(d1, F(F(d2))) || E(d1, F(F(F(d2)))) || E(d1, F(R(d2))) || E(d1, F(R(R(R(d2)))));
But I still dont know why I need to add these two conditions. Isn't it already been covered?
Upvotes: 1
Views: 282
Reputation: 3844
You need the two additional conditions because a six-sided die (cube) can be oriented one of 24 different ways. Your original algorithm was only checking 16 orientations (4 positions in main
times 4 positions in E
= 16) , and would therefore sometimes fail to detect equality. With the two new conditions you have 6 positions in main
times 4 positions in E
, for a total of 24. Note, however, that it isn't immediately obvious that the 24 orientations you generate are all distinct. (I think they probably are, but I didn't take the time to verify it.)
Upvotes: 2
Reputation: 490048
cin >> d1 >> d2;
reads the input as decimal. scanf("%lx %lx", &d1, &d2);
reads the input as hexadecimal.
If you want to read the input as hexadecimal with iostream
, you probably want to use the std::hex
manipulator, something like: cin >> hex >> d1 >> d2;
Note that the hex
bit is "sticky", so it'll remain set until you specify another base. That means the one hex
affects both when you read d1
and d2
above -- but it also means if you read something else afterwards, it'll still read it as hex.
Upvotes: 1