【POJ】1204 Word Puzzles

题目链接:https://vjudge.net/problem/POJ-1204

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.

Analysis

这一道题还是用字典树做,所以我们先做一个小分析。这个大型的矩阵,如果我们要存储下来,那么我们要用(1000*1000*8*1000)的内存,那么就会超内存,所以我们采用的策略是把要查找的字符串放到trie数组里面,在拿矩阵进行匹配,还要注意方向。
这一道题有特殊判断,所以只要找到一组答案就行了。

Code

const
  dx:array[1..8]of longint=(-1,-1,0,1,1,1,0,-1);
  dy:array[1..8]of longint=(0,1,1,1,0,-1,-1,-1);//定义方向数组
var
  tr:array[0..100000,0..25]of longint;//存要查找的边
  tot,n,m,k,i,j:longint;///tot是节点个数
  s,w:array[1..1000]of ansistring;//
  ed:array[0..100000]of longint;
  ansx,ansy,ansd:array[1..1000]of longint;

procedure build(s:ansistring;num:longint);//建树模板,这次要在尾节点标记这个是那个字符串的,但是题目没有说有没有重复,所以就开一个数组存储为节点
var
  i,k,p:longint;
begin
  k:=1;
  for i:=1 to length(s) do 
    begin
      p:=ord(s[i])-65;
      if tr[k][p]=0 then 
        begin
          inc(tot);
          tr[k][p]:=tot;
        end;
      k:=tr[k][p];
    end;
  ed[k]:=num;//标记尾节点
end;

procedure find(x,y:longint);//查找第x,y这个方格
var
  id,k,i,d,tx,ty:longint;
begin
  for d:=1 to 8 do //枚举方向
    begin
      k:=1;//从根节点开始查找
      for i:=1 to 1000 do //向每个方向走
        begin
          tx:=x+(i-1)*dx[d];
          ty:=y+(i-1)*dy[d];
          if(tx<1)or(ty<1)or(tx>n)or(ty>m)then break;//判断是否有出界现象。
          id:=ord(s[tx][ty])-65;
          if tr[k][id]=0 then break;//如果没有这个儿子说明就没有这个单词
          k:=tr[k][id];//向下查找
          if ed[k]>0 then //如果当前的是某一个单词的结尾那个赋值为答案
            begin
              ansx[ed[k]]:=x;
              ansy[ed[k]]:=y;
              ansd[ed[k]]:=d;//这样做就省下了扫描的时间,用空间换时间
            end;
        end;
    end;
end;

begin
  tot:=1;
  readln(n,m,k);
  for i:=1 to n do readln(s[i]);
  for i:=1 to k do 
  begin 
    readln(w[i]);
    build(w[i],i);
  end;
  for i:=1 to n do 
    for j:=1 to m do 
      find(i,j);
  for i:=1 to k do 
    writeln(ansx[i]-1,' ',ansy[i]-1,' ',chr(ansd[i]+64));
end.

猜你喜欢

转载自blog.csdn.net/Phantom_stars/article/details/78886952