编写一个程序求解字谜游戏问题

本文探讨如何编写一个程序来解决字谜游戏。通过算法分析和编程技巧,介绍如何高效地处理字谜游戏中的线索,找出可能的答案并优化搜索过程。
问题描述:输入是由一些字母和单词构成的二维数组,目标是找出字谜中的单词,这些单词可以是水平、垂直或沿对角线以任何方向放置。
 编写一个程序求解字谜游戏问题
  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;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值