Start: Dec, 22, 2016 18:15:00
2016年秋季学期程序设计基础第三次考试
End: Dec, 22, 2016 21:00:00
Time elapsed:
Time remaining:

Problem_ID: G
Result: Accepted
Time: 5ms
Memory: 2564kB
Author: fyk19980409
In contest: 1080

#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;

long long gcd(long long m,long long n)
{
	return n?gcd(n,m%n):m;
}

int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		char map[1000][1000];
		int s[1000];
		int e[1000];
		memset(s,-1,sizeof(s));
		memset(e,-1,sizeof(e));
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				cin>>map[i][j];
				if(s[i]==-1&&map[i][j]=='T')s[i]=j;
				if(map[i][j]=='T')e[i]=j;
			}
		}
		int tot=0;
		int tmp=0;
		for(int i=0;i<n-1;i++)
		{
			if(i%2==0)
			{
				tot+=max(e[i],e[i+1])-tmp;
				tmp=max(e[i],e[i+1]);
			}else
			{
				tot+=tmp-min(s[i],s[i+1]);
				tmp=min(s[i],s[i+1]);
			}
		}
//		cout<<tot<<endl;
		if(n%2==0)tot+=tmp-s[n-1];
		else tot+=e[n-1]-tmp;
		tot+=n-1;
		if(map[0][0]=='T');
		cout<<tot<<endl;
	}
}