1

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?

4

2 回答 2

2

您需要这两个附加条件,因为六面模具(立方体)可以以 24 种不同方式之一定向。您的原始算法仅检查 16 个方向(4 个位置main乘以 4 个位置E= 16),因此有时无法检测到相等性。在这两个新条件下,您有 6 个位置main乘以 4 个位置E,总共 24 个。但是请注意,您生成的 24 个方向并不是立即明显的。(我认为他们可能是,但我没有花时间验证它。)

于 2013-09-05T07:14:57.290 回答
1

cin >> d1 >> d2;将输入读取为十进制。scanf("%lx %lx", &d1, &d2);将输入读取为十六进制。

如果您想使用 将输入读取为十六进制iostream,您可能需要使用std::hex操纵器,例如:cin >> hex >> d1 >> d2;

请注意,该hex位是“粘性的”,因此它将保持设置,直到您指定另一个基数。这意味着hex当您阅读时d1以及d2以上内容都会影响 - 但它也意味着如果您之后阅读其他内容,它仍会以十六进制读取。

于 2013-09-05T05:51:13.327 回答