Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int pozx[27], pozy[27];
  4. string s;
  5. int m, n, t;
  6. char c;
  7. int sol;
  8. int main()
  9. {
  10. std::ios_base::sync_with_stdio (false);
  11. cin >> m >> n;
  12. getline(cin, s);
  13. for(int i = 1; i <= m; i++)
  14. {
  15. getline(cin, s);
  16. for(int j = 0; j < s.size(); j++)
  17. {
  18. if(s[j] != '.')
  19. {
  20. pozx[s[j] - 'a'] = i;
  21. pozy[s[j] - 'a'] = j + 1;
  22. }
  23. }
  24. }
  25. cin >> t;
  26. getline(cin, s);
  27. for(int i = 1; i <= t; i++)
  28. {
  29. sol = 0;
  30. getline(cin, s);
  31. int pozxcur, pozycur;
  32. if(pozx[s[0] - 'a'] && pozy[s[0] - 'a'])
  33. {
  34. pozxcur = pozx[s[0] - 'a'];
  35. pozycur = pozy[s[0] - 'a'];
  36. bool mere = true;
  37. for(int j = 1; j < s.size() && mere; j++)
  38. {
  39. if(s[j] >= 'a' && s[j] <= 'z')
  40. {
  41. if(!pozx[s[j] - 'a'] || !pozy[s[j] - 'a'])
  42. mere = false;
  43. else
  44. {
  45. sol += abs(pozxcur - pozx[s[j] - 'a']);
  46. sol += abs(pozycur - pozy[s[j] - 'a']);
  47. sol -= min(abs(pozxcur - pozx[s[j] - 'a']), abs(pozycur - pozy[s[j] - 'a']));
  48. pozxcur = pozx[s[j] - 'a'];
  49. pozycur = pozy[s[j] - 'a'];
  50. }
  51. }
  52. }
  53. if(!mere)
  54. cout << -1;
  55. else
  56. cout << sol;
  57. }
  58. else
  59. cout << -1;
  60. cout << '\n';
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement