J-Word Search
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相关推荐
- 79. Word Search
leetcod79题在矩阵中寻找某个单词 """ 79. Word Search MediumShare Given a 2D board and a word, fin ...
- LeetCode 79. Word Search
原题链接在这里:https://leetcode.com/problems/word-search/ 题目: Given a 2D board and a word, find if the word ...
- [Swift]LeetCode79. 单词搜索 | Word Search
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 【DFS + Backtracking】LeetCode 212. Word Search II
LeetCode 212. Word Search II Solution1:我的答案 暴力搜索..基于第79题的答案,真的是非常之慢啊!!! 快的方法均是基于字典树的方法,真是复杂.. class ...
- 【DFS + Backtracking】LeetCode 79. Word Search
LeetCode 79. Word Search Solution1:我的答案 DFS + Backtracking class Solution { public:bool exist(vector ...
- 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连
解析 max ksubarray sum: 最大和 of 连续子序列 => 最大和 of k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff: 无queu ...
- 【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 字典 ...
- leetcode算法题--Word Search
原题链接:https://leetcode.com/problems/word-search/ class Solution {public:int n, m;vector<vector< ...
- 在矩阵中查找字符串 Word Search
2019独角兽企业重金招聘Python工程师标准>>> 问题: Given a 2D board and a word, find if the word exists in the ...
- 79. Word Search 单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...
最新文章
- IDEA IntelliJ 如何设置网站的欢迎页面
- Corrigendum: A window into third generation sequencing
- 数据中心内虚拟机迁移带来的网络技术难题
- python能做什么游戏好-Python有做大型游戏的潜力吗?
- python批量读取文件夹的图片并处理成模型输入格式
- UNIX时间戳及日期的转换与计算
- Java 常用缓存淘汰算法解析
- 左列定宽,右列自适应
- 流拍后,Cerberus 银行木马源代码在黑市免费公开
- 第四章——变换域处理方法
- No module named ‘wrapt‘
- 微信小程序地图导航实例讲解
- 杂题 P1640 [SCOI2010]连续攻击游戏
- 163邮箱登录界面在哪里?如何登陆163邮箱呢?邮箱163格式怎么写
- 亮度、对比度与饱和度
- 【IDEA】IDEA常用配置
- 费曼技巧:一张白纸提高学习效率
- 换元积分法和分部积分法
- 创意卡通风格会员日海报
- 祝愿大家在新的一年里,万事大吉,送上我设计的可爱暴富小老虎微信红包封面,祝愿大家新的一年财运旺,事业旺,健康旺。
热门文章
- spark远程桌面连接:使用机器人平台
- c语言一段scanf代码,各位谁有c语言中 scanf 语句的源代码
请发邮箱 wsk456@163.com
小弟不胜感激~谢谢~...
- 你BT我更BT!记一次入侵BT服务器
- PDF文档电子公章的初试
- 安卓智能语音机器人--AlphaGo?你也可以
- k8s Service yml 浅薄理解
- 【LeetCode】第599题——两个列表的最小索引总和(难度:简单)
- Visual Studio 搜索文件快捷键
- JAVA 生成同音字的方法,随机生成汉字,汉字转拼音,写的不好,望指正.谢谢~
- 计算机实际上是如何工作的