33,027
社区成员




// 10010_WhereWaldorf.cpp
/**
*
* 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
**/
/**
* 题目分析:(从八个方向找出匹配的字符串)
* 1.先输入一个测试用例的数目,然后输入测试数组的行,列可看成是构成了一个二维字符数组,
* 接下来输入准备输入单词的数目,然后,分别输入各个单词。
* 2.要求,输入的这些单词能分别从数组中的单词首字母所在行列中找出来,
* 这个单词在字符数组中可能是从东,南,西,北,东南,西南,东北,西北八个方向的其中一个顺序读取。
* 3.输入该单词对应二维数组中的首元素的地址。
*
* 下面程序直接暴力了,按照题意,从每个方向与输入的单词作对比。
**/
// RE rutime error
#include <cstdio>
#include <cctype>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
const int maxRow = 50;
const int maxColumn = 50;
char testGroup[maxRow + 10][maxColumn + 10];
const int wordMaxNum = 20;
// 一个单词的最长长度可以计算矩形的对角线, sqrt(50*sqrt(2))会小于100
const int wordMaxLong = 100;
char testWordGroup[wordMaxNum + 5][wordMaxLong];
int main()
{
// freopen("test.in", "r", stdin);
int row, column;
scanf("%d%d", &row, &column);
char cTemp;
for (int i = 0; i < row; ++i){
for (int j = 0; j < column; ++j){
cin >> cTemp;
testGroup[i][j] = tolower(cTemp); // 全部转换为小写
}
}
int wordNum; // 输入的会小于20
scanf("%d", &wordNum);
for (int i = 0; i < wordNum; ++i){
scanf("%s", &(testWordGroup[i][0]));
for (int j = 0; j < strlen(*(testWordGroup + i)); ++j){
testWordGroup[i][j] = tolower(testWordGroup[i][j]);
}
}
// 1. 遍历数组找到相同的首字母
// 2. 从这个首字母开始,向八个方向散开比较东南西北。。。
int result(const int &, const int&, const int&, const int&);
for(int wi = 0; wi < wordNum; ++wi){ // 单个单词
for (int tgi = 0; tgi < row; ++tgi){ // 遍历数组
for (int tgj = 0; tgj < column; ++tgj){
if (testWordGroup[wi][0] == testGroup[tgi][tgj]){ // 能找到原点位置
int wordLong = strlen(*(testWordGroup + wi)); // 计算单个单词的长度
if (result(wi, tgi, tgj, wordLong)){
tgj = column; // 结束行
tgi = row; // 结束列
}
}
}
}
}
return 0;
}
int result(const int & wi, const int& tgi, const int& tgj, const int& wL)
{
int count = 0;
// 1.west
for(int k = 0, j = tgj; k < wL && j >= 0; --j, ++k){
if(testWordGroup[wi][k] == testGroup[tgi][j]){
++count;
}
else{
break;
}
}
// west end and judge if it can be the result
if (wL == count){
printf("%d %d\n", tgi + 1, tgj + 1);
return 1;
}
else{
count = 0; // 归零,换方向,再继续计算
}
// 2.north-west
for(int k = 0, i = tgi, j = tgj; k < wL && i >= 0 && j >= 0; --i, --j, ++k){
if(testWordGroup[wi][k] == testGroup[i][j]){
++count;
}
else{
break;
}
}
// north-west end and judge if it can be the result
if (wL == count){
printf("%d %d\n", tgi + 1, tgj + 1);
return 1;
}
else{
count = 0; // 归零,换方向,再继续计算
}
// 3.north
for(int k = 0, i = tgi; k < wL && i >= 0; --i, ++k){
if(testWordGroup[wi][k] == testGroup[i][tgj]){
++count;
}
else{
break;
}
}
// north end and judge if it can be the result
if (wL == count){
printf("%d %d\n", tgi + 1, tgj + 1);
return 1;
}
else{
count = 0; // 归零,换方向,再继续计算
}
// 4. north-east
for(int k = 0, i = tgi, j = tgj; k < wL && i >= 0 && j < maxRow; --i, ++j, ++k){
if(testWordGroup[wi][k] == testGroup[i][j]){
++count;
}
else{
break;
}
}
// east end and judge if it can be the result
if (wL == count){
printf("%d %d\n", tgi + 1, tgj + 1);
return 1;
}
else{
count = 0; // 归零,换方向,再继续计算
}
// 5.east
for(int k = 0, j = tgj; k < wL && j < maxRow; ++j, ++k){
if(testWordGroup[wi][k] == testGroup[tgi][j]){
++count;
}
else{
break;
}
}
// east end and judge if it can be the result
if (wL == count){
printf("%d %d\n", tgi + 1, tgj + 1);
return 1;
}
else{
count = 0; // 归零,换方向,再继续计算
}
// 6.south-east
for(int k = 0, i = tgi, j = tgj; k < wL && j < maxRow && i < maxColumn; ++j, ++i, ++k){
if(testWordGroup[wi][k] == testGroup[i][j]){
++count;
}
else{
break;
}
}
// south-east end and judge if it can be the result
if (wL == count){
printf("%d %d\n", tgi + 1, tgj + 1);
return 1;
}
else{
count = 0; // 归零,换方向,再继续计算
}
// 7.south
for (int k = 0, i = tgi; k < wL && i < maxRow; ++i, ++k){
if(testWordGroup[wi][k] == testGroup[i][tgj]){
++count;
}
else{
break;
}
}
// south end and judge if it can be the result
if (wL == count){
printf("%d %d\n", tgi + 1, tgj + 1);
return 1;
}
else{
count = 0; // 归零,换方向,再继续计算
}
// 8.south-west
for (int k = 0, i = tgi, j = tgj; k < wL && i < maxRow && j >= 0; --j, ++i, ++k){
if(testWordGroup[wi][k] == testGroup[i][j]){
++count;
}
else{
break;
}
}
// south-west end and judge if it can be the result
if (wL == count){
printf("%d %d\n", tgi + 1, tgj + 1);
return 1;
}
else{
count = 0; // 归零,换方向,再继续计算
}
// 8 directions judge end
return 0;
}