2015-06-07 20 views
0

我想了解名爲「Boggle」 的遊戲算法,它找出N * N矩陣中的單詞。「相鄰單元方向X-Y delta對的含義」

#include <cstdio> 
#include <iostream> 

using namespace std; 

const int N = 6; // max length of a word in the board 

char in[N * N + 1]; // max length of a word 
char board[N+1][N+2]; // keep room for a newline and null char at the end 
char prev[N * N + 1]; 
bool dp[N * N + 1][N][N]; 

// direction X-Y delta pairs for adjacent cells 
int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}; 
int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}; 
bool visited[N][N]; 

bool checkBoard(char* word, int curIndex, int r, int c, int wordLen) 
{ 
if (curIndex == wordLen - 1) 
{ 
    //cout << "Returned TRUE!!" << endl; 
    return true; 
} 

int ret = false; 

for (int i = 0; i < 8; ++i) 
{ 
    int newR = r + dx[i]; 
    int newC = c + dy[i]; 

    if (newR >= 0 && newR < N && newC >= 0 && newC < N && !visited[newR]  [newC] && word[curIndex+1] == board[newR][newC]) 

我不明白這個部分:

// direction X-Y delta pairs for adjacent cells 
int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}; 
int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}; 

爲什麼這些陣列有他們的價值和爲什麼這項工作?

回答

1

這些陣列表示從當前的「光標」位置的行和列偏移的可能的組合(這是一個x,y中的代碼作爲變量c座標跟蹤,r):

// direction X-Y delta pairs for adjacent cells 
int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}; 
int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}; 

例如,如果你想象一個3x3的正方形網格,並且把中心框看作當前的位置,那麼其他8個周圍的單元就是那些由這些行和列的偏移量表示的單元格。如果我們在索引2(dx[2] = 1dy[2] = 0)處取得偏移量,則這將指示單元格向下一行(並且向左/向右移動零點)。