I - Boggle(DFS)

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

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

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;

typedef long long ll;
#define memset(a,n) memset(a,n,sizeof(n))
string s[205];
char a[10][10];
int vis[10][10];
int n,w,d;
int xx[8]= {0,0,-1,-1,-1,1,1,1};
int yy[8]= {1,-1,-1,0,1,-1,0,1};
bool flagg;
void dfs(int x,int y,int k,int flag)
{
    if(flagg==true)
        return ;
    int len=s[k].size();

    if(a[x][y]=='q')
        flag++;
    if(len-1==flag)
    {
        flagg=true;
        return ;
    }
     vis[x][y]=1;     //标记的这一句必须写在这里,因为这一句WA了好几次
    for(int i=0; i<8; i++)
    {
        int x1=x+xx[i];
        int y1=y+yy[i];
        if(x1>=0&&x1<n&&y1>=0&&y1<n&&vis[x1][y1]==0&&s[k][flag+1]==a[x1][y1])
        {
              dfs(x1,y1,k,flag+1);
        }
    }
    vis[x][y]=0;

}
int check(int k)
{
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            if(a[i][j]==s[k][0])
            {
                flagg=false;
                memset(vis,0);
                dfs(i,j,k,0);
                if(flagg==true)
                    return 1;
            }
    return 0;
}
int main()
{
    scanf("%d",&w);
    for(int i=0; i<w; i++)
        cin>>s[i];
    sort(s,s+w);
    int flag;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        for(int i=0; i<n; i++)
            scanf("%s",a[i]);
        for(int i=0; i<w; i++)
        {
            flag=0;
            int len=s[i].size();
            for(int j=0; j<len; j++)
                if(s[i][j]=='q'&&s[i][j+1]!='u')
                {
                    flag=1;
                    break;
                }

            if(flag==1)
                continue;

            if(check(i)==1)
                cout<<s[i]<<endl;
            else
                continue;
        }
        printf("-\n");

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值