Word Search

题意:单词搜索,在一个单词网中搜索单词,该单词可能存在这个网的水平,垂直,或对角线方向。

no solution:有词汇未在方阵图中出现;

ambiguous:词汇在方阵图中出现了两次;

empty solution:所有词汇均出现且仅出现一次,最终方阵图无未标记字母;

此外则依次输出剩余大写字母。

// Solution to Word Search
// Author: Thomas Beuman// Time complexity: O(n*h*w*wordlength)
// Memory: O(h*w)// @EXPECTED_RESULTS@: CORRECT#include <cstdio>
#include <cstring>
#include <string>
using namespace std;const int hmax = 32, wmax = 32, lmax = 32;char Grid[hmax][wmax+1];
char Word[lmax+1];bool Marked[hmax][wmax];
//控制8个方向矩阵
int di[] = {-1,-1,-1,0,0,1,1,1};
int dj[] = {-1,0,1,-1,1,-1,0,1};int main()
{   int runs, run, n, h, w, d, i, j, k, m, s, i2, j2, solutions;bool nosolution, ambiguous, palindrome, empty;scanf("%d", &runs);for (run = 0; run < runs; run++){  // Read inputscanf("%d %d %d", &n, &h, &w);for (i = 0; i < h; i++)scanf("%s", Grid[i]);memset(Marked, false, sizeof(Marked));nosolution = ambiguous = false;// Process the wordsfor (s = 0; s < n; s++){  scanf("%s", Word);m = strlen(Word);//检查是否回文for (k = 0; k < (m-1)/2 && Word[k] == Word[m-1-k]; k++);palindrome = (k == (m-1)/2);solutions = 0;// 尝试所有的开始的位置和矩阵for (i = 0; i < h; i++)for (j = 0; j < w; j++)for (d = 0; d < 8; d++){    for (k = 0; k < m; k++){  i2 = i + k * di[d];j2 = j + k * dj[d];if (i2 < 0 || i2 >= h || j2 < 0 || j2 >= w || Grid[i2][j2] != Word[k])break;}if (k == m) // 查找到单词{   for (k = 0; k < m; k++)Marked[i+k*di[d]][j+k*dj[d]] = true;solutions++;}}if (solutions == 0)nosolution = true;if (!(solutions == 1 || (palindrome && solutions == 2) || (m == 1 && solutions == 8)))ambiguous = true;}// 输出答案if (nosolution)printf("no solution\n");else if (ambiguous)printf("ambiguous\n");else{   // Print unmarked lettersempty = true;for (i = 0; i < h; i++)for (j = 0; j < w; j++)if (!Marked[i][j]){    printf("%c", Grid[i][j]);empty = false;}if (empty)printf("empty solution");printf("\n");}}return 0;
}

转载于:https://www.cnblogs.com/kuroko-ghh/p/9363341.html

J-Word Search相关推荐

  1. 79. Word Search

    leetcod79题在矩阵中寻找某个单词 """ 79. Word Search MediumShare Given a 2D board and a word, fin ...

  2. LeetCode 79. Word Search

    原题链接在这里:https://leetcode.com/problems/word-search/ 题目: Given a 2D board and a word, find if the word ...

  3. [Swift]LeetCode79. 单词搜索 | Word Search

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  4. 【DFS + Backtracking】LeetCode 212. Word Search II

    LeetCode 212. Word Search II Solution1:我的答案 暴力搜索..基于第79题的答案,真的是非常之慢啊!!! 快的方法均是基于字典树的方法,真是复杂.. class ...

  5. 【DFS + Backtracking】LeetCode 79. Word Search

    LeetCode 79. Word Search Solution1:我的答案 DFS + Backtracking class Solution { public:bool exist(vector ...

  6. 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

    解析 max ksubarray sum:  最大和 of 连续子序列 =>   最大和 of  k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff:  无queu ...

  7. 【CODE】Unique Paths Word Search (DFS dp 字典树)

    目录 62. Unique Paths 63. Unique Paths II 980. Unique Paths III 79. Word Search 212. Word Search II 字典 ...

  8. leetcode算法题--Word Search

    原题链接:https://leetcode.com/problems/word-search/ class Solution {public:int n, m;vector<vector< ...

  9. 在矩阵中查找字符串 Word Search

    2019独角兽企业重金招聘Python工程师标准>>> 问题: Given a 2D board and a word, find if the word exists in the ...

  10. 79. Word Search 单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

最新文章

  1. IDEA IntelliJ 如何设置网站的欢迎页面
  2. Corrigendum: A window into third generation sequencing
  3. 数据中心内虚拟机迁移带来的网络技术难题
  4. python能做什么游戏好-Python有做大型游戏的潜力吗?
  5. python批量读取文件夹的图片并处理成模型输入格式
  6. UNIX时间戳及日期的转换与计算
  7. Java 常用缓存淘汰算法解析
  8. 左列定宽,右列自适应
  9. 流拍后,Cerberus 银行木马源代码在黑市免费公开
  10. 第四章——变换域处理方法
  11. No module named ‘wrapt‘
  12. 微信小程序地图导航实例讲解
  13. 杂题 P1640 [SCOI2010]连续攻击游戏
  14. 163邮箱登录界面在哪里?如何登陆163邮箱呢?邮箱163格式怎么写
  15. 亮度、对比度与饱和度
  16. 【IDEA】IDEA常用配置
  17. 费曼技巧:一张白纸提高学习效率
  18. 换元积分法和分部积分法
  19. 创意卡通风格会员日海报
  20. 祝愿大家在新的一年里,万事大吉,送上我设计的可爱暴富小老虎微信红包封面,祝愿大家新的一年财运旺,事业旺,健康旺。

热门文章

  1. spark远程桌面连接:使用机器人平台
  2. c语言一段scanf代码,各位谁有c语言中 scanf 语句的源代码 请发邮箱 wsk456@163.com 小弟不胜感激~谢谢~...
  3. 你BT我更BT!记一次入侵BT服务器
  4. PDF文档电子公章的初试
  5. 安卓智能语音机器人--AlphaGo?你也可以
  6. k8s Service yml 浅薄理解
  7. 【LeetCode】第599题——两个列表的最小索引总和(难度:简单)
  8. Visual Studio 搜索文件快捷键
  9. JAVA 生成同音字的方法,随机生成汉字,汉字转拼音,写的不好,望指正.谢谢~
  10. 计算机实际上是如何工作的