弱智却繁琐的苦力题。。纯kill time,遍历每个点,向八个方向找是否有符合的字符串,写的蛋疼死了。。虽然简单但提交的人这么少,原来大家都不愿意做这种费力不讨好的题么。。
#include<cstdio>
#include<cstring>
#include<utility>
using namespace std;
namespace
{
int n;
char c[101][101], s[101];
pair<int, int> found(int row, int col, int len)
{
int i, j;
if (row + len <= n)
{
for (i = row; i < row + len; i++)
if (c[i][col] != s[i - row])
break;
if (i == row + len)
return make_pair(i - 1, col);
}
if (row - len + 1 >= 0)
{
for (i = row; i > row - len; i--)
if (c[i][col] != s[row - i])
break;
if (i == row - len)
return make_pair(i + 1, col);
}
if (col + len <= n)
{
for (j = col; j < col + len; j++)
if (c[row][j] != s[j - col])
break;
if (j == col + len)
return make_pair(row, j - 1);
}
if (col - len + 1 >= 0)
{
for (j = col; j > col - len; j--)
if (c[row][j] != s[col - j])
break;
if (j == col - len)
return make_pair(row, j + 1);
}
if (row + len <= n && col + len <= n)
{
for (i = row; i < row + len; i++)
for (j = col; j < col + len; j++)
if (i - row == j - col && c[i][j] != s[i - row])
goto label1;
if (i == row + len && j == col + len)
return make_pair(i - 1, j - 1);
}
label1: if (row + len <= n && col - len + 1 >= 0)
{
for (i = row; i < row + len; i++)
for (j = col; j > col - len; j--)
if (i - row == col - j && c[i][j] != s[i - row])
goto label2;
if (i == row + len && j == col - len)
return make_pair(i - 1, j + 1);
}
label2: if (row - len + 1 >= 0 && col + len <= n)
{
for (i = row; i > row - len; i--)
for (j = col; j < col + len; j++)
if (row - i == j - col && c[i][j] != s[row - i])
goto label3;
if (i == row - len && j == col + len)
return make_pair(i + 1, j - 1);
}
label3: if (row - len + 1 >= 0 && col - len + 1 >= 0)
{
for (i = row; i > row - len; i--)
for (j = col; j > col - len; j--)
if (row - i == col - j && c[i][j] != s[row - i])
goto label4;
if (i == row - len && j == col - len)
return make_pair(i + 1, j + 1);
}
label4: return make_pair(-1, -1);
}
}
int main()
{
int N;
scanf("%d", &N);
for (int t = 0; t < N; t++)
{
if (t)
puts("");
scanf("%d", &n);
getchar();
for (int i = 0; i < n; i++)
gets(c[i]);
while (gets(s), strcmp(s, "0"))
{
int len = strlen(s);
bool flag = false;
for (int i = 0; i < n && !flag; i++)
for (int j = 0; j < n; j++)
if (c[i][j] == s[0])
{
pair<int, int> p = found(i, j, len);
if (p.first != -1 && p.second != -1)
{
printf("%d,%d %d,%d\n", i + 1, j + 1, p.first + 1,
p.second + 1);
flag = true;
break;
}
}
if (!flag)
puts("Not found");
}
}
return 0;
}