本文主要是介绍UVA10010(八方向暴力枚举),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18656
Description
Where's Waldorf?
Given a m by n grid of letters, ( $1 \leq m,n \leq 20$), 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, $1 \leq
m,n \leq 50$ in decimal notation on a single line. The next m lines contain n letters 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 ( $1 \leq k \leq 20$). 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的字符矩阵,然后q次询问,每次问该单词在矩阵中出现的位置,输出第一个字母的位置即可。如果有多个匹配,那么输出第一个字母最靠近左上这个方向的第一个字母坐标。
其实这道题简化了不少,它要求匹配时所有单词必须在一条直线上,相当于如果当前点和第一个字母匹配后,你只要对当前点的八个方向去暴力枚举判断即可。刚开始想多了,忽略了一条直线这个条件,就想各种大搜索(因为可能存在折线匹配情况)。
最后要注意下它的要求,起初输入完情况数T后,要来个getline吃掉空行。两个连续情况要加一个空行。对于最后一组情况,不要输出多余空行。
暴力了两百多行,我也是醉了·····
完整代码:
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#include <ctype.h>
using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
string str;
const int maxn = 101;
char maps[maxn][maxn];
string t;
int n , m;bool check(char ch1 , char ch2)
{// cout << (char)toupper(ch1) << " " << (char)tolower(ch1) << " " << ch2 << endl;if((char)toupper(ch1) == ch2 || (char)tolower(ch1) == ch2)return true;elsereturn false;
}int main()
{#ifdef DoubleQfreopen("in.txt","r",stdin);#endifint T;cin >> T;string sss;getline(cin , sss);while(T--){cin >> n >> m;sizeof(maps , 0 , sizeof(maps));for(int i = 0 ; i < n ; i ++){cin >> str;for(int j = 0 ; j < m ; j++)maps[i][j] = str[j];}int q;cin >> q;while(q--){cin >> t;int len = t.length();int all = 0;for(int i = 0 ; i < n ; i ++){for(int j = 0 ; j < m ; j ++){if(check(maps[i][j] , t[0])){if(i - len + 1 >= 0 && j - len + 1 >= 0){int flag = 0;for(int k1 = i , k2 = j , k = 0 ; k < len ; k1 -- , k2 -- , k ++){if(!check(maps[k1][k2] , t[k])){flag = 1;break;}}if(flag == 0){cout << i + 1 << " " << j + 1 << endl;all = 1;break;}}if(i - len + 1 >= 0 && j + len - 1 < m){int flag = 0;for(int k1 = i , k2 = j , k = 0 ; k < len ; k1 -- , k2 ++ , k ++){if(!check(maps[k1][k2] , t[k])){flag = 1;break;}}if(flag == 0){cout << i + 1 << " " << j + 1 << endl;all = 1;break;}}if(i + len - 1 < n && j - len + 1 >= 0){int flag = 0;for(int k1 = i , k2 = j , k = 0 ; k < len ; k1 ++ , k2 -- , k ++){if( ! check ( maps[k1][k2] , t[k] ) ){flag = 1;break;}}if(flag == 0){cout << i + 1 << " " << j + 1 << endl;all = 1;break;}}if(i + len - 1 < n && j + len - 1 < m){int flag = 0;for(int k1 = i , k2 = j , k = 0 ; k < len ; k1 ++ , k2 ++ , k ++){if(!check(maps[k1][k2] , t[k])){flag = 1;break;}}if(flag == 0){cout << i + 1 << " " << j + 1 << endl;all = 1;break;}}if(i - len + 1 >= 0){int flag = 0;for(int k1 = i , k = 0 ; k < len ; k1 -- , k ++){if(!check(maps[k1][j] , t[k]) ){flag = 1;break;}}if(flag == 0){cout << i + 1 << " " << j + 1 << endl;all = 1;break;}}if(i + len - 1 < n){int flag = 0;for(int k1 = i , k = 0 ; k < len ; k1 ++ , k ++){if(!check(maps[k1][j] , t[k]) ){flag = 1;break;}}if(flag == 0){cout << i + 1 << " " << j + 1 << endl;all = 1;break;}}if(j - len + 1 >= 0){int flag = 0;for(int k1 = j , k = 0 ; k < len ; k1 -- , k ++){if(!check(maps[i][k1] , t[k]) ){flag = 1;break;}}if(flag == 0){cout << i + 1 << " " << j + 1 << endl;all = 1;break;}}if(j + len - 1 < m){int flag = 0;for(int k1 = j , k = 0 ; k < len ; k1 ++ , k ++){if(!check(maps[i][k1] , t[k]) ){flag = 1;}}if(flag == 0){cout << i + 1 << " " << j + 1 << endl;all = 1;break;}}continue;}}if(all)break;}}if(T != 0)cout << endl;}
}
这篇关于UVA10010(八方向暴力枚举)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!