Zju1290 Word-Search Wonder(http://begin.lydsy.com/JudgeOnline/problem.php?id=2768)
来源:互联网 发布:拟线性偏好知乎 编辑:程序博客网 时间:2024/05/01 17:53
2768: Zju1290 Word-Search Wonder
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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个,水平、垂直、两个对角线,外加每种两个方向。求给定的词是否在字母矩阵中,并求开始和结束的坐标。
``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.
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.
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
15EDEEEDISKEESEEEECEEEEEEEEDISCDISKDISP0
Sample Output
1,2 4,22,1 2,4Not found
HINT
Source
Trie系列
题解:
分类是trie树,写了一个dfs过了,醉!
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 char a[110][110],s[1000]; 6 int n,m,i,j; 7 int x1,y1,x2,y2; 8 bool dfs(int deep,int x,int y,int dx,int dy) 9 {10 if (deep>strlen(s+1)){x2=x-dx; y2=y-dy; return true;}11 if (x>n || x<=0 || y>n|| y<=0) return false;12 if (a[x][y]!=s[deep]) return false;13 return dfs(deep+1,x+dx,y+dy,dx,dy);14 }15 bool find (char *s)16 {17 for (int i=1; i<=n; i++)18 for (int j=1; j<=n; j++)19 if (a[i][j]==s[1])20 {21 for (int k=-1; k<=1; k++) 22 for (int kk=-1; kk<=1; kk++)23 if (k!=0 || kk!=0)24 if (dfs(1,i,j,k,kk))25 {26 x1=i; y1=j; return true;27 } 28 } 29 return false;30 }31 void work()32 {33 cin>>n;34 for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) cin>>a[i][j];35 while (true) 36 {37 scanf("%s",s+1);38 if (s[1]=='0') break;39 if (find(s)) cout<<x1<<','<<y1<<' '<<x2<<','<<y2<<endl; else cout<<"Not found"<<endl; 40 } 41 }42 int main()43 {44 work(); 45 }
0 0
- Zju1290 Word-Search Wonder(http://begin.lydsy.com/JudgeOnline/problem.php?id=2768)
- http://begin.lydsy.com/JudgeOnline/problem.php?id=2770(PKU2503 Babelfish)
- http://begin.lydsy.com/JudgeOnline/problem.php?id=2774(poi病毒)
- http://211.67.33.62/JudgeOnline/problem.php?id=1014,排序二叉树
- http://acm.nyist.net/JudgeOnline/problem.php?pid=290
- http://acm.nyist.net/JudgeOnline/problem.php?pid=434
- http://acm.nyist.net/JudgeOnline/problem.php?pid=13
- http://acm.nyist.net/JudgeOnline/problem.php?pid=239
- http://acm.nyist.net/JudgeOnline/problem.php?pid=129
- http://acm.nyist.net/JudgeOnline/problem.php?pid=431
- http://acm.nyist.net/JudgeOnline/problem.php?pid=301
- http://acm.nyist.net/JudgeOnline/problem.php?pid=451
- http://acm.nyist.net/JudgeOnline/problem.php?pid=459
- http://acm.nyist.net/JudgeOnline/problem.php?pid=20
- http://acm.nyist.net/JudgeOnline/problem.php?pid=267
- http://acm.nyist.net/JudgeOnline/problem.php?pid=305
- http://acm.nyist.net/JudgeOnline/problem.php?pid=309
- http://acm.nyist.net/JudgeOnline/problem.php?pid=325
- bzoj3942——2016——3——15
- 2016——3——16 kmp 7题
- 编译fdk-aac for ios
- 几种哈希算法原理(转发)
- begin lydsy 2731
- Zju1290 Word-Search Wonder(http://begin.lydsy.com/JudgeOnline/problem.php?id=2768)
- 这些时间投,更容易被HR“翻牌子”!
- 关键词匹配(Ac自动机模板题)
- BufferKnife及插件使用
- 利用 ajax 像后台传递数组
- Android 优秀文章收集整理集合
- 5个最佳的Android测试框架(带示例)
- http://begin.lydsy.com/JudgeOnline/problem.php?id=2770(PKU2503 Babelfish)
- 10天精通Sass 之 Sass运算