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++);不剪枝的会超时,剪枝的内容代码里会讲;

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

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#define true 1
#define false 0
using namespace std;
typedef long long LL;
const int N=1e3+;
vector<string>arr,str;
int dis[][]= {{,},{-,},{,-},{,},{-,-},{-,},{,-},{,}};//方向数组
int vis[N][N];//标记数组
int m,n,flag,len;
void solve();
void Init()
{
arr.clear();
memset(vis,,sizeof(vis));
}
void Input()
{
string s;
cin>>m;
for(int i=; i<m; i++)
{
cin>>s;
str.push_back(s);
}
while(cin>>n&&n)
{
Init();
for(int i=; i<n; i++)
{ cin>>s;
arr.push_back(s);
}
solve();
} } void dfs(int x,int y,int num,int cur)//x当前行,y当前列,num代表第几个单词,cur代表该单词的第几个字符
{
int cnt=str[num].size();
if(cur>=cnt||flag||x>=n||y>=n)//如果当前cur大于等于该字符串的长度或该字符窜已经找到或者当前行的长度或列的高度大于矩阵的阶数,剪枝1
{
         return ;
}
if(str[num][cur]=='q')//剪枝2
{
if(cur+<cnt&&str[num][cur+]=='u')//判断下一个字符是否为‘u'
{
cur++;
}
else
return; }
if(cur==cnt-)
{
flag=;
return ;
}
for(int i=; i<; i++)//八方向搜索
{
int dx=x+dis[i][];
int dy=y+dis[i][];
if(dx>=&&dx<n&&dy>=&&dy<n&&!vis[dx][dy])//判断是否满足条件
{
if(arr[dx][dy]==str[num][cur+])
{
vis[dx][dy]=true;//标记
dfs(dx,dy,num,cur+);
vis[dx][dy]=false;//标记还原
}
}
} }
void solve()
{
vector<string>s;
for(int i=,len=str.size(); i<len; i++)
{
flag=;
for(int j=; j<n; j++)
{
memset(vis,,sizeof(vis));
for(int k=; k<n; k++)//找到与该单词第一个字符相等的矩阵的位置
{
if(arr[j][k]==str[i][])
{
vis[j][k]=true;//标记
dfs(j,k,i,);
}
}
if(flag)//如果搜索到flag返回true
break;
}
if(flag)
s.push_back(str[i]);
}
sort(s.begin(),s.end());//对字符串排序
for(int i=,len=s.size(); i<len; i++)
cout<<s[i]<<endl;
cout<<"-"<<endl;
}
int main()
{
Input();
}

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. 加密算法中涉及C/C++总结

    学习网站:http://www.runoob.com/cplusplus/cpp-functions.html char在VC(c++)中占1字节(byte),8位(bit). int在VC(c++) ...

  2. Eclipse中配置Solr源码

    转自 http://hongweiyi.com/2013/03/configurate-solr-src-in-eclipse/ 1. 下载solr的src包,并解压 2. 解压后,在解压后的根目录执 ...

  3. epoll模型边沿触发

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  4. keras_基本网络层结构(2)_卷积层

    参考文献:http://keras-cn.readthedocs.io/en/latest/layers/convolutional_layer/ 卷积层 Conv1D层 keras.layers.c ...

  5. HDU 3667

    http://acm.hdu.edu.cn/showproblem.php?pid=3667 最小费用最大流 本题流量和费用不是线性关系,fee=a*flow*flow,所以常规套模板spfa无法得到 ...

  6. tf.cast()数据类型转换

    tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换,比如读入的图片如果是int8类型的,一般在要在训练前把图像的数据格式转换为float32. cast定义: cast(x, ...

  7. Loj 2005 相关分析

    Loj 2005 相关分析 大力把式子拆开. \[ \begin{aligned} a &= \frac {\sum_{i=L}^{R} (x_i-\bar{x})(y_i-\bar{y})} ...

  8. Codeforces 1009C: Annoying Present

    C. Annoying Present time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. jquery类的创建方式及关键字new的原理

    一.由jQuery创建类引发的问题 在用jQuery选择器时候,可以通过下面两种方式获取元素,并得到一个jQuery对象. var d1 = jQuery('#demo01'); var d2 = n ...

  10. 在laravel之外使用eloquent

    视频地址 https://laracasts.com/lessons/how-to-use-eloquent-outside-of-laravel