Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int pozx[27], pozy[27];
- string s;
- int m, n, t;
- char c;
- int sol;
- int main()
- {
- std::ios_base::sync_with_stdio (false);
- cin >> m >> n;
- getline(cin, s);
- for(int i = 1; i <= m; i++)
- {
- getline(cin, s);
- for(int j = 0; j < s.size(); j++)
- {
- if(s[j] != '.')
- {
- pozx[s[j] - 'a'] = i;
- pozy[s[j] - 'a'] = j + 1;
- }
- }
- }
- cin >> t;
- getline(cin, s);
- for(int i = 1; i <= t; i++)
- {
- sol = 0;
- getline(cin, s);
- int pozxcur, pozycur;
- if(pozx[s[0] - 'a'] && pozy[s[0] - 'a'])
- {
- pozxcur = pozx[s[0] - 'a'];
- pozycur = pozy[s[0] - 'a'];
- bool mere = true;
- for(int j = 1; j < s.size() && mere; j++)
- {
- if(s[j] >= 'a' && s[j] <= 'z')
- {
- if(!pozx[s[j] - 'a'] || !pozy[s[j] - 'a'])
- mere = false;
- else
- {
- sol += abs(pozxcur - pozx[s[j] - 'a']);
- sol += abs(pozycur - pozy[s[j] - 'a']);
- sol -= min(abs(pozxcur - pozx[s[j] - 'a']), abs(pozycur - pozy[s[j] - 'a']));
- pozxcur = pozx[s[j] - 'a'];
- pozycur = pozy[s[j] - 'a'];
- }
- }
- }
- if(!mere)
- cout << -1;
- else
- cout << sol;
- }
- else
- cout << -1;
- cout << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement