《数据结构与算法分析-C语言描述》中的字谜问题

通过将已知单词表中的单词存到Hash表中来提高查找效率

还可以将单词表中单词的前缀也存入Hash表中再提高效率

以下代码仅将单词存入Hash表

#include "HashQuad.h"
#include 
#include 
#include "Fatal.h"

#define MaxWordLen (4)  //已知单词最大长度为4
#define ROW (4) //4行
#define COLUMN (4)  //4列

void LoadWords(FILE*, HashTable);

int
main()
{
    int i, j;
	HashTable WordsTable = InitializeTable(10);
	FILE* fp = fopen("wordsfile.txt","r");
    //文件内容:this two fat that
	if(fp == NULL)
	{
		FatalError("File open failed");
	}
    
    //将单词表录入散列表中
    LoadWords(fp, WordsTable);
    printf("Loding Completed\n");
    
    char grids[COLUMN][ROW] = { 't','h','i','s',
                                'w','a','t','s',
                                'o','a','h','g',
                                'f','g','d','t' };
    
    for(i = 0; i < ROW; i++)
    {
        for(j = 0; j < COLUMN; j++)
        {
            int n = 1;
            char word[MaxWordLen+1];
            word[0] = grids[i][j];
            //1、横向向右
            while((j+n) < COLUMN && n < MaxWordLen)
            {
                word[n] = grids[i][j+n];
                word[++n] = '\0';
                if(!IsEmpty(Find(word, WordsTable), WordsTable))
                {
                    printf("'%s' exist : from (%d,%d) to (%d,%d)\n",word,i+1,j+1,i+1,j+n);
                    break;
                }
            }
            n = 1;
            //2、横向向左
            while((j-n)>-1 && n < MaxWordLen)
            {
                word[n] = grids[i][j-n];
                word[++n] = '\0';
                if(!IsEmpty(Find(word, WordsTable), WordsTable))
                {
                    printf("'%s' exist : from (%d,%d) to (%d,%d)\n",word,i+1,j+1,i+1,j-n);
                    break;
                }
            }
            n = 1;
            //3、纵向向上
            while((i-n)>-1 && n < MaxWordLen)
            {
                word[n] = grids[i-n][j];
                word[++n] = '\0';
                if(!IsEmpty(Find(word, WordsTable), WordsTable))
                {
                    printf("'%s' exist : from (%d,%d) to (%d,%d)\n",word,i+1,j+1,i-n,j+1);
                    break;
                }
            }
            n = 1;
            //4、纵向向下
            while((i+n)-1 && (j-n)>-1 && n < MaxWordLen)
            {
                word[n] = grids[i-n][j-n];
                word[++n] = '\0';
                if(!IsEmpty(Find(word, WordsTable), WordsTable))
                {
                    printf("'%s' exist : from (%d,%d) to (%d,%d)\n",word,i+1,j+1,i-n+2,j-n+2);
                    break;
                }
            }
            n = 1;
            //6、主对角线向下
            while((i+n)-1 && (j+n)-1 && n < MaxWordLen)
            {
                word[n] = grids[i+n][j-n];
                word[++n] = '\0';
                if(!IsEmpty(Find(word, WordsTable), WordsTable))
                {
                    printf("'%s' exist : from (%d,%d) to (%d,%d)\n",word,i+1,j+1,i+n,j-n+2);
                    break;
                }
            }
            
        }
    }
	return 0;
}

void
LoadWords(FILE *fp, HashTable WordsTable)
{
    char c;
    char temp[MaxWordLen+1];
    int i = 0;
    while(1)
    {
        c = fgetc(fp);
        if(isalpha(c))
        {
            temp[i] = c;
            i++;
        }
        else
        {
            temp[i] = '\0';
            Insert(temp, WordsTable);
            i = 0;
            if(c == EOF)
                break;
        }
        
    }
}


你可能感兴趣的:(数据结构与算法学习)