2015-06-07 21 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)处取得偏移量,则这将指示单元格向下一行(并且向左/向右移动零点)。