Advertisement
Kaidul

B

Mar 5th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. /****************************************************
  2. ***   Problem       :
  3. ***   Author        : Kaidul Islam
  4. ***   E-mail        : ikaidul@yahoo.com
  5. ***   University    : KUET, Dept. of CSE
  6. ***   Team          : KUET_BitMask
  7. ***   Blog          : http://kaidul.efireit.com
  8. ****************************************************/
  9. #include <bits/stdc++.h>
  10.  
  11. using namespace std;
  12.  
  13. #define MEMSET_INF 127
  14. #define MEMSET_HALF_INF 63
  15. #define stream istringstream
  16. #define rep(i,n) for(__typeof(n) i=0; i<(n); i++)
  17. #define repl(i,n) for(__typeof(n) i=1; i<=(n); i++)
  18. #define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
  19. #define forstl(i, s) for (__typeof ((s).end ()) i = (s).begin (); i != (s).end (); ++i)
  20. #define INF (1<<30)
  21. #define PI acos(-1.0)
  22. #define pb push_back
  23. #define ppb pop_back
  24. #define all(x) x.begin(),x.end()
  25. #define mem(x,y) memset(x,y,sizeof(x))
  26. #define memsp(x) mem(x,MEMSET_INF)
  27. #define memdp(x) mem(x,-1)
  28. #define memca(x) mem(x,0)
  29. #define eps 1e-9
  30. #define pii pair<int,int>
  31. #define pmp make_pair
  32. #define ft first
  33. #define sd second
  34. #define vi vector<int>
  35. #define vpii vector<pii>
  36. #define si set<int>
  37. #define msi map<string , int >
  38. #define mis map<int , string >
  39. typedef long long i64;
  40. typedef unsigned long long ui64;
  41. /** function **/
  42. #define SDi(x) sf("%d", &x)
  43. #define SDl(x) sf("%lld", &x)
  44. #define SDs(x) sf("%s", x)
  45. #define SD2(x,y) sf("%d%d", &x, &y)
  46. #define SD3(x,y,z) sf("%d%d%d", &x, &y, &z)
  47. #define pf printf
  48. #define print(x) pf("%d ", x)
  49. #define println(x) pf("%d\n", x)
  50. #define sf scanf
  51. #define READ(f) freopen(f, "r", stdin)
  52. #define WRITE(f) freopen(f, "w", stdout)
  53.  
  54. const i64 INF64 = (i64)1E18;
  55.  
  56. template<class T> inline T gcd(T a,T b) {
  57.     if(a<0)return gcd(-a,b);
  58.     if(b<0)return gcd(a,-b);
  59.     return (b==0)?a:gcd(b,a%b);
  60. }
  61. template<class T> inline T lcm(T a,T b) {
  62.     if(a<0)return lcm(-a,b);
  63.     if(b<0)return lcm(a, -b);
  64.     return a*(b/gcd(a,b));
  65. }
  66. template<class T> inline T sqr(T x) {
  67.     return x*x;
  68. }
  69. template<class T> T power(T N,T P) {
  70.     return (P==0)?  1: N*power(N,P-1);
  71. }
  72. template<class T> bool inside(T a,T b,T c) {
  73.     return (b>=a && b<=c);
  74. }
  75. double _dist(double x1,double y1,double x2,double y2) {
  76.     return sqrt(sqr(x1-x2)+sqr(y1-
  77.                                y2));
  78. }
  79. int distsq(int x1,int y1,int x2,int y2) {
  80.     return sqr(x1-x2)+sqr(y1-y2);
  81. }
  82. i64 toInt64(string s) {
  83.     i64 r=0;
  84.     istringstream sin(s);
  85.     sin>>r;
  86.     return r;
  87. }
  88. double Log(i64 N, i64 B) {
  89.     return (log10l(N)) / (log10l(B));
  90. }
  91. string itoa(long long a) {
  92.     if(a==0) return "0";
  93.     string ret;
  94.     for(long long i=a; i>0; i=i/10)
  95.         ret.push_back((i%10)+48);
  96.     reverse(ret.begin(),ret.end());
  97.     return ret;
  98. }
  99. vector< string > token( string a, string b ) {
  100.     const char *q = a.c_str();
  101.     while( count( b.begin(), b.end(), *q ) ) q++;
  102.     vector< string > oot;
  103.     while( *q ) {
  104.         const char *e = q;
  105.         while( *e && !count( b.begin(), b.end(), *e ) )
  106.             e++;
  107.         oot.push_back( string( q, e ) );
  108.         q = e;
  109.         while( count( b.begin(), b.end(), *q ) )
  110.             q++;
  111.     }
  112.     return oot;
  113. }
  114.  
  115. int isvowel(char s) {
  116.     s=tolower(s);
  117.     if(s=='a' or s=='e' or s=='i' or s=='o' or s=='u')
  118.         return 1;
  119.     return 0;
  120. }
  121. int isupper(char s) {
  122.     if(s>='A' and s<='Z') return 1;
  123.     return 0;
  124. }
  125. template<class T> struct Fraction {
  126.     T a,b;
  127.     Fraction(T a=0,T b=1);
  128.     string
  129.     toString();
  130. };//NOTES:Fraction
  131. template<class T> Fraction<T>::Fraction(T a,T b) {
  132.     T d=gcd(a,b);
  133.     a/=d;
  134.     b/=d;
  135.     if (b<0) a=-a, b=-b;
  136.     this->a=a;
  137.     this->b=b;
  138. }
  139. template<class T> string Fraction<T>::toString() {
  140.     ostringstream
  141.     sout;
  142.     sout<<a<<"/"<<b;
  143.     return sout.str();
  144. }
  145. template<class T> Fraction<T> operator+(Fraction<T> p,Fraction<T> q) {
  146.     return
  147.         Fraction<T>(p.a*q.b+q.a*p.b,p.b*q.b);
  148. }
  149. template<class T> Fraction<T> operator-(Fraction<T> p,Fraction<T> q) {
  150.     return
  151.         Fraction<T>(p.a*q.b-q.a*p.b,p.b*q.b);
  152. }
  153. template<class T> Fraction<T> operator*(Fraction<T> p,Fraction<T> q) {
  154.     return
  155.         Fraction<T>(p.a*q.a,p.b*q.b);
  156. }
  157. template<class T> Fraction<T> operator/(Fraction<T> p,Fraction<T> q) {
  158.     return
  159.         Fraction<T>(p.a*q.b,p.b*q.a);
  160. }
  161.  
  162. /** BitMask **/
  163. int Set(int N, int pos) {
  164.     return N = N | (1 << pos);
  165. }
  166. int Reset(int N,int pos) {
  167.     return N = N & ~(1 << pos);
  168. }
  169. int Check(int N, int pos) {
  170.     return (N & (1 << pos));
  171. }
  172. int toggle(int N, int pos) {
  173.     if( Check(N, pos) )
  174.         return N = Reset(N, pos);
  175.     return N = Set(N, pos);
  176. }
  177.  
  178. const i64 INFFF = 1e16;
  179.  
  180. int main(void) {
  181.     int tcase, caseNo = 0;
  182. #ifndef ONLINE_JUDGE
  183.     READ("input.txt");
  184. #endif
  185.     int n, m;
  186.     char in;
  187.     int posS, posG;
  188.     int dis;
  189.     bool yes;
  190.     while(cin >> n >> m) {
  191.         yes = true;
  192.         dis = 0;
  193.         rep(i, n) {
  194.             rep(j, m) {
  195.                 cin >> in;
  196.                 if(in == 'S') posS = j;
  197.                 if(in == 'G') posG = j;
  198.             }
  199.             if(posS < posG) yes = false;
  200.             else if(posS == posG){ dis = max(dis, 0); }
  201.             else if(posS == m - 1) dis = max(dis, posS - posG - 1);
  202.             else yes = false;
  203.         }
  204.         if(yes) cout << dis << endl;
  205.         else cout << -1 << endl;
  206.     }
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement