2768: Zju1290 Word-Search Wonder

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 4  Solved: 2
[Submit][Status][Web Board]

Description

The Pyrates Restaurant was starting to fill up as Valentine McKee walked in. She scanned the crowd for her sister, brother-in-law, and nephew. Seeing her sister waving from the far end of the restaurant, she made her way back to their booth. ``Hi, Valentine,'' her sister and brother-in-law, Niki and Dennis Chapman, greeted her.
``Hi, guys,'' she replied. ``What are you doing, Wade?'' she asked her nephew. He was busy working on one of the restaurant's activity sheets with a crayon.
``I'm doing a word search game,'' Wade explained. ``I have to find all of these words in this big mess of letters. This is really hard.'' Wade looked intently at the paper in front of him.
``Can I help?'' asked Valentine, looking across the table at the activity sheet.
``Sure. These are the words we're looking for. They're the names of different kinds of Planes, Trains, and Automobiles.''
在字母矩阵找找单词游戏,找的方向有8个,水平、垂直、两个对角线,外加每种两个方向。求给定的词是否在字母矩阵中,并求开始和结束的坐标。

Input

The first line of input will specify the length (in characters) of the sides of the letter matrix (the matrix of letters will be square). The length, l, will be in the range 1 <= l <= 100. The next l lines of input will be the matrix itself, each line will contain l uppercase letters.

A list of words will follow. Each word will be on a line by itself; there will be 100 or fewer words. Each word will be 100 or fewer characters long, and will only contain uppercase letters.

The final line of input will contain a single zero character.

Output

Your program should attempt to find each word from the word list in the puzzle. A word is ``found'' if all the characters in the word can be traced in a single (unidirectional) horizontal, vertical, or diagonal line in the letter matrix. Words may not ``wrap around'' rows or columns, but horizontal and diagonal words may proceed from right to left (``backwards''). For each word that is found, your program should print the coordinates of its first and last letters in the matrix on a single line, separated by a single space. Coordinates are pairs of comma-separated integers (indexed from 1), where the first integer specifies the row number and the second integer specifies the column number.

If a word is not found, the string ``Not found'' should be output instead of a pair of coordinates.

Each word from the input can be ``found'' at most once in the puzzle.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input

1

5
EDEEE
DISKE
ESEEE
ECEEE
EEEEE
DISC
DISK
DISP
0

Sample Output

1,2 4,2
2,1 2,4
Not found

HINT

 

Source

Trie系列

题解:

  分类是trie树,写了一个dfs过了,醉!

 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char a[][],s[];
int n,m,i,j;
int x1,y1,x2,y2;
bool dfs(int deep,int x,int y,int dx,int dy)
{
if (deep>strlen(s+)){x2=x-dx; y2=y-dy; return true;}
if (x>n || x<= || y>n|| y<=) return false;
if (a[x][y]!=s[deep]) return false;
return dfs(deep+,x+dx,y+dy,dx,dy);
}
bool find (char *s)
{
for (int i=; i<=n; i++)
for (int j=; j<=n; j++)
if (a[i][j]==s[])
{
for (int k=-; k<=; k++)
for (int kk=-; kk<=; kk++)
if (k!= || kk!=)
if (dfs(,i,j,k,kk))
{
x1=i; y1=j; return true;
}
}
return false;
}
void work()
{
cin>>n;
for (int i=; i<=n; i++) for (int j=; j<=n; j++) cin>>a[i][j];
while (true)
{
scanf("%s",s+);
if (s[]=='') break;
if (find(s)) cout<<x1<<','<<y1<<' '<<x2<<','<<y2<<endl; else cout<<"Not found"<<endl;
}
}
int main()
{
work();
}

Zju1290 Word-Search Wonder(http://begin.lydsy.com/JudgeOnline/problem.php?id=2768)的更多相关文章

  1. http://begin.lydsy.com/JudgeOnline/problem.php?id=2770(PKU2503 Babelfish)

    2770: PKU2503 Babelfish Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2  Solved: 2[Submit][Status][ ...

  2. http://begin.lydsy.com/JudgeOnline/problem.php?id=2774(poi病毒)

    2774: Poi2000 病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 5  Solved: 4[Submit][Status][Web Boa ...

  3. Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  4. LeetCode: Word Search 解题报告

    Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...

  5. [LeetCode] Word Search II 词语搜索之二

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. [LeetCode] Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  7. Leetcode: word search

    July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...

  8. Word Search I & II

    Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  9. 【leetcode】Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

随机推荐

  1. 【使用Itext处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)】

    iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...

  2. VIJOS P1647 不差钱 SBT

    [描述] 同学们一起看了小品<不差钱>,LX神突发奇想,想刁难一下十八居士,他让十八居士模拟一下点菜的过程. [输入格式] 输入第一行为一个数price,表示价钱大于price的菜赵本山都 ...

  3. getWriter() has already been called for this response 的解决办法

    getWriter() has already been called for this response response已经被其他对象调用了,导致无法继续使用如下 类似的方法 PrintWrite ...

  4. iOS开发-正则表达式3种形式

    转至:http://www.cnblogs.com/GarveyCalvin/p/4250145.html iOS开发-正则表达式的使用方法 前 言:在表单验证中,我们经常会使用到正则,因为我们需要用 ...

  5. 转 :Vim文件编码识别与乱码处理

    Vim文件编码识别与乱码处理   在 Vim 中,有四个与编码有关的选项,它们是:fileencodings.fileencoding.encoding 和 termencoding.在实际使用中,任 ...

  6. HDU 1166 敌兵布阵(树状数组)

    之前用过了线段树的做法,树状数组的也补上吧 #include<iostream> #include<cstdio> #include<cstring> using ...

  7. 我的第一个Android项目之环境搭建

    开发IDE Android Studio2.0 + Genymotion + JDK1.8 网盘地址:http://pan.baidu.com/s/1kUSVqaN Android Studio 我的 ...

  8. Android Studio实现Service AIDL

    Android Studio实现Service AIDL [日期:2015-01-02] 来源:Linux社区  作者:teenyboy [字体:大 中 小]       今天要开发过程中要用到AID ...

  9. Apache不能启动解决办法

    这是我这两天频繁遇到的问题.Apache服务器还真是问题少年!任何点改动都可能导致它无法使用. 原因一:80端口占用例如IIS,另外就是迅雷.我的apache服务器就是被迅雷害得无法启用! 原因二:软 ...

  10. springmvc配置首页的方式

    <mvc:view-controller path="/" view-name="redirect:/user/loginUI" />