Druid's blog

By Druid, 10 years ago, In English

I'm getting a TLE with this code and I'm carious to know the exact reason , also I want to calculate it's complexity , any help ?

Problem : Here

Submission : Here

Thanks in advance !

  • Vote: I like it
  • -5
  • Vote: I do not like it

»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

include

include

using namespace std; queue < pair< int,int > > st; char a[101][101]; int dx[7] = {0, 1, -1, 0, 0 },dy[7] = {0,0,0,1,-1},n,m; bool used[101][101]; int main () { cin >> n >> m;

for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin >> a[i][j];

for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
if (!used[i][j]) 
  {
       st.push(make_pair(i,j));
       while(!st.empty())
     {
            int x,y;
            x = st.front().first;
        y = st.front().second;
        st.pop();
          if(used[x][y])
          {
            cout<<"Yes";
            return 0;
          } 
          used[x][y] = 1;
          for(int t = 1; t <= 4; t++)
          {
             int ii,jj;
             ii = x + dx[t];
             jj = y + dy[t];
             if ( a[ii][jj] != a[x][y]) 
            continue;
             if (!used[ii][jj])
                st.push(make_pair(ii,jj));

          }
        }
  }
}
 cout <<"No";

}