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的值,也可以直接检验当前数组是否合法. # ...
随机推荐
- OAF 返回供应商门户主页
if (pageContext.getParameter("returnHomePage") != null) { pageContext.setForceForwardURL(& ...
- 让FireFox支持window.event属性
场景描述: 在用户行为采集的过程中,需要侦听window下的event对象,根据事件类型做相应的过滤处理,但在firefox下window.event是未定义的: 问题分析: 要想获取event属性共 ...
- 快速切题 poj 2993 Emag eht htiw Em Pleh 模拟 难度:0
Emag eht htiw Em Pleh Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2806 Accepted: ...
- 转载:【Oracle 集群】RAC知识图文详细教程(九)--RAC基本测试与使用
文章导航 集群概念介绍(一) ORACLE集群概念和原理(二) RAC 工作原理和相关组件(三) 缓存融合技术(四) RAC 特殊问题和实战经验(五) ORACLE 11 G版本2 RAC在LINUX ...
- ViewPager + Fragment 实现主界面底部导航栏
1. 四个类似的Frament布局 tab_main_fragment.xml <LinearLayout xmlns:android="http://schemas.android. ...
- vuex: 简单(弹窗)实现
在使用基于 vue.js 2.0 的UI框架 ElementUI 开发网站的时候 , 就遇到了这种问题 : 一个页面有很多表单 , 我试图将表单写成一个单文件组件 , 但是表单 ( 子组件 ) 里的数 ...
- 转:application/json 四种常见的 POST 提交数据方式
四种常见的 POST 提交数据方式 HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 PO ...
- 便捷的Jenkins jswidgets
很多时候我们在构建完成之后需要查看构建的状态,类似github 中的build Status 插件安装 搜索插件 使用 目前好像只支持自由项目的构建 代码集成 <!DOCTYPE html> ...
- angularJS的ng-repeat-start
使用angularJS的同学对ng-repeat都不会陌生,他是用来进行数据循环的,一般用于数组或者对象.但是今天我们用到了一个ng-repeat-start. ng-repeat-start,与ng ...
- CentOS 6.5 下搭建FastDFS服务
参考网站: http://www.open-open.com/lib/view/open1435468300700.html http://blog.csdn.net/lynnlovemin/arti ...