簡體   English   中英

“相鄰單元格的方向XY增量對”的含義

[英]Meaning of “direction X-Y delta pairs for adjacent cells”

我試圖了解一種稱為“ Boggle”的游戲算法,該算法在N * N矩陣中查找單詞。

#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};

為什么這些數組具有它們具有的值,為什么行得通?

這些數組表示與當前“光標”位置(這是在代碼中作為變量cr跟蹤的x,y坐標)的行和列偏移的可能組合:

 // 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 ),則這將指示該單元格向下移動一列(零向左/向右移動)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM