zoukankan      html  css  js  c++  java
  • [数据结构][字典树]Word Puzzles

    Description

    Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's perception of any possible delay in bringing them their order. 

    Even though word puzzles may be entertaining to solve by hand, they may become boring when they get very large. Computers do not yet get bored in solving tasks, therefore we thought you could devise a program to speedup (hopefully!) solution finding in such puzzles. 

    The following figure illustrates the PizzaHut puzzle. The names of the pizzas to be found in the puzzle are: MARGARITA, ALEMA, BARBECUE, TROPICAL, SUPREMA, LOUISIANA, CHEESEHAM, EUROPA, HAVAIANA, CAMPONESA. 

    Your task is to produce a program that given the word puzzle and words to be found in the puzzle, determines, for each word, the position of the first letter and its orientation in the puzzle. 

    You can assume that the left upper corner of the puzzle is the origin, (0,0). Furthemore, the orientation of the word is marked clockwise starting with letter A for north (note: there are 8 possible directions in total). 

    Input

    The first line of input consists of three positive numbers, the number of lines, 0 < L <= 1000, the number of columns, 0 < C <= 1000, and the number of words to be found, 0 < W <= 1000. The following L input lines, each one of size C characters, contain the word puzzle. Then at last the W words are input one per line.

    Output

    Your program should output, for each word (using the same order as the words were input) a triplet defining the coordinates, line and column, where the first letter of the word appears, followed by a letter indicating the orientation of the word according to the rules define above. Each value in the triplet must be separated by one space only.

    Sample Input

    20 20 10
    QWSPILAATIRAGRAMYKEI
    AGTRCLQAXLPOIJLFVBUQ
    TQTKAZXVMRWALEMAPKCW
    LIEACNKAZXKPOTPIZCEO
    FGKLSTCBTROPICALBLBC
    JEWHJEEWSMLPOEKORORA
    LUPQWRNJOAAGJKMUSJAE
    KRQEIOLOAOQPRTVILCBZ
    QOPUCAJSPPOUTMTSLPSF
    LPOUYTRFGMMLKIUISXSW
    WAHCPOIYTGAKLMNAHBVA
    EIAKHPLBGSMCLOGNGJML
    LDTIKENVCSWQAZUAOEAL
    HOPLPGEJKMNUTIIORMNC
    LOIUFTGSQACAXMOPBEIO
    QOASDHOPEPNBUYUYOBXB
    IONIAELOJHSWASMOUTRK
    HPOIYTJPLNAQWDRIBITG
    LPOINUYMRTEMPTMLMNBO
    PAFCOPLHAVAIANALBPFS
    MARGARITA
    ALEMA
    BARBECUE
    TROPICAL
    SUPREMA
    LOUISIANA
    CHEESEHAM
    EUROPA
    HAVAIANA
    CAMPONESA

    Sample Output

    0 15 G
    2 11 C
    7 18 A
    4 8 C
    16 13 B
    4 15 E
    10 3 D
    5 1 E
    19 7 C
    11 11 H
    思路:枚举起点和方向,将当前枚举的这个串,与询问中的w个串比较,当然逐个比较会超时,这时可以考虑将w个串建一棵字典树,让枚举的串与字典树比较;
    AC代码:
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    using namespace std;
    
    int n,m,w;
    char Map[1005][1005];
    char word[1005][1005];
    int ansx[1005],ansy[1005],ansk[1005];
    int dire[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};
    
    struct node{
      int id;
      node* son[26];
    };
    
    node* creat(){
      node* p=(node*)malloc(sizeof(node));
      p->id=0;
      for(int i=0;i<26;i++) p->son[i]=NULL;
      return p;
    }
    
    void insertTrie(node *root,char s[],int id){
      node *p=root;
      int len=strlen(s);
      for(int i=0;i<len;i++){
        int t=s[i]-'A';
        if(p->son[t]==NULL) p->son[t]=creat();
        p=p->son[t];
      }
      p->id=id;
    }
    
    bool check(int x,int y){
      if(x<1||x>n||y<1||y>m) return 0;
      return 1;
    }
    
    void searchTrie(node *root,int x,int y,int k){
      node *p=root;
      for(int i=x,j=y;check(i,j);i+=dire[k][0],j+=dire[k][1]){
        int t=Map[i][j]-'A';
        if(p->son[t]==NULL) return;
        else p=p->son[t];
        if(p->id>0){
            int id=p->id;
            ansx[id]=x,ansy[id]=y,ansk[id]=k;
            p->id=0;//保证ans存的是第一个包含该串的位置
        }
      }
    }
    
    int main()
    {
        scanf("%d%d%d",&n,&m,&w);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf(" %c",&Map[i][j]);
            }
        }
        node* root=creat();
        for(int i=1;i<=w;i++){
            scanf("%s",word[i]);
            insertTrie(root,word[i],i);
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                for(int k=0;k<8;k++){
                    searchTrie(root,i,j,k);
                }
            }
        }
        for(int i=1;i<=w;i++) printf("%d %d %c
    ",ansx[i]-1,ansy[i]-1,ansk[i]+'A');
        return 0;
    }

    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    Web 前端 UI 组件库文档自动化方案 All In One
    how to auto open demo and create it in a new codesandbox
    TypeScript & Canvas 实现可视化白板
    2020~2021 职业规划书
    PostCSS All In One
    zsh terminal set infinity scroll height
    npm version ^ meaning
    vue-cli & webpack & vue.config.js
    [HDOJ5350]MZL's munhaff function
    [POJ3061]Subsequence
  • 原文地址:https://www.cnblogs.com/lllxq/p/9817891.html
Copyright © 2011-2022 走看看