码迷,mamicode.com
首页 > 其他好文 > 详细

[POJ 1204]Word Puzzles(Trie树暴搜)

时间:2014-07-16 14:42:26      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:ac自动机   trie树   trie图   字符串   

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.
bubuko.com,布布扣

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

 

可以说是Trie模板题,将单词插入Trie树后,以地图上每个点作为起点,8个方向搜索单词即可,WA了6发,总算AC了,需要注意的就是当目前的单词已经搜到尽头(终止节点),不要停止搜索!这个题也可以用AC自动机和TrIe图做!

#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>

#define WORD 26
#define MAXN 1050

using namespace std;

struct trie
{
	trie *child[WORD]; //儿子节点指针
	int id; //若该节点为某单词的终止节点,id=该单词编号
	trie(){
		memset(child,0,sizeof(child)); //儿子节点初始化为空
		id=-1;
	}
}; //root=根节点

trie *root=new trie();

int dx[]={-1,-1,0,1,1,1,0,-1};
int dy[]={0,1,1,1,0,-1,-1,-1}; //搜索方向,暴力枚举每个点八个方向
int ans[MAXN][3],visit[MAXN],L,C,W; //第i个单词的位置=(ans[i][1],ans[i][2]),方向为ans[i][0]

string c[MAXN],word; //保存所有字符串的矩阵

void build(string s,int num) //将编号为num的字符串s插入trie树中
{
	int i;
	trie *p=root; //初始时p指向根节点
	for(i=0;i<s.size();i++)
	{
		if(p->child[s[i]-'A']==NULL) //无现成的字符串对应位置儿子节点
			p->child[s[i]-'A']=new trie();
		p=p->child[s[i]-'A']; //将指针移向当前节点下面的对应儿子节点
	}
	p->id=num; //记下终止节点的对应单词编号
}

void search(int sx,int sy,int dir) //搜索trie树,单词起点(sx,sy),延伸方向为dir
{
	int xx=sx,yy=sy; //当前单词字母的坐标(xx,yy)
	trie *point=root; //point指向当前trie树的节点
	while(xx>=0&&xx<L&&yy>=0&&yy<C) //坐标未越界
	{
		if(!point->child[c[xx][yy]-'A']) //到达了终止节点,但单词还没结束
			break;
		else
			point=point->child[c[xx][yy]-'A']; //指针向该节点下方移动
		if(point->id!=-1) //该节点为终止节点
		{
			if(visit[point->id]==0)
			{
				visit[point->id]=1;
				ans[point->id][0]=dir; //记录下该单词的开始坐标、方向
				ans[point->id][1]=sx;
				ans[point->id][2]=sy;
			}
		}
		xx+=dx[dir]; //移动坐标
		yy+=dy[dir];
	}
}

int main()
{
	int i,j,k;
	scanf("%d%d%d",&L,&C,&W);
	for(i=0;i<L;i++)
	{
		cin>>c[i];
	}
	for(i=0;i<W;i++)
	{
		cin>>word;
		build(word,i); //将单词插入trie树
	}
	for(i=0;i<L;i++) //枚举起点(i,j)
		for(j=0;j<C;j++)
			for(k=0;k<8;k++) //暴力枚举单词存在的方向
				search(i,j,k);
	for(i=0;i<W;i++)
		printf("%d %d %c\n",ans[i][1],ans[i][2],ans[i][0]+'A');
	return 0;
}


 

 

[POJ 1204]Word Puzzles(Trie树暴搜),布布扣,bubuko.com

[POJ 1204]Word Puzzles(Trie树暴搜)

标签:ac自动机   trie树   trie图   字符串   

原文地址:http://blog.csdn.net/qpswwww/article/details/37869197

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!