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. IOS-详解KVO底层实现

    一.KVO (Key-Value Observing) KVO 是 Objective-C 对观察者模式(Observer Pattern)的实现.也是 Cocoa Binding 的基础.当被观察对 ...

  2. 非关联容器|hash|unordered_map/multimap,unordered_set/multiset

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

  3. [批处理]NetstatFilter快速查找端口被占用问题

    前言 准确的说,他是一个网络连接端口查看器,可以根据进程查端口,也可以根据端口查进程.期初是因在使用Fiddler的时候发现无法启动,提示端口被占用,但是由不知道用什么方法才能找到是哪个程序占用的Fi ...

  4. scikit-learn 学习笔记-- Generalized Linear Models (三)

    Bayesian regression 前面介绍的线性模型都是从最小二乘,均方误差的角度去建立的,从最简单的最小二乘到带正则项的 lasso,ridge 等.而 Bayesian regression ...

  5. javaScript基础篇之数据类型

    我主要学习廖雪峰老师官方网站的javaScript,所以很多都是出自于廖老师,请见谅.以下是廖老师的官方网站的地址:http://www.liaoxuefeng.com/wiki/0014344466 ...

  6. 阻塞和非阻塞I/O

    阻塞和非阻塞I/O是设备访问的两种不同模式,驱动程序可以灵活的支持用户空间对设备的这两种访问形式.        阻塞操作是指在执行设备操作时,若不能获得资源,则挂起进程直到满足可操作的条件后在进行操 ...

  7. google chrome浏览器离线小恐龙游戏刷分bug

    F12打开开发者工具->console->输入如下代码,分数要多少有多少 Runner.instance_.setSpeed(99999); 试试 瞬间 满分window.tempGame ...

  8. python的面向对象

    1.self 类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称,但是在调用这个方法的时候你不为这个参数赋值,Python会提供这个值.这个特别的变量指对象本身,按照惯例它的 ...

  9. ORA-12560:TNS:协议器错误的解决方法

    使用SQL Plus登录数据库时,系统报ORA-12560:TNS:协议器错误.之前建了三个数据库实例,删除了一个实例.现在登录其中一个数据库报此错误.   工具/原料   Oracle11g 方法/ ...

  10. Excel VBA 找出选定范围不重复值和重复值

    Sub 找出选定范围内不重复的值() On Error Resume Next Dim d As Object Set d = CreateObject("scripting.diction ...