Boggle(DFS)

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章