POJ 1204 AC自动机入门

题目

Word Puzzles
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 10684 Accepted: 4038 Special Judge

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

Source


想法

SpecialJudge其实没有什么意思,主要就是方向的那个字母可以是该方向上的任意一个,所以需要特判。

代码 View Source On GitHub

#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

const int MAXC=1005;
const int MAXL=1005;
const int MAXW=1005;
#define nullptr NULL
//#define ISDEBUG

#ifdef ISDEBUG
#define dprintf(format,args...) printf(format,##args)
#define dsystem(cmd) system(cmd)
#else
#define dprintf(format,args...)
#define dsystem(cmd)
#endif // ISDEBUG
char map[MAXL][MAXC];
char word[MAXW];
int ans[MAXW][3];
int L,C,W;

int direction[8][2];

inline void initdirection()
{
    /// ..[..][0] +-Y   ..[..][1] +-X
    direction[0][0]=-1;
    direction[0][1]=0;

    direction[1][0]=-1;
    direction[1][1]=+1;

    direction[2][0]=0;
    direction[2][1]=+1;

    direction[3][0]=+1;
    direction[3][1]=+1;

    direction[4][0]=+1;
    direction[4][1]=0;

    direction[5][0]=+1;
    direction[5][1]=-1;

    direction[6][0]=0;
    direction[6][1]=-1;

    direction[7][0]=-1;
    direction[7][1]=-1;
}

struct node
{
    node* next[26];
    int wid;
};

node _root;
node* root=&_root;

void display_tree(int step,node* ptr)
{
    for(int i=0;i<26;i++)
    {
        if(ptr->next[i]!=nullptr)
        {
            for(int i=0;i<step;i++)
            {
                printf("\t");
            }
            printf("%c\n",i+'A');
            display_tree(step+1,ptr->next[i]);
        }
    }
}

void printtree()
{
    for(int i=0;i<26;i++)
    {
        if(root->next[i]!=nullptr)
        {
            printf("%c\n",i+'A');
            display_tree(1,root->next[i]);
        }
    }
}

void dfs(node* ptr,int starty,int startx,int y,int x,int dir)
{
    dprintf("In DFS: %d %d Dir=%d\n",y,x,dir);
    if(ptr==nullptr)
    {
        dprintf("Null Pointer.\n");
        return;
    }

    if(ptr->wid!=0)
    {
        dprintf("Set ID ptr->wid=%d Set to %d,%d,%d(dir)\n",ptr->wid,startx,starty,dir);
        dsystem("PAUSE");
        ans[ptr->wid][0]=starty;
        ans[ptr->wid][1]=startx;
        ans[ptr->wid][2]=dir+'A';
        /// Reset word ID to 0.
        ptr->wid=0;
    }
    if(y<0||y>=L||x<0||x>=C) return;
    /// Same Direction (dir)
    dfs(ptr->next[map[y][x]-'A'],starty,startx,y+direction[dir][0],x+direction[dir][1],dir);
}

void buildtree(char* str,node* curnode,int id)
{
    int len=strlen(str);
    node* nptr=curnode;
    for(int i=0;i<len;i++)
    {
        int index=str[i]-'A';
        if(nptr->next[index]==nullptr)
        {
            node* newnode=(node*)malloc(sizeof(node));
            for(int s=0;s<26;s++) newnode->next[s]=nullptr;
            newnode->wid=0;
            nptr->next[index]=newnode;
            nptr=newnode;
        }
        else
        {
            nptr=nptr->next[index];
        }
    }
    nptr->wid=id;
}

int main()
{
    initdirection();
    scanf("%d %d %d",&L,&C,&W);
    for(int i=0;i<L;i++)
    {
        scanf("%s",&map[i][0]);
    }
    for(int i=0;i<W;i++)
    {
        scanf("%s",word);
        buildtree(word,root,i+1);
    }
    /// Main LOOP
    for(int i=0;i<L;i++)
    {
        for(int j=0;j<C;j++)
        {
            for(int k=0;k<8;k++)
            {
                node* ptrStart=root;
                dfs(ptrStart,i,j,i,j,k);
            }
        }
    }
    for(int i=1;i<=W;i++)
    {
        printf("%d %d %c\n",ans[i][0],ans[i][1],(char)ans[i][2]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值