uvalive 7299 Boggle
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的更多相关文章
- UVALive 7299 Boggle(深搜的姿势)
一开始确实是我的锅,我把题意理解错了,以为是一个q周围没有q的时候才可以当时qu,其实是只要碰到q,他就是qu,所以我们也可以通过预处理的方式,把字典中的不满足qu连在一起的直接去掉. 后来的各种TI ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
- UVALive 6948 Jokewithpermutation dfs
题目链接:UVALive 6948 Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...
随机推荐
- mysql插入中文数据报错 java.sql.SQLException: Incorrect string value: '\xE5\x90\x88\xE8\xAE\xA1' for column
1.我们创建数据库的时候没有更改数据库的字符集为utf8. 在mysql工具中,右击数据库,->"改变数据库",->选择“基字符集”为utf-8; 2,数据库中表的字符 ...
- BZOJ2620 [Usaco2012 Mar]Haybale Restacking
恩,非常好的题...至少思路非常巧妙 首先可以得到性质:对于相邻的两堆A & B,A给B然后B再给A是完全没有意义的...也就是说只能单向传递 然后我们记下每个点要给(被给)多少堆干草a[i] ...
- SpringMvc中的Interceptor拦截器的学习
拦截器是SpringMvc框架中常用的一个东东,它跟Filter相似,但是也有区别,以前也没用过,今天看到就顺便学习了一下. SpirngMvc中的Interceptor主要是通过HandlerInt ...
- Java第七次作业--图形用户界面
Deadline: 2017-5-11 23:00 一.学习要点 认真看书并查阅相关资料,掌握以下内容: 了解GUI开发的相关原理和技巧 熟悉Swint组件的使用 理解事件处理模型 二.作业要求 发布 ...
- /dev/mem直接操作硬件寄存器
/******************************************************************************* * /dev/mem直接操作硬件寄存器 ...
- error: 'ENOSYS' undeclared (first use in this function)
/************************************************************************ * error: 'ENOSYS' undeclar ...
- 51Nod 1089:最长回文子串 V2(Manacher算法)
1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 回文串是指aba.abba.cccbccc.aaa ...
- wpf 客户端【JDAgent桌面助手】业余开发的终于完工了。。晒晒截图
目录区域: 业余开发的wpf 客户端终于完工了..晒晒截图 wpf 客户端[JDAgent桌面助手]开发详解-开篇 wpf 客户端[JDAgent桌面助手]详解(一)主窗口 圆形菜单... wpf 客 ...
- i2c接口笔记
一. i2c基础知识 1. NACK信号:当在第9个时钟脉冲的时候SDA线保持高电平,就被定义为NACK信号.Master要么产生STOP条件来放弃这次传输,或者重复START条件来发起一个新的开始. ...
- Mysql5.6 buffer_pool预热功能
通常在mysql重启服务后,需要通过手工执行SQL来预热buffer_pool,在mysql5.6中,有如下参数可以无需人工干预. innodb_buffer_pool_dump_at_shutdow ...