欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Boggle(DFS)

程序员文章站 2022-05-20 20:52:21
...

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 (-).

3
april
purple
quilt
5
rprit
ahqln
ietep
zrysg
ogwey
3
pel
aup
bcr

Sample Output

april
quilt
-
purple
-

题意:把n个单词存入字典里,然后在网格中搜索字典里的单词,每次对八个方向进行搜索,如果搜到了这个单词就输出。
注意以下几个点:
1.输出的单词按字典序排序
2.如果在一条错的路上已经搜到过一个字母(这个字母肯定已经被标记过了),要把他的标记再去掉,这是防止在正确的路上搜索的时候会再次搜到这个字母
3.如果网格里出现了q,直接当做qu,也就是说,字典里存的所有单词都必须是qu相连,如果有其他的出现都不行。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#define PI acos(-1)
const int mod = 1001113;
const int maxx = 1e9;
typedef long long LL;
using namespace std;
string s[300];//字典
string ss[300];
char a[10][10];//给出的网格
int vis[10][10];//标记表格中的字母位置
int flag[300];
int f[8][2]= {{1,0},{1,1},{0,1},{0,-1},{1,-1},{-1,0},{-1,1},{-1,-1}};
int w,n,z=0;
void dfs(int x,int y,int k,int q)
{
    int xx,yy;
    vis[x][y]=1;
    if(flag[k]==1)
        return ;
    if(a[x][y]=='q')
    {
        if(s[k][q+1]=='u'&&q+1<s[k].size())
        {
            q++;
        }
        else
            return ;
    }
    if(q==s[k].size()-1)
    {
        flag[k]=1;
        return ;
    }
    for(int i=0; i<8; i++)
    {
        xx=x+f[i][0];
        yy=y+f[i][1];
        if(xx>=0&&xx<n&&yy>=0&&yy<n&&vis[xx][yy]==0)
        {
            if(a[xx][yy]==s[k][q+1])
            {
                dfs(xx,yy,k,q+1);
            }
        }
    }
    vis[x][y]=0;
    return ;
}
int main()
{
    cin>>w;
    for(int i=0; i<w; i++)
        cin>>s[i];
    sort(s,s+w);
    while(scanf("%d",&n)!=EOF)
    {
        z=0;
        memset(flag,0,sizeof(flag));
        if(n==0)
            break;
        for(int i=0; i<n; i++)
            scanf(" %s",a[i]);
        for(int i=0; i<w; i++)
        {
            memset(vis,0,sizeof(vis));
            for(int j=0; j<n; j++)
            {
                for(int k=0; k<n; k++)
                {
                    if(s[i][0]==a[j][k])
                        dfs(j,k,i,0);
                }
            }
            if(flag[i]==1)
                cout<<s[i]<<endl;
        }
        printf("-\n");
    }
    return 0;
}
相关标签: DFS