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
Sample Output
april
quilt
-
purple
-
题意:先给n个单词,再给m*m的方格,看能否遍历出给出的单词,8个方向都可以搜到,输出,一组样例—结束输出。注意q的时候,把qu看成一个整体,如果q在网格出现过,就是qu在网格中出现过,要是单词在没有q但没有qu,则不能搜到。
思路:深搜。flag数组用来剪枝,就是判断是否有重复的字典出现,在DFS中为什么要最后清0了呢,就是在路走错了,回溯回去,要标记,防止出现第一次没走对,第二次能走对的情况。注意数组,vector的清空。
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
const ll MAX=1000+10;
string v[250];
set<string> s;
char w[250][250];
ll vis[300][300];
ll flag[2500];
ll n,m;
ll f[8][2]= {{1,0},{1,1},{0,1},{0,-1},{1,-1},{-1,0},{-1,1},{-1,-1}};
void DFS(ll x,ll y,ll k,ll q)
{
ll g,h,i;
vis[x][y]=1;
if(flag[k]==1)
return ;
if(w[x][y]=='q')
{
if(v[k][q+1]=='u'&&q+1<v[k].size())
q++;
else
return ;
}
if(q==v[k].size()-1)
{
s.insert(v[k]);
flag[k]=1;
return ;
}
for(i=0; i<8; i++)
{
g=x+f[i][0],h=y+f[i][1];
if(g>=0&&g<m&&h>=0&&h<m&&vis[g][h]==0)
if(w[g][h]==v[k][q+1])
DFS(g,h,k,q+1);
}
vis[x][y]=0;//走错了路回溯回去清标记
return ;
}
int main()
{
ll i,j,k;
cin>>n;
for(i=0; i<n; i++)
cin>>v[i];
while(cin>>m&&m)
{
memset(flag,0,sizeof(flag));
s.clear();
for(i=0; i<m; i++)
for(j=0; j<m; j++)
cin>>w[i][j];
for(i=0; i<n; i++)
{
memset(vis,0,sizeof(vis));
for(j=0; j<m; j++)
for(k=0; k<m; k++)
{
if(v[i][0]==w[j][k])
DFS(j,k,i,0);
}
}
set<string>::iterator it;
for(it=s.begin(); it!=s.end(); it++)
cout<<*it<<endl;
cout<<"-"<<endl;
}
return 0;
}