问题描述:输入是由一些字母和单词构成的二维数组,目标是找出字谜中的单词,这些单词可以是水平、垂直或沿对角线以任何方向放置。
编写一个程序求解字谜游戏问题
t h i s 找出 this、two、fat、that
w a t s
o a h g
f g d t
#include <stdio.h>
#include <string.h>
#define alphaTableMax (4)
typedef enum tableDir{LEFT = 0, RIGHT, UP, DOWN, LEFTUP, RIGHTUP, LEFTDOWN, RIGHTDOWN} tableDir;
typedef struct tableAddr
{
int row;
int colum;
}tableAddr;
static char alphaTable[alphaTableMax][alphaTableMax] = { {'t', 'h', 'i', 's'},
{'w', 'a', 't', 's'},
{'o', 'a', 'h', 'g'},
{'f', 'g', 'd', 't'}};
int findLeftLetter(const char* word, int row, int colum, int letterNum, tableAddr alphaAddr[])
{
if (alphaTable[row][colum - letterNum] == word[letterNum])
{
alphaAddr[letterNum].row = row;
alphaAddr[letterNum].colum = colum - letterNum;
return 0;
}
return -1;
}
int findRightLetter(const char* word, int row, int colum, int letterNum, tableAddr alphaAddr[])
{
if (alphaTable[row][colum + letterNum] == word[letterNum])
{
alphaAddr[letterNum].row = row;
alphaAddr[letterNum].colum = colum + letterNum;
return 0;
}
return -1;
}
int findUpLetter(const char* word, int row, int colum, int letterNum, tableAddr alphaAddr[])
{
if (alphaTable[row - letterNum][colum] == word[letterNum])
{
alphaAddr[letterNum].row = row - letterNum;
alphaAddr[letterNum].colum = colum;
return 0;
}
return -1;
}
int findDownLetter(const char* word, int row, int colum, int letterNum, tableAddr alphaAddr[])
{
if (alphaTable[row + letterNum][colum] == word[letterNum])
{
alphaAddr[letterNum].row = row + letterNum;
alphaAddr[letterNum].colum = colum;
return 0;
}
return -1;
}
int findLeftUpLetter(const char* word, int row, int colum, int letterNum, tableAddr alphaAddr[])
{
if (alphaTable[row - letterNum][colum - letterNum] == word[letterNum])
{
alphaAddr[letterNum].row = row - letterNum;
alphaAddr[letterNum].colum = colum - letterNum;
return 0;
}
return -1;
}
int findRightUpLetter(const char* word, int row, int colum, int letterNum, tableAddr alphaAddr[])
{
if (alphaTable[row - letterNum][colum + letterNum] == word[letterNum])
{
alphaAddr[letterNum].row = row - letterNum;
alphaAddr[letterNum].colum = colum + letterNum;
return 0;
}
return -1;
}
int findLeftDownLetter(const char* word, int row, int colum, int letterNum, tableAddr alphaAddr[])
{
if (alphaTable[row + letterNum][colum - letterNum] == word[letterNum])
{
alphaAddr[letterNum].row = row + letterNum;
alphaAddr[letterNum].colum = colum - letterNum;
return 0;
}
return -1;
}
int findRightDownLetter(const char* word, int row, int colum, int letterNum, tableAddr alphaAddr[])
{
if (alphaTable[row + letterNum][colum + letterNum] == word[letterNum])
{
alphaAddr[letterNum].row = row + letterNum;
alphaAddr[letterNum].colum = colum + letterNum;
return 0;
}
return -1;
}
void findLetter(const char* word, int len, int row, int colum, tableDir dir)
{
int i = 0, pass = 0;
tableAddr alphaAddr[alphaTableMax];
int (*findLetterFunc[])(const char*, int, int, int, tableAddr*)
= { findLeftLetter, findRightLetter, findUpLetter, findDownLetter,
findLeftUpLetter, findRightUpLetter, findLeftDownLetter, findRightDownLetter };
alphaAddr[0].row = row;
alphaAddr[0].colum = colum;
for (i = 1; i < len; i++)
{
if (findLetterFunc[dir](word, row, colum, i, alphaAddr) != 0)
{
pass = -1;
break;
}
}
if (pass == 0)
{
for (i = 0; i < len; i++)
{
printf("%c [%d][%d]", word[i], alphaAddr[i].row, alphaAddr[i].colum);
}
printf("\n");
}
return;
}
void findWordLetter(const char* word, int len, int row, int colum)
{
int distance = 0;
/*向左查找*/
if (colum + 1 >= len)
{
findLetter(word, len, row, colum, LEFT);
}
/*向右查找*/
if (alphaTableMax - colum >= len)
{
findLetter(word, len, row, colum, RIGHT);
}
/*向上查找*/
if (row + 1 >= len)
{
findLetter(word, len, row, colum, UP);
}
/*向下查找*/
if (alphaTableMax - row >= len)
{
findLetter(word, len, row, colum, DOWN);
}
/*向左上对角线查找*/
distance = (row > colum)? colum + 1 : row + 1;
if (distance >= len)
{
findLetter(word, len, row, colum, LEFTUP);
}
/*向右上对角线查找*/
distance = (row + 1 > alphaTableMax - colum) ? alphaTableMax - colum : row + 1;
if (distance >= len)
{
findLetter(word, len, row, colum, RIGHTUP);
}
/*向左下对角线查找*/
distance = (colum + 1 > alphaTableMax - row) ? alphaTableMax - row : colum + 1;
if (distance >= len)
{
findLetter(word, len, row, colum, LEFTDOWN);
}
/*向右下对角线查找*/
distance = (alphaTableMax - colum > alphaTableMax - row) ? alphaTableMax - row : alphaTableMax - colum;
if (distance >= len)
{
findLetter(word, len, row, colum, RIGHTDOWN);
}
return;
}
void findWord(const char* word)
{
int len = 0, i = 0, j = 0;
if (word == NULL)
{
printf("Word point is NULL!\n");
return;
}
len = strlen(word);
if (len > alphaTableMax)
{
printf("Word: \"%s\" with too many letters!\n", word);
return;
}
for (i = 0; i < alphaTableMax; i++)
{
for (j = 0; j < alphaTableMax; j++)
{
if (word[0] == alphaTable[i][j] && len == 1) //查找单词首字母
{
printf("%c [%d][%d]", word[0], i, j);
}
else if((word[0] == alphaTable[i][j]) && len > 1)
{
findWordLetter(word, len, i, j);//
}
}
}
}
int main(void)
{
char* pDict[] = { "that", "this", "two", "fat" };
findWord(pDict[0]);
findWord(pDict[1]);
findWord(pDict[2]);
findWord(pDict[3]);
return 0;
}