Boggle (UVALive 7299) Regionals 2015--North America - South Central USA

                                                                               Boggle

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=716&page=show_problem&problem=5311
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
-

思路: 深搜,对字典中每个单词在网格中进行搜索,找到的话就输出(要排序,从小到大按字典序),然后比赛的时候掉了好多坑。

1. 首先,DFS函数最后一行的  vis[x][y]我们没加,之所以要加这一行,是因为当你搜一条路发现搜错了,这条路不能满足整个单词,然后就要回溯吧,回溯到可以继续搜的地方(假设一定可以搜出来),但是搜过得错的那条路上的点你都已经标记了,所以如果正确的路上如果包含你已经标记过得点了,你就不可以搜这个点了,然后本来可以搜出来的单词就搜不出来了。

举个例子吧:假设有一个单词是  abwcdw   ,然后网格是 

abwx
bxcx
wdxx
xxxx

如果你刚开始是从a到a下面的b时,搜到w的时候你会发现走不动了,然后你会回溯到a继续搜a左面的b,然后搜到最后那个w的时候,发现那个w已经被你刚才的错误的搜索加上标记了,所以。。。

2. 关于q的问题,刚开始我们也理解错了,应该是当网格中出现q的时候,要把q当成qu,即一个单词中的q后面必须是u,否则这个单词不会在网格中搜到。

3. 然后最后还T了一发,flag数组就是减少一些搜索的

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<cstring>
#include<set>
using namespace std;
typedef long long LL ;
string s[2100];
char dfs[100][100];
int vis[100][100];
int f[8][2]={{1,0},{-1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
set<string> ans;
int n,m;
bool flag[1000];
void init()
{
    cin>>m;
    for(int i=0;i<m;i++)
        cin>>s[i];
    return ;
}
void DFS(int x,int y,int k,int res)
{
    vis[x][y]=1;
    if(flag[k]==1)  //降低时间复杂度,没加的时候T了
        return ;
    if(dfs[x][y]=='q')  //把q当成qu,即q后面必须是u,否则这个单词不会在网格中搜到
    {
        if(s[k][res+1]=='u'&&res+1<s[k].size())
            res++;
        else
            return ;
    }
    if(res==s[k].size()-1)
    {
        ans.insert(s[k]);
        flag[k]=1;
        return ;
    }
    for(int i=0;i<8;i++)
    {
        int z=x+f[i][0],o=y+f[i][1];
        if(z>=0&&z<n&&o>=0&&o<n&&vis[z][o]==0)
            if(dfs[z][o]==s[k][res+1])
            {
                DFS(z,o,k,res+1);
            }
    }
    vis[x][y]=0; //防止搜错路了,导致下一次用到的时候被标记
    return ;
}
int main()
{
    init();
    while(cin>>n&&n)
    {
        memset(flag,0,sizeof(flag));
        ans.clear();
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
              cin>>dfs[i][j];
        for(int k=0;k<m;k++)
        {
            memset(vis,0,sizeof(vis)); //搜一个单词的时候清空一次
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                {
                    if(dfs[i][j]==s[k][0])
                    {
                        DFS(i,j,k,0);
                    }
                }
        }
        set<string>::iterator it;
        for(it=ans.begin();it!=ans.end();it++)
            cout<<*it<<endl;
        cout<<"-"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Septembre_/article/details/84200270