Description
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
Output
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
//题目是输入一个字符矩阵,然后输入一系列字符串找出,字符串的起始位置以及方向
#include"iostream"
#include "string.h"
using namespace std;
bool search_way(char**b,int num,char**a,int bl1,int dir,int c,int d)
{ int dx=0;int dy=0;//方向修改量
char cdir;
int cr=c;int dr=d;//保存
int len1=strlen(b[num]);
int len2=len1;
while(len2)
{ if(len2!=len1)
{
c=c+dx;
d=d+dy;
if(c<0||(c>=bl1)||d<0||(d>=strlen(a[c])))
{
return false;
}
if(b[num][len1-len2]!=a[c][d])
{
return false;
}
}
len2--;
} switch (dir)
{
case 0:dy=1;cdir='C';break;
case 1:dx=1;dy=1;cdir='D';break;
case 2:dx=1;cdir='E';break;
case 3:dx=1;dy=-1;cdir='F';break;
case 4:dy=-1;cdir='G';break;
case 5:dx=-1;dy=-1;cdir='H';break;
case 6:dx=-1;cdir='A';break;
case 7:dx=-1;dy=1;cdir='B';break;
}
cout<<cr<<" "<<dr<<" "<<cdir<<endl;
return true;
}
void findway(char**b,int num,char**a,int bl1)
{
int dir=0;
for(int i=0;i<bl1;i++)
for(int j=0;j<strlen(a[i]);j++)
{
if(b[num][0]==a[i][j])//先找到第一个合适的位置,如果能够匹配就 return ;如果不能继续找 找的时候需要 考虑边界范围 8个方向深度搜索 定义一个dir 分别对应north...
{ while(dir<8)
{
if(search_way(b,num,a,bl1,dir,i,j))
return ;
dir++;
}
dir=0;
}
}
}
int main()
{
int fa,fb,fc;
cin>>fa>>fb>>fc;
int num=fc;
char **a=new char*[fa];
for(int i=0;i<fa;i++)
a[i]=new char[fb];
char **b=new char*[fc];
for(i=0;i<fc;i++)
b[i]=new char[i];
for(i=0;i<fa;i++)
for(int j=0;j<fb;j++)
cin>>a[i][j];
while(fc)
{
cin>>b[num-fc];
findway(b,num-fc,a,fa);
fc--;
}
return 0;
}