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

Where‘s Waldorf?

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

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?(八方向寻找字符串)

时间: 2024-10-05 20:12:25

UVA 10010- Where's Waldorf?(八方向寻找字符串)的相关文章

uva 10010 - Where&#39;s Waldorf?

#include<iostream> #include<cctype> #include<algorithm> using namespace std; char a[55][55]; string s[22]; int T,m,n,k; int mx[] = {1, 1, 1, 0, 0, -1, -1, -1}; int my[] = {1, 0, -1, 1, -1, 1, 0, -1}; bool in(int x,int y){ return x>=1&

UVA 之10010 - Where&#39;s Waldorf?

 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

UvaOJ 10010 Where&#39;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

unity3d easytouch计算摇杆旋转角度以及摇杆八方向控制角色

在写第三人称控制的时候,一开始在电脑测试是用WASD控制角色 后来需要发布到手机上,于是就加了一个摇杆 键盘控制角色的代码已经写好了,角色八方向移动 如果按照传统的大众思路来控制的话,是达不到我想要的效果的,传统控制思路代码如下: 1 //当摇杆处于移动状态时,角色开始奔跑 2 void OnJoystickMove(MovingJoystick move) 3 { 4 if (move.joystickName != "EasyJoystick") 5 { 6 return; 7 }

hdu 1241 Oil Deposits(八方向简单深搜,对新手挺经典)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12461 Accepted Submission(s): 7245 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground

UVA 12718 Dromicpalin Substrings(寻找字符串连续子串的回文)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4456 题意:寻找给出字符串的字串中是回文的个数(注意:字串中字母换位置后能组成回文也要算在内,例如:aab之类的可以换位置为:aba 也是一个回文). 思路:只需统计每个字母出现的次数,再统计出现次数中

GameUnity 2.0 文档(三) 纸片人八方向

DirectSprite类 有别于 上篇文档出现的 AnimationSprite类 (从头播放到尾) 这个类根据 path的图,如果是 8*8 64个图 八方向,可以设置长宽和 角度 角度 代表 8 个方向. Camera2DAngle类 摄像机 旋转,2d 模仿3d 观察 遍历全身. Move2DAngle 类 八方向寻路 动画控制 //////未完,待续

【cocos2d-x 3.2 - JS -横版摇杆八方向移动】

花了少许时间利用了一下cocos2dx 3.2Js版本+cocosstiduo2.0.6做了一个横版摇杆八方向的demo: 目前到今天为止,Js版本为3.2稳定版本,官网下载传送门: http://www.cocos2d-x.org/filedown/cocos2d-js-v3.2.zip Js环境搭载传送门: [cocos2d-x 3.0 - JS -Mac配置篇] 轻松搭建完后,开始用JS写一个横版摇杆动作游戏的Demo,听起来貌似很高大上~~. 首先要做好几个准备: 1,角色精灵,我准备了

寻找字符串中出现字符最多的次数,出现次数最多的字符,出现次数最多的字符的索引

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>demo</title> <link rel="stylesheet" href="css.css"> <script src="js.js"></script>