找到num pad上两个键之间方向的算法?

时间:2011-09-18 08:16:14

标签: puzzle

给出以下方向枚举:

typedef enum {
    DirectionNorth = 0,
    DirectionNorthEast,
    DirectionEast,
    DirectionSouthEast,
    DirectionSouth,
    DirectionSouthWest,
    DirectionWest,
    DirectionNorthWest
} Direction;

与数字键盘类似的数字矩阵:

7 8 9
4 5 6
1 2 3

你如何编写一个函数来返回矩阵中相邻数字之间的方向?说:

1, 2 => DirectionEast
2, 1 => DirectionWest
4, 8 => DirectionNorthEast
1, 7 => undef

如果需要,您可以更改枚举的数值。可读的解决方案首选。 (不是作业,只是我正在处理的应用程序的算法。我有一个工作版本,但我对更优雅的拍摄感兴趣。)

3 个答案:

答案 0 :(得分:3)

我会重新定义枚举中的值,以便北,南,东和西每个取不同的位。

typedef enum {
    undef = 0,
    DirectionNorth = 1,
    DirectionEast = 2,
    DirectionSouth = 4,
    DirectionWest = 8,
    DirectionNorthEast = DirectionNorth | DirectionEast,
    DirectionSouthEast = DirectionSouth | DirectionEast,
    DirectionNorthWest = DirectionNorth | DirectionWest,
    DirectionSouthWest = DirectionSouth | DirectionWest
} Direction;

使用这些新值:

int ax = ( a - 1 ) % 3, ay = ( a - 1 ) / 3;
int bx = ( b - 1 ) % 3, by = ( b - 1 ) / 3;

int diffx = std::abs( ax - bx );
int diffy = std::abs( ay - by );

int result = undef;
if( diffx <= 1 && diffy <= 1 )
{
    result |= ( bx == ax - 1 ) ? DirectionWest : 0;
    result |= ( bx == ax + 1 ) ? DirectionEast : 0;
    result |= ( by == ay - 1 ) ? DirectionSouth : 0;
    result |= ( by == ay + 1 ) ? DirectionNorth : 0;
}
return static_cast< Direction >( result );

更新:最后,我认为现在正确。

答案 1 :(得分:3)

int direction_code(int a, int b)
{
    assert(a >= 1 && a <= 9 && b >= 1 && b <= 9);
    int ax = (a - 1) % 3, ay = (a - 1) / 3,
        bx = (b - 1) % 3, by = (b - 1) / 3,
        deltax = bx - ax, deltay = by - ay;
    if (abs(deltax) < 2 && abs(deltay) < 2)
        return 1 + (deltay + 1)*3 + (deltax + 1);
    return 5;
}

结果代码

1 south-west
2 south
3 south-east
4 west
5 invalid
6 east
7 north-west
8 north
9 north-east

答案 2 :(得分:0)

使用这个数字矩阵,下列情况属实: 1)1(+ ve或-ve)的差异总是暗示方向是东方或西方。 2)相似,北方或南方方向的差异为3。 3)相差4个东北或西南。 4)相差2西北或东南。