zoukankan      html  css  js  c++  java
  • uvalive 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
    -

    题意:给出m个要查询的的单词,n组输入,对于每一个n×n的矩阵你可以与任意一边相邻的八个方向搜索,若能匹配到要查询的单词则输出,每组数据后输出一行“-”

    若果遇到‘q’你需要把它替换成“qu”;

    题解:dfs搜索八个方向,如果遇到‘q',判断他下一个字符是否为’u',若不是则返回,反之继续搜索它的后一个字符(‘u'已经搜索过了,所以cur++);不剪枝的会超时,剪枝的内容代码里会讲;

    注意:输出时要将搜索到的单词按字典序排列

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<iostream>
      4 #include<vector>
      5 #include<map>
      6 #include<algorithm>
      7 #define true 1
      8 #define false 0
      9 using namespace std;
     10 typedef long long LL;
     11 const int N=1e3+10;
     12 vector<string>arr,str;
     13 int dis[8][2]= {{1,0},{-1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};//方向数组
     14 int vis[N][N];//标记数组
     15 int m,n,flag,len;
     16 void solve();
     17 void Init()
     18 {
     19     arr.clear();
     20     memset(vis,0,sizeof(vis));
     21 }
     22 void Input()
     23 {
     24     string s;
     25     cin>>m;
     26     for(int i=0; i<m; i++)
     27     {
     28         cin>>s;
     29         str.push_back(s);
     30     }
     31     while(cin>>n&&n)
     32     {
     33         Init();
     34         for(int i=0; i<n; i++)
     35         {
     36 
     37             cin>>s;
     38             arr.push_back(s);
     39         }
     40         solve();
     41     }
     42 
     43 }
     44 
     45 void dfs(int x,int y,int num,int cur)//x当前行,y当前列,num代表第几个单词,cur代表该单词的第几个字符
     46 {
     47     int cnt=str[num].size();
     48     if(cur>=cnt||flag||x>=n||y>=n)//如果当前cur大于等于该字符串的长度或该字符窜已经找到或者当前行的长度或列的高度大于矩阵的阶数,剪枝1
    49 {
     50         return ;
     51     }
     52     if(str[num][cur]=='q')//剪枝2
     53     {
     54         if(cur+1<cnt&&str[num][cur+1]=='u')//判断下一个字符是否为‘u'
     55         {
     56             cur++;
     57         }
     58         else
     59             return;
     60 
     61     }
     62     if(cur==cnt-1)
     63     {
     64         flag=1;
     65         return ;
     66     }
     67     for(int i=0; i<8; i++)//八方向搜索
     68     {
     69         int dx=x+dis[i][0];
     70         int dy=y+dis[i][1];
     71         if(dx>=0&&dx<n&&dy>=0&&dy<n&&!vis[dx][dy])//判断是否满足条件
     72         {
     73             if(arr[dx][dy]==str[num][cur+1])
     74             {
     75                 vis[dx][dy]=true;//标记
     76                 dfs(dx,dy,num,cur+1);
     77                 vis[dx][dy]=false;//标记还原
     78             }
     79         }
     80     }
     81 
     82 }
     83 void solve()
     84 {
     85     vector<string>s;
     86     for(int i=0,len=str.size(); i<len; i++)
     87     {
     88         flag=0;
     89         for(int j=0; j<n; j++)
     90         {
     91             memset(vis,0,sizeof(vis));
     92             for(int k=0; k<n; k++)//找到与该单词第一个字符相等的矩阵的位置
     93             {
     94                 if(arr[j][k]==str[i][0])
     95                 {
     96                 vis[j][k]=true;//标记
     97                 dfs(j,k,i,0);
     98                 }
     99             }
    100             if(flag)//如果搜索到flag返回true
    101                 break;
    102         }
    103         if(flag)
    104             s.push_back(str[i]);
    105     }
    106     sort(s.begin(),s.end());//对字符串排序
    107     for(int i=0,len=s.size(); i<len; i++)
    108         cout<<s[i]<<endl;
    109     cout<<"-"<<endl;
    110 }
    111 int main()
    112 {
    113     Input();
    114 }
  • 相关阅读:
    MySQL 序列使用
    04_使用httpclient提交参数_get提交参数
    03_使用httpurlconnection提交参数_get中文参数解决&post提交
    01_今日内容
    00_消息机制回顾
    20_内容回顾
    19_MySmartImageView添加展示默认图的功能
    18_MySmartImageView实现
    17_自定义View对象构造说明
    16_新闻客户端_展示图片内容完成
  • 原文地址:https://www.cnblogs.com/moomcake/p/9997553.html
Copyright © 2011-2022 走看看