POJ-1204-Word Puzzles

POJ-1204-Word Puzzles

http://poj.org/problem?id=1204

字典树加深搜

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;  
char map[1005][1005];
int n1,n2;
int xx,yy;
int dirx[8]={-1,-1,0,1,1,1,0,-1};
int diry[8]={0,1,1,1,0,-1,-1,-1};
int out[1005][3];
struct node
{
	int count;
	node *childs[26];
	node()
	{
		count=0;
		for(int i=0;i<26;i++)
		childs[i]=NULL;
	}
};
node *root,*current,*newnode;
int go(int x,int y)
{
	if(0<=x&&x<n1&&0<=y&&y<n2)
	return 1;
	return 0;
}
void insert(char *str,int t)
{
	int i,m;
	current=root;
	for(i=0;i<strlen(str);i++)
	{
		m=str[i]-'A';
		if(current->childs[m]!=NULL)
		current=current->childs[m];
		else
		{
			newnode=new node;
			current->childs[m]=newnode;
			current=newnode;
		}
	}
	current->count=t;
}
void dfs(node *head,int i,int j,int k)
{
	if(head==NULL)
	return;
	if(head->count!=0)
	{
        out[head->count][0]=xx;
		out[head->count][1]=yy;
		out[head->count][2]=k;
		head->count=0;
	}
	if(!go(i,j))
	return;
	dfs(head->childs[map[i][j]-'A'],i+dirx[k],j+diry[k],k);
}
int main()
{
	int i,j,k,t;
	char str[1005];
	root=new node;
	scanf("%d%d%d",&n1,&n2,&t);
	for(i=0;i<n1;i++)
	scanf("%s",map[i]);
	for(i=1;i<=t;i++)
	{
		scanf("%s",str);
		insert(str,i);
	}
	for(i=0;i<n1;i++)
	for(j=0;j<n2;j++)
	for(k=0;k<8;k++)
	{
		xx=i;
		yy=j;
		dfs(root,i,j,k);
	}
	for(i=1;i<=t;i++)
	printf("%d %d %c\n",out[i][0],out[i][1],out[i][2]+'A');
	system("pause");
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值