这个版本出炉比较快,还yeah乎着呢,呵呵.

思想,就是,记录下单词的所有前缀,这样可以减少扫描次数.

我的实现方式,新建一个文件存放所有单词的所有前缀,之后同单词一起装进表中.添加一个指示域来指示表中的字符串是单词还是前缀.之后在主循环中先进行单词判断,而后进行前缀判断.如果当前的字符串不是单词并且也未作为前缀出现在表中,那么就跳出当前方向的循环.

/* 5-13(b)-10-28-21.17.c -- 第五章第十三题 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define NEITHER 0 #define WORD 1 #define PREFIX 2 #define ROW 4 #define COLS 4 #define NUL '/0' #define PF '/n' #define CONSTANT 5 #define TABLESIZE 107 /* 数据类型定义 */ typedef struct cell { char * word ; int word_or_prefix ; } Cell ; typedef struct hashtable { int size ; Cell * cell ; } * HashTable ; /* ADT声明 */ long int Hash (const char * word, const int size) ; int InitializeTable (HashTable * const ph, const int size) ; Cell * Find (const HashTable * const ph, const char * const word) ; int Insert (const HashTable * const ph, const char * word, const int mode) ; void Release (const HashTable * const ph) ; int leaner (const int i) ; /* 局部函数声明 */ int main (void) ; char * eat_enter (char * const word) ; int either_word_or_prefix (const HashTable * const ph, const char * const word) ; int is_prefix (const HashTable * const ph, const char * const word) ; int main (void) { HashTable h ; FILE * fp ; char riddle[ROW][COLS] = { "this", "wats", "oahg", "fgdt" } ; char word[ROW * COLS] ; int size = TABLESIZE, lenth = ROW * COLS ; int i, j, i_max, j_max, i_temp, j_temp, w ; // 将一个词典读入表中 InitializeTable (&h, size) ; fp = fopen ("word.txt", "r") ; while (fgets (word, lenth, fp) != NULL) { strcpy (word, eat_enter (word)) ; if (0 == Insert (&h, word, WORD)) puts ("Insert failed") ; } fclose (fp) ; // 将词典中单词的所有前缀读入表中 fp = fopen ("prefix.txt", "r") ; while (fgets (word, lenth, fp) != NULL) { strcpy (word, eat_enter (word)) ; if (0 == Insert (&h, word, PREFIX)) puts ("Insert failed") ; } fclose (fp) ; // 开始测试 for (i = 0, i_max = ROW; i < i_max; i++) { for (j = 0, j_max = COLS; j < j_max; j++) { // 左 for (j_temp = j, w = 0; j_temp >= 0; j_temp--, w++) { word[w] = riddle[i][j_temp] ; word[w + 1] = NUL ; if (WORD == either_word_or_prefix (&h, word)) printf ("%s/n", word) ; else if (NEITHER == either_word_or_prefix (&h, word)) break ; } // 右 for (j_temp = j, w = 0; j_temp < j_max; j_temp++, w++) { word[w] = riddle[i][j_temp] ; word[w + 1] = NUL ; if (WORD == either_word_or_prefix (&h, word)) printf ("%s/n", word) ; else if (NEITHER == either_word_or_prefix (&h, word)) break ; } // 上 for (i_temp = i, w = 0; i_temp >= 0; i_temp--, w++) { word[w] = riddle[i_temp][j] ; word[w + 1] = NUL ; if (WORD == either_word_or_prefix (&h, word)) printf ("%s/n", word) ; else if (NEITHER == either_word_or_prefix (&h, word)) break ; } // 下 for (i_temp = i, w = 0; i_temp < i_max; i_temp++, w++) { word[w] = riddle[i_temp][j] ; word[w + 1] = NUL ; if (WORD == either_word_or_prefix (&h, word)) printf ("%s/n", word) ; else if (NEITHER == either_word_or_prefix (&h, word)) break ; } // 左上 for (i_temp = i, j_temp = j, w = 0; i_temp >= 0 && j_temp >= 0; i_temp--, j_temp--, w++) { word[w] = riddle[i_temp][j_temp] ; word[w + 1] = NUL ; if (WORD == either_word_or_prefix (&h, word)) printf ("%s/n", word) ; else if (NEITHER == either_word_or_prefix (&h, word)) break ; } // 右上 for (i_temp = i, j_temp = j, w = 0; i_temp >= 0 && j_temp < j_max; i_temp--, j_temp++, w++) { word[w] = riddle[i_temp][j_temp] ; word[w + 1] = NUL ; if (WORD == either_word_or_prefix (&h, word)) printf ("%s/n", word) ; else if (NEITHER == either_word_or_prefix (&h, word)) break ; } // 左下 for (i_temp = i, j_temp = j, w = 0; i_temp < i_max && j_temp >= 0; i_temp++, j_temp--, w++) { word[w] = riddle[i_temp][j_temp] ; word[w + 1] = NUL ; if (WORD == either_word_or_prefix (&h, word)) printf ("%s/n", word) ; else if (NEITHER == either_word_or_prefix (&h, word)) break ; } // 右下 for (i_temp = i, j_temp = j, w = 0; i_temp < i_max && j_temp < j_max; i_temp++, j_temp++, w++) { word[w] = riddle[i_temp][j_temp] ; word[w + 1] = NUL ; if (WORD == either_word_or_prefix (&h, word)) printf ("%s/n", word) ; else if (NEITHER == either_word_or_prefix (&h, word)) break ; } } } Release (&h) ; return 0 ; } char * eat_enter (char * const word) { int count = 0; while (word[count] != PF) { count++ ; if (NUL == word[count]) return word ; } word[count] = NUL ; return word ; } int either_word_or_prefix (const HashTable * const ph, const char * const word) { Cell * cell ; cell = Find (ph, word) ; if (cell -> word && 0 == strcmp (cell -> word, word)) { if (WORD == cell -> word_or_prefix) return WORD ; else if (PREFIX == cell -> word_or_prefix) return PREFIX ; } return NEITHER ; } /* ADT定义 */ long int Hash (const char * word, const int size) { long int hash_value = 0 ; while (*word != PF && *word != NUL) hash_value = (hash_value << CONSTANT) + *word++ ; return hash_value % size ; } int InitializeTable (HashTable * const ph, const int size) { int temp ; *ph = (struct hashtable *) malloc (sizeof (struct hashtable)) ; if (NULL == *ph) { puts ("Out of space.[1]") ; return 0 ; } temp = sizeof (Cell) ; (*ph) -> cell = (Cell *) calloc (temp, size) ; if (NULL == (*ph) -> cell) { puts ("Out of space.[2]") ; free (*ph) ; return 0 ; } (*ph) -> size = size ; return 1 ; } Cell * Find (const HashTable * const ph, const char * const word) { long int hash_value ; int size, i, index ; size = (*ph) -> size ; i = 0 ; do { hash_value = Hash (word, size) ; index = (hash_value + leaner (i++)) % size ; } /* 数据域不为空并且字符串不相同 */ while ((*ph) -> cell[index].word != NULL && strcmp (word, (*ph) -> cell[index].word) != 0) ; return (*ph) -> cell + index ; } int Insert (const HashTable * const ph, const char * word, const int mode) { Cell * cell ; int size = (*ph) -> size ; // 不添加换行符 if (PF == *word) return 0 ; cell = Find (ph, word) ; if (NULL == cell -> word) { cell -> word = (char *) malloc (sizeof (char)) ; if (NULL == cell -> word) { puts ("out of space.[4]") ; return 0 ; } strcpy (cell -> word, word) ; cell -> word_or_prefix = mode ; return 1 ; } /* 不重复添加 */ else if (0 == strcmp (cell -> word, word)) return 1 ; return 0 ; } void Release (const HashTable * const ph) { free ((*ph) -> cell) ; free (*ph) ; } int leaner (const int i) { return i ; }

字谜游戏(b)C语言相关推荐

  1. 数据结构猜字谜游戏(Java语言编写)

    前几年买的一本数据结构与算法分析的书籍, 偶然看到以前有很多题目没有做, 现在抽空来看一下. 题目:输入是由一些字母构成的一个二维数组以及一组单词组成.目标是要找出字谜中的单词, 这些单词可能是水平. ...

  2. 一步一步实现扫雷游戏(C语言实现)(三)

    使用WIN32API连接窗口 此项目相关博文链接 一步一步实现扫雷游戏(C语言实现)(一) 一步一步实现扫雷游戏(C语言实现)(二) 一步一步实现扫雷游戏(C语言实现)(三) 一步一步实现扫雷游戏(C ...

  3. 选择问题 and 字谜游戏问题

    #include <stdio.h> #include <stdlib.h> // 第一题 // 找出N个数的第k个最大者 // 方法1:排序(冒泡),降序找出第k个值 // ...

  4. 飞机游戏在C语言程序的基本语句能完成功能的体会

    论飞机游戏在C语言程序的基本语句能完成功能的体会 一.题目:  射击类飞机游戏 二.目的:  通过c语言编写一个射击类的打飞机小游戏,可以通过键盘来进行游戏,操作方法是"a"&qu ...

  5. C语言习题:猜数字游戏(C语言随机数)

    C语言习题:猜数字游戏(C语言随机数) 需要了解C语言随机数的生成方法: 在C语言中比较常用的随机函数是 rand 函数,它可以随机的产生 0 ~ rand_max 的随机数,定义类型不同最大值也不同 ...

  6. java赛马游戏,用JAVA语言实现赛马游戏

    用JAVA语言实现赛马游戏 用JAVA语言实现赛马游戏 在大二下半学期,学校举办了一场编程比赛.其中一个选题就是:用JAVA语言实现一个赛马游戏程序.此程序即为当时的参赛完成的程序. 程序界面风格在当 ...

  7. 游戏夜读 | 写游戏用什么语言?

    写几句题记 这个标题如果用英文表达,可能更贴切,比如:Beginning game programming,What language should I use? 用简洁的中文似乎表达不出重点. 之所 ...

  8. c语言小游戏代码(c语言小游戏代码简单)

    c语言编写小游戏请提供俄罗斯方块,坦克大战之类的小游戏的程序的c 应该是做出方块函数 然后以 这个方块 为单位 绘制 俄罗斯方块的 积木图形 ,在制作游戏界面的时候 也以方块长度为单位长度绘制 二维数 ...

  9. 为什么不可以使用哈曼顿距离_用Python计算8字谜游戏中的曼哈顿距离

    我试图用Python编写一个简单的a*解算器,用于一个简单的8字谜游戏. 我用这种方式代表了我比赛的目标:goal = [[1, 2, 3], [8, 0, 4], [7, 6, 5]] 我的问题是, ...

  10. c语言迷宫闯关游戏大全,C语言写出的迷宫闯关游戏代码.doc

    C语言写出的迷宫闯关游戏代码 C语言写出的迷宫闯关游戏代码: #include #include #define LEFT 75 #define RIGHT 77 #define UPPER 72 # ...

最新文章

  1. iOS:自己写的一个星级评价的小Demo
  2. 在你的 Rails App 中开启 ETag 加速页面载入同时节省资源
  3. 洛谷 P1034 矩形覆盖
  4. GDB调试基础操作详解【GDB调试】
  5. 《那些年啊,那些事——一个程序员的奋斗史》——60
  6. SAP CDS view里,什么时候用left join,什么时候用association
  7. 斐讯k1潘多拉专版固件_斐讯K1刷专版潘多拉固件以及教程(使用感受)
  8. linux安装包管理(未完待续)
  9. GitHub上已超过2900星!这份有原理、有代码、有Demo的算法资源火了
  10. SQL - 通过某个字段名称找到数据库中对应的表
  11. 接口测试工具--apipost如何取消json参数中转义字符
  12. java sdk他edk de区别_最低SDK版本/目标SDK版本与编译SDK版本之间有什么区别?
  13. Photoshop CS2序列号大全 官方免费密钥
  14. ios开发人员行为准则_如何成为iOS开发人员
  15. 华为手机刷机功能总结
  16. 7-3 前序序列创建二叉树 (25 分) PTA
  17. android手机怎么投屏到电视盒子,手机怎么投屏到电视?原来这么简单
  18. 【初创期】企业的安全建设之路到底有多难?
  19. Python如何查询版本号
  20. vs code下载慢的解决方法

热门文章

  1. Linux内核崩溃转存,Ubuntu 12.04 配置内核崩溃自动重启及转存
  2. Hdu--5064(DP)
  3. Pygame做一期吃豆子游戏
  4. C++最简单的日期计算
  5. P1713 麦当劳叔叔的难题(90分)
  6. 网络安全知识竞赛选择题(91-120题)
  7. 遇见心想事成的自己……
  8. FLINK任务重启 Streaming File Sink落地hdfs的中间状态In-progress格式文件处理方案
  9. 百度地图API学习 - 点击地图显示为中心点
  10. C++ 万年历、生肖判断、计算第几天