#include <iostream>
#include <map>
#include <cstring>
#include <cmath>
#define int long long
using namespace std;
int a, b, l, ans, lenx;
string x;
double len[35][35];
char q[35][35];
bool flag, s[35][35], p[35][35];
map<char, int> m;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
double calc(int x1, int y1, int x2, int y2)
{
return double(sqrt(1.0 * (x1 - x2) * (x1 - x2) + 1.0 * (y1 - y2) * (y1 - y2)));
}
void dfs(int x, int y, int sx, int sy)
{
if(calc(x, y, sx, sy) > l) return ;
if(x < 0 || x > a || y < 0 || y > b) return ;
if(p[x][y] && len[x][y] <= calc(x, y, sx, sy)) return ;
if(q[x][y] >= 'a' && q[x][y] <= 'z')
m[char(q[x][y] - 32)] = 0;
len[x][y] = calc(x, y, sx, sy), p[x][y] = true;
for(int i = 0; i <= 3; i++)
dfs(x + dx[i], y + dy[i], sx, sy);
return ;
}
signed main()
{
cin >> a >> b >> l;
for(char i = 'a'; i <= 'z'; i++) m[i] = -1;
for(char i = 'A'; i <= 'Z'; i++) m[i] = -1;
memset(s, 0, sizeof s);
for(int i = 0; i <= a - 1; i++)
for(int j = 0; j <= b - 1; j++)
{
cin >> q[i][j];
if(q[i][j] != 'S') m[q[i][j]] = 0;
else s[i][j] = 1;
}
for(int i = 0; i <= a - 1; i++)
for(int j = 0; j <= b - 1; j++)
if(s[i][j])
{
flag = true;
break;
}
if(!flag) cout << -1 << endl;
else
{
for(int i = 0; i <= a; i++)
for(int j = 0; j <= b; j++)
if(s[i][j])
dfs(i, j, i, j);
for(char i = 'a'; i <= 'z'; i++)
if(m[i] == 0 && m[i - 32] == -1)
m[i - 32] = 1;
cin >> lenx >> x;
for(int i = 0; i <= lenx - 1; i++)
if(m[x[i]] == -1)
{
cout << -1 << endl;
return 0;
}
else ans += m[x[i]];
cout << ans << endl;
}
return 0;
}
错误的点:

在线等。。。。