TLE 啊!!求帮忙说一下算法
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
const int N=220;
const int mid=101;
char str[N];
int n;
bool dp[mid][N][N],dpt[mid][N][N];
int main()
{
    while (scanf(\"%s%d\",str,&n))
    {
          int m=strlen(str);
          memset(dp,0,sizeof(dp));
          //memset(dpt,0,sizeof(dpt));
          dp[0][mid][mid]=1;
          for (int i=m-1;i>=0;--i)
          {
              for (int p=0;p<=n;++p)
              {
                 for (int x=-m+i;x<=m-i;++x)
                     for (int y=-m+i;y<=m-i;++y)
                         {
                             if (!dp[p][mid+x][mid+y]) continue; 
                             //printf(\"Use %d   %d: (%d,%d)-%d\\n\",i,p,x,y,di);
                             if (str[i]==\'T\')
                             {
                                dpt[p][mid+y][mid-x]=1;
                                dpt[p+1][mid+x][mid+y+1]=1;
                             }
                             else
                             {
                                dpt[p][mid+x][mid+y+1]=1;
                                dpt[p+1][mid+y][mid-x]=1;
                             }
                         } 
              }
              for (int p=0;p<=n;++p)
                 for (int x=-m+i;x<=m-i;++x)
                     for (int y=-m+i;y<=m-i;++y)
                      {
                         dp[p][x+mid][y+mid]=dpt[p][x+mid][y+mid];
                         dpt[p][x+mid][y+mid]=0;
                      }
          }
          int ans=0;
          for (int x=-m;x<=m;++x)
                  for (int y=-m;y<=m;++y)
                      {
                           if (dp[n][x+mid][y+mid] && (x*x+y*y)>ans)
                              ans=x*x+y*y;
                      }
          printf(\"%d\\n\",ans);
    }
}
© 2015 HUST ACMICPC TEAM. All Right Reserved.