Boggle is a game in which 16 dice with letters on each side are placed into a 4 × 4 grid. Players then attempt to find words using letters from adjacent dice. You must write a program to find words from letters in a Boggle-like grid. When forming a word, each letter must be adjacent in the grid (horizontally, vertically, or diagonally) to the previous letter, and each grid position (not necessarily each letter) may only appear once in each word. In normal Boggle, every side of each die contains a single letter with one exception. No side has the letter q by itself; instead, the 2 letters qu appear together. Similarly, when the letter q appears in one of the grids in this problem’s Boggle variant, it should be treated as qu.

Input Input consists of a dictionary of words followed by several letter grids.

The first line contains an integer W (1 ≤ W ≤ 200) indicating the number of words in the dictionary.
Each of the following W lines contains a string of 1 to 25 lowercase ASCII letters (a - z) representing
a unique word in the dictionary.
The dictionary is followed by 1 or more letter grids. Each begins with a line containing an integer
D (2 ≤ D ≤ 8) indicating the size of that grid (square of size D × D) or 0 (zero) indicating end of
input. The following D lines each contain D lowercase ASCII letters (a - z); each line represents a row
in the grid.
Output
For each grid, output consists of the following:
• A line for each dictionary word that was found in the grid, sorted alphabetically.
• A line containing a dash (-).

Sample Input
3
april
purple
quilt
5
rprit
ahqln
ietep
zrysg
ogwey
3
pel
aup
bcr
0

Sample Output
april
quilt
-
purple
-

题意:给出m个要查询的的单词,n组输入,对于每一个n×n的矩阵你可以与任意一边相邻的八个方向搜索,若能匹配到要查询的单词则输出,每组数据后输出一行“-”

若果遇到‘q’你需要把它替换成“qu”;

题解:dfs搜索八个方向,如果遇到‘q',判断他下一个字符是否为’u',若不是则返回,反之继续搜索它的后一个字符(‘u'已经搜索过了,所以cur++);不剪枝的会超时,剪枝的内容代码里会讲;

注意:输出时要将搜索到的单词按字典序排列

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<vector>
  5. #include<map>
  6. #include<algorithm>
  7. #define true 1
  8. #define false 0
  9. using namespace std;
  10. typedef long long LL;
  11. const int N=1e3+;
  12. vector<string>arr,str;
  13. int dis[][]= {{,},{-,},{,-},{,},{-,-},{-,},{,-},{,}};//方向数组
  14. int vis[N][N];//标记数组
  15. int m,n,flag,len;
  16. void solve();
  17. void Init()
  18. {
  19. arr.clear();
  20. memset(vis,,sizeof(vis));
  21. }
  22. void Input()
  23. {
  24. string s;
  25. cin>>m;
  26. for(int i=; i<m; i++)
  27. {
  28. cin>>s;
  29. str.push_back(s);
  30. }
  31. while(cin>>n&&n)
  32. {
  33. Init();
  34. for(int i=; i<n; i++)
  35. {
  36.  
  37. cin>>s;
  38. arr.push_back(s);
  39. }
  40. solve();
  41. }
  42.  
  43. }
  44.  
  45. void dfs(int x,int y,int num,int cur)//x当前行,y当前列,num代表第几个单词,cur代表该单词的第几个字符
  46. {
  47. int cnt=str[num].size();
  48. if(cur>=cnt||flag||x>=n||y>=n)//如果当前cur大于等于该字符串的长度或该字符窜已经找到或者当前行的长度或列的高度大于矩阵的阶数,剪枝1
    {
  1. return ;
  2. }
  3. if(str[num][cur]=='q')//剪枝2
  4. {
  5. if(cur+<cnt&&str[num][cur+]=='u')//判断下一个字符是否为‘u'
  6. {
  7. cur++;
  8. }
  9. else
  10. return;
  11. }
  12. if(cur==cnt-)
  13. {
  14. flag=;
  15. return ;
  16. }
  17. for(int i=; i<; i++)//八方向搜索
  18. {
  19. int dx=x+dis[i][];
  20. int dy=y+dis[i][];
  21. if(dx>=&&dx<n&&dy>=&&dy<n&&!vis[dx][dy])//判断是否满足条件
  22. {
  23. if(arr[dx][dy]==str[num][cur+])
  24. {
  25. vis[dx][dy]=true;//标记
  26. dfs(dx,dy,num,cur+);
  27. vis[dx][dy]=false;//标记还原
  28. }
  29. }
  30. }
  31. }
  32. void solve()
  33. {
  34. vector<string>s;
  35. for(int i=,len=str.size(); i<len; i++)
  36. {
  37. flag=;
  38. for(int j=; j<n; j++)
  39. {
  40. memset(vis,,sizeof(vis));
  41. for(int k=; k<n; k++)//找到与该单词第一个字符相等的矩阵的位置
  42. {
  43. if(arr[j][k]==str[i][])
  44. {
  45. vis[j][k]=true;//标记
  46. dfs(j,k,i,);
  47. }
  48. }
  49. if(flag)//如果搜索到flag返回true
  50. break;
  51. }
  52. if(flag)
  53. s.push_back(str[i]);
  54. }
  55. sort(s.begin(),s.end());//对字符串排序
  56. for(int i=,len=s.size(); i<len; i++)
  57. cout<<s[i]<<endl;
  58. cout<<"-"<<endl;
  59. }
  60. int main()
  61. {
  62. Input();
  63. }

uvalive 7299 Boggle的更多相关文章

  1. UVALive 7299 Boggle(深搜的姿势)

    一开始确实是我的锅,我把题意理解错了,以为是一个q周围没有q的时候才可以当时qu,其实是只要碰到q,他就是qu,所以我们也可以通过预处理的方式,把字典中的不满足qu连在一起的直接去掉. 后来的各种TI ...

  2. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  3. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  4. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  5. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  6. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  8. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  9. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

随机推荐

  1. python学习笔记(一)---python下载以及环境的安装

    转载网址:https://www.runoob.com/python/python-install.html 1.下载python安装包: 安装包下载网址(如下图所在的网址):https://www. ...

  2. hdu 3682 10 杭州 现场 C To Be an Dream Architect 容斥 难度:0

    C - To Be an Dream Architect Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  3. centos6.9使用yum安装mysql(简单粗暴,亲测有效)

    第1步.yum安装mysql[root@stonex ~]#  yum -y install mysql-server安装结果:Installed:    mysql-server.x86_64 0: ...

  4. -webkit新属性 image-set和srcset

    响应式图片的作用: 为使用不同分辨率的不同浏览器用户提供适合其浏览环境的图片大小的解决方案. 之前的解决方法是使用@media 但是-webkit新提出的image-set和srcset同样可以解决问 ...

  5. 20181009-5 选题 Scrum立会报告+燃尽图 04

    Scrum立会报告+燃尽图(04)选题 此作业要求参见:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2194] 一.小组介绍 组长:刘 ...

  6. JS在项目中用到的AOP, 以及函数节流, 防抖, 事件总线

    1. 项目中在绑定事件的时候总想在触发前,或者触发后做一些统一的判断或逻辑,在c#后端代码里,可以用Attribute, filter等标签特性实现AOP的效果,可是js中没有这种用法,归根到本质还是 ...

  7. SOA实践指南-读书笔记

    SOA是英文Service-Oriented Architecture,即面向服务架构的缩写. SOA是一种范式,目的是增强灵活性.SOA很适宜处理复杂的分布式系统. SOA方法接受异质(不同的平台, ...

  8. leisure time

    终于把论文翻译完了,天哪,现在感觉解脱一般. 这些天看电视,玩游戏,也不止学了写什么,现在调整了下心情,重新确定下目标吧. 最近很想学Python和Qt,哎,技术永远都是学不完了,理解操作系统和组成原 ...

  9. oracle中union和minus的用法【oracle技术】

    UNION是将两个或者两个以上的搜索结果集合并在一起!这个合并是有条件滴!记录的类型要匹配啦,记录的列数要一样啦!看看下面简单的例子: 有的朋友会说为什么要用union呢,直接用txt3 in ('I ...

  10. ES6必知必会 (八)—— async 函数

    async 函数 1.ES2017 标准引入了 async 函数,它是对 Generator 函数的改进 , 我们先看一个读取文件的例子: Generator 写法是这样的 : var fs = re ...