UVA10010(八方向暴力枚举)

2024-09-07 19:58
文章标签 方向 枚举 暴力 uva10010

本文主要是介绍UVA10010(八方向暴力枚举),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Where's Waldorf?
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
题目链接: 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(八方向暴力枚举)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1146018

相关文章

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn

嵌入式方向的毕业生,找工作很迷茫

一个应届硕士生的问题: 虽然我明白想成为技术大牛需要日积月累的磨练,但我总感觉自己学习方法或者哪些方面有问题,时间一天天过去,自己也每天不停学习,但总感觉自己没有想象中那样进步,总感觉找不到一个很清晰的学习规划……眼看 9 月份就要参加秋招了,我想毕业了去大城市磨练几年,涨涨见识,拓开眼界多学点东西。但是感觉自己的实力还是很不够,内心慌得不行,总怕浪费了这人生唯一的校招机会,当然我也明白,毕业

【Rust练习】12.枚举

练习题来自:https://practice-zh.course.rs/compound-types/enum.html 1 // 修复错误enum Number {Zero,One,Two,}enum Number1 {Zero = 0,One,Two,}// C语言风格的枚举定义enum Number2 {Zero = 0.0,One = 1.0,Two = 2.0,}fn m

理解分类器(linear)为什么可以做语义方向的指导?(解纠缠)

Attribute Manipulation(属性编辑)、disentanglement(解纠缠)常用的两种做法:线性探针和PCA_disentanglement和alignment-CSDN博客 在解纠缠的过程中,有一种非常简单的方法来引导G向某个方向进行生成,然后我们通过向不同的方向进行行走,那么就会得到这个属性上的图像。那么你利用多个方向进行生成,便得到了各种方向的图像,每个方向对应了很多

枚举相关知识点

1.是用户定义的数据类型,为一组相关的常量赋予有意义的名字。 2.enum常量本身带有类型信息,即Weekday.SUN类型是Weekday,编译器会自动检查出类型错误,在编译期间可检查错误。 3.enum定义的枚举类有什么特点。         a.定义的enum类型总是继承自java.lang.Enum,且不能被继承,因为enum被编译器编译为final修饰的类。         b.只能定义

【C语言】结构体、枚举、联合体

【C语言】结构体、枚举、联合体 文章目录 @[TOC](文章目录) 前言一、结构体声明1.一般格式2.typedef 重命名结构体类型定义变量 二、结构体数组三、结构体与指针及函数传参四、结构体传参五.结构体在内存的存储六、参考文献总结 前言 使用工具: 1.编译器:VScode 2.C Primer Plus 第六版-1 提示:以下是本篇文章正文内容,下面案例可供参考

[SWPUCTF 2021 新生赛]web方向(一到六题) 解题思路,实操解析,解题软件使用,解题方法教程

题目来源 NSSCTF | 在线CTF平台因为热爱,所以长远!NSSCTF平台秉承着开放、自由、共享的精神,欢迎每一个CTFer使用。https://www.nssctf.cn/problem   [SWPUCTF 2021 新生赛]gift_F12 这个题目简单打开后是一个网页  我们一般按F12或者是右键查看源代码。接着我们点击ctrl+f后快速查找,根据题目给的格式我们搜索c

10125-Sumsets【暴力】

利用n^2的时间枚举所有a[i] + a[j] 利用n^2的时间枚举所有a[i] - a[j] 之后利用n^2时间一个一个找a[i] - a[j]的值是否存在于a[i] + a[j]中 找的时候需要二分查找 另外一点就是注意long long的范围以及四个数是集合内不同的四个元素 15222638 10125 Sumsets Accepted C++ 0.449 2015-03-