fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, m;
  6. cin >> n >> m;
  7.  
  8. int dist[1005]; // max n = 1000
  9. int cnt = 0;
  10.  
  11. for (int i = 0; i < n; i++) {
  12. string row;
  13. cin >> row;
  14.  
  15. int g = -1, s = -1;
  16. for (int j = 0; j < m; j++) {
  17. if (row[j] == 'G') g = j;
  18. if (row[j] == 'S') s = j;
  19. }
  20.  
  21. // Impossible case
  22. if (g > s) {
  23. cout << -1 << "\n";
  24. return 0;
  25. }
  26.  
  27. int d = s - g;
  28. if (d > 0)
  29. dist[cnt++] = d;
  30. }
  31.  
  32. // Sort the array
  33. sort(dist, dist + cnt);
  34.  
  35. // Count distinct distances
  36. int moves = 0;
  37. for (int i = 0; i < cnt; i++) {
  38. if (i == 0 || dist[i] != dist[i - 1])
  39. moves++;
  40. }
  41.  
  42. cout << moves << "\n";
  43. return 0;
  44. }
Success #stdin #stdout 0.01s 5284KB
stdin
1 3
S*G
stdout
-1