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. 内存泄漏 之 MAT工具的使用

    1 内存泄漏的排查方法 Dalvik Debug Monitor Server (DDMS) 是 ADT插件的一部分,其中有两项功能可用于内存检查 : ·    heap 查看堆的分配情况 ·     ...

  2. 公式中表达单个双引号【"】和空值【""】的方法及说明

    http://club.excelhome.net/thread-661904-1-1.html 有人问为什么不用三个双引号"""来表示单个双引号["]呢,如果 ...

  3. 2017年7月ROS学习资料小结

    <孙子兵法·谋攻篇>:"上兵伐谋,其次伐交,其次伐兵,其下攻城:攻城之法为不得已." 任何发生在自己国土上的战争,即便胜利,也饱含屈辱. ----~~~~----Gaz ...

  4. BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2530  So ...

  5. java 之DelayQueue,TaskDelayed,handlerFactory,dataChange消息配置.收发等.java spring事务处理TransactionTemplate

    java 之DelayQueue,TaskDelayed,handlerFactory,dataChange消息配置.收发等.java spring事务处理TransactionTemplate等. ...

  6. Git 学习记录一

    主要来源参考http://www.runoob.com/git/git-install-setup.html Windows 平台上安装 在 Windows 平台上安装 Git 同样轻松,有个叫做 m ...

  7. opencrud 中文参考翻译(完成部分)

    opencrud 是一个就比较好的关于graphql 实现的指南(当前只有部分文档,完整的还没有,实际上apollo 有相关的文档都挺不错的) 同时在github 有一个中文的简单翻译(后期应该会和官 ...

  8. 《快学Scala》

    Robert Peng's Blog - https://mr-dai.github.io/ <快学Scala>Intro与第1章 - https://mr-dai.github.io/S ...

  9. [转]无网络环境,在Windows Server 2008 R2和SQL Server 2008R2环境安装SharePoint2013 RT

    无网络环境,在Windows Server 2008 R2和SQL Server 2008R2环境安装SharePoint2013 RT,这个还有点麻烦,所以记录一下,下次遇到省得绕弯路.进入正题: ...

  10. VB中上传下载文件到SQL数据库

    VB中上传下载文件到SQL数据库 编写人:左丘文 2015-4-11 近期在修改一个VB编写的系统时,想给画面增加一个上传文件到数据库,并可以下载查看的功能,今天在这里,我想与大家一起分享代码,在此做 ...