-3

这是一个crossword game. 我想对角读取一个数组。我应该在整个二维给定数组中找到一些单词,这个数组从给定文件中读取,它是 n*m 大小;m 并不总是 = n

我怎样才能读取这样对角线给出的 2d:

例子:

m = 4
n = 4
b o o k
z a k o
s l l e
x y z l

ball: found

[b] o o k
z [a] k o
s l [l] e
x y z [l]

foo: not found

这是代码:

char ReadArray(char* array, int r, int c, int n, int m)
{
   return (r > 0 && r <= n && c > 0 && c <= m) ?
                  array[n * (r - 1) + (c - 1)] : '\0';
}
4

1 回答 1

2
char readrc(char* array, int r, int c, int n, int m)
{
   return (r > 0 && r <= n && c > 0 && c <= m) ?
                  array[n * (r - 1) + (c - 1)] : '\0';
}

void read_down_right(char* array, int n, int m, vector<string>& list)
{
   for (int sc = 2 - n; sc <= m - 1; sc++)
   {  
      string str = "";
      for (int r = 1, c = sc; r <= n; r++, c++)
      {
         char chr = readrc(array, r, c, n, m);
         if (chr != '\0')
            str += chr;
      }
      list.push_back(str);
   }
}

void read_down_left(char* array, int n, int m, vector<string>& list)
{
   for (int sc = 2; sc <= m + n - 2; sc--)
   { 
      string str = "";
      for (int r = 1, c = sc; r <= n; r++, c--)
      {
         char chr = readrc(array, r, c, n, m);
         if (chr != '\0')
            str += chr;
      }
      list.push_back(str);
   }
}

每次传递对空白列表的引用。列表包含之后所有可能的字符串,进行线性搜索。

于 2015-07-09T19:23:57.303 回答