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

UVA 10010- Where's Waldorf?(八方向寻找字符串)

时间:2015-01-02 17:36:45      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

Where‘s Waldorf?
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

技术分享


  Where‘s Waldorf? 

Given a m by n grid of letters, ( 技术分享), and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid. A word can match the letters in the grid regardless of case (i.e. upper and lower case letters are to be treated as the same). The matching can be done in any of the eight directions either horizontally, vertically or diagonally through the grid.

Input 

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The input begins with a pair of integers, m followed by n技术分享 in decimal notation on a single line. The next m lines contain nletters each; this is the grid of letters in which the words of the list must be found. The letters in the grid may be in upper or lower case. Following the grid of letters, another integer k appears on a line by itself ( 技术分享). The next k lines of input contain the list of words to search for, one word per line. These words may contain upper and lower case letters only (no spaces, hyphens or other non-alphabetic characters).

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

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

Sample Input 

1

8 11
abcDEFGhigg
hEbkWalDork
FtyAwaldORm
FtsimrLqsrc
byoArBeDeyv
Klcbqwikomk
strEBGadhrb
yUiqlxcnBjf
4
Waldorf
Bambi
Betty
Dagbert

Sample Output 

2 5
2 3
1 2
7 8


题意:给出n行m列的字符网格,然后给出k个单词,要求在字符网格中找出该单词,可以从八个方向找。如果能找到完整的单词,就输出坐标。单词大小写不计

思路:和DFS不同,这里只是按照一个方向一个方向的走。只要找到该单词的第一个首字母,就在这八个方向都搜一次,看是不是这个单词。


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;
char str[110][110];
int n,m;
int x,y;
int jx[]= {0,0,1,-1,1,-1,1,-1};
int jy[]= {1,-1,0,0,1,-1,-1,1};
void findx(char *a)
{
    int i,j,k;
    int cnt;
    int dx,dy;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[0]==str[i][j])
            {
                for(k=0;k<8;k++)
                {
                    dx=i;
                    dy=j;
                    cnt=0;
                    while(a[cnt]==str[dx][dy]&&a[cnt])
                    {
                        dx+=jx[k];
                        dy+=jy[k];
                        cnt++;
                    }
                    if(a[cnt]==0)
                    {
                        x=i;
                        y=j;
                        return ;
                    }
                }
            }
        }
    }
}

int main()
{
    int T,i,j,k,t;
    char s[110];
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        if(t>=2)
            printf("\n");
        scanf("%d %d",&n,&m);
        for(i=0;i<n;i++)
        {
            scanf("%s",str[i]);
            for(j=0;j<m;j++)
            if(str[i][j]>='A'&&str[i][j]<='Z')
            str[i][j]=str[i][j]+32;
        }
        scanf("%d",&k);
        while(k--)
        {
            scanf("%s",s);
            for(i=0;i<strlen(s);i++)
            if(s[i]>='A'&&s[i]<='Z')
                s[i]=s[i]+32;
            findx(s);
            printf("%d %d\n",x+1,y+1);
        }
    }
    return 0;
}


UVA 10010- Where's Waldorf?(八方向寻找字符串)

标签:

原文地址:http://blog.csdn.net/u013486414/article/details/42342183

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