uva10010

来源:互联网 发布:学通网络ps序列号 编辑:程序博客网 时间:2024/05/20 14:43

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=951

10010 - Where's Waldorf?

Time limit: 3.000 seconds

Where'sWaldorf? Given a m by n grid ofletters, ($1 \leq m,n \leq 20$), and a list of words, find the location in the gridat which the word can be found. A word matches a straight,uninterrupted line of letters in the grid. A word can match theletters in the grid regardless of case (i.e. upper and lower caseletters are to be treated as the same). The matching can be done inany of the eight directions either horizontally, vertically ordiagonally through the grid.

Input

The input begins with a single positive integer on a line byitself indicating the number of the cases following, each of themas described below. This line is followed by a blank line, andthere is also a blank line between two consecutive inputs.

The input begins with a pair of integers, m followed byn, $1 \leq
m,n \leq 50$in decimal notation on a single line. The nextm lines contain n letters each; this is the grid ofletters in which the words of the list must be found. The lettersin the grid may be in upper or lower case. Following the grid ofletters, another integerk appears on a line by itself ($1 \leq k \leq 20$). The next klines of input contain the listof words to search for, one word per line. These words may containupper and lower case letters only (no spaces, hyphens or othernon-alphabetic characters).

Output

For each test case, the output must follow the descriptionbelow. The outputs of two consecutive cases will be separated by ablank line.

For each word in the word list, a pair of integers representingthe location of the corresponding word in the grid must be output.The integers must be separated by a single space. The first integeris the line in the grid where the first letter of the given wordcan be found (1 represents the topmost line in the grid, andm represents the bottommost line). The second integer is thecolumn in the grid where the first letter of the given word can befound (1 represents the leftmost column in the grid, and nrepresents the rightmost column in the grid). If a word can befound more than once in the grid, then the location which is outputshould correspond to the uppermost occurence of the word (i.e. theoccurence which places the first letter of the word closest to thetop of the grid). If two or more words are uppermost, the outputshould correspond to the leftmost of these occurences. All wordscan be found at least once in the grid.

Sample Input

18 11abcDEFGhigghEbkWalDorkFtyAwaldORmFtsimrLqsrcbyoArBeDeyvKlcbqwikomkstrEBGadhrbyUiqlxcnBjf4WaldorfBambiBettyDagbert

Sample Output

2 52 31 27 8



Miguel Revilla
2000-08-22这题以前做过了,不过当时不大理解。。这次理解多了。。嘻嘻。。。题意:找到字符串在字符矩阵里的位置,并输出其头字母的坐标。我们先找到首字母的位置,

然后按照八个方向当中的一个搜索,如果能找到完整的字符串,就输出坐标。与DFS每个点按照

八个方向搜索不同的是,这里只是按照一个方向一个方向地搜索。所以搜索函数也稍有不同。

此题必须从八个方向来查找,依次是从当前位置向左上方、上方、右上方、左边、右边、左下方、下方、右下方,由于本题的要求,这个查找顺序不能改变。

不过我试过几种搜索路径发现还是可以A。。。看来这题数据弱了。。

输出时,每组测试数据之间没有空行;在每组测试数据中,每种情况(case)之间才有空行

The outputs of twoconsecutive cases will be separated by a blankline.

 

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#define MAXN 100
int cas,m,n,q;
int x=0,y=0;
char r[MAXN][MAXN];
//const int dx[]={-1,-1,0,1,1,-1,0,1};//搜索的方向最上top(correspond tothe uppermost) 然后是最左然后是右再最后是下
//const int dy[]={0,1,1,1,0,-1,-1,-1};//If two or more words areuppermost, the output should correspond to the leftmost
const int dx[]={-1,0,1,-1,1,-1,0,1};
const int dy[]={1,1,1,0,0,-1,-1,-1};
void search(const char*a,int &x,int&y)
{
 int pos,nx,ny;
 int i,j,k;
 for(i=1;i<=m;i++)
 {
  for(j=1;j<=n;j++)
  {
   if(r[i][j]==a[0])//从a[]字符串第一个相同的字符开始
   {
    for(k=0;k<8;k++)//朝8个方向搜索
    {
     pos=0;//待查找字符在字符串的首位置
     nx=i;
     ny=j;
     while(a[pos]&&a[pos]==r[nx][ny])
     {
      nx+=dx[k];//下一个搜索的方向的坐标
      ny+=dy[k];//下一个搜索的方向的坐标
      pos++;//pos后移一位
     }
     if(a[pos]=='\0')//如果a[pos]=='\0',表示要查找的字符串找到了,记录x,y的坐标
     {
      x=i;
      y=j;
      return;
     }
    }
   }
  }
 }
}
int main()
{
 int i,j;
 scanf("%d",&cas);//输入测试数据
 for(int tt=1;tt<=cas;tt++)
 {
  charword[MAXN];//word[]是输入的字符串
  if(tt>=2)
   printf("\n");//Theoutputs of two consecutive cases will be separated by a blankline.
  memset(r,0,sizeof(r));
  scanf("%d%d",&m,&n);//输入m表示行数,n表示列数
  for(i=1;i<=m;i++)//共有m行也就是有m行的测试数据,每行有n个字符
  {//之所以从1开始是因为输出的行列数下标是从1开始的
   scanf("%s",word);
   for(j=1;j<=n;j++)
   {
    r[i][j]=tolower(word[j-1]);//一律按照小写字母表示
   }
  }
  scanf("%d",&q);//q为要查找的测试数据的个数
  while(q--)
  {
   scanf("%s",word);//输入待查找的字符串
   intlen=strlen(word);//求字符串的长度
   for(i=0;i<len;i++)
   {
    word[i]=tolower(word[i]);//一律按照小写字母表示
   }
   search(word,x,y);//调用search函数开始搜索(x=0,y=0)
   printf("%d%d\n",x,y);//输出最后的坐标
  }
 }
 return 0;
}

原创粉丝点击