I have a problem with 1166 of poj.org...
write a program that passed the testcases of this problem...but it's
time limit exceeded...
please suggest me a solution to solve this...
thanks...

Here is my code:

#include <iostream>
#include <cmath>
#include <vector>
#include <string>

using namespace std;

int main ()
{
        int RESULT = 28;
        string ANSWER;
        string s ( 9 , ' ' );
        for ( int i = 0 ; i < 9 ; i++ )
                cin >> s[i];
        vector <string> str ( 9 );
        str[0] = "110110000";
        str[1] = "111000000";
        str[2] = "011011000";
        str[3] = "100100100";
        str[4] = "010111010";
        str[5] = "001001001";
        str[6] = "000110110";
        str[7] = "000000111";
        str[8] = "000011011";
        for ( int i = 0 ; i < pow ( 4. , 9. ) ; i++ )
        {
                int temp = i;
                int sum = 0;
                string ss = s;
                string ans;
                for ( int j = 0 ; temp != 0 ; j++ )
                {
                        int mod = temp % 4;
                        sum += mod;
                        for ( int k = 0 ; k < 9 ; k++ )
                                ss[k] = '0' + ( ( ( ss[k] - '0' ) + mod * ( 
str[j][k] - '0' ) ) %
4 );
                        temp /= 4;
                        for ( int k = 0 ; k < mod ; k++ )
                                ans.push_back ( '0' + j + 1 );
                }
                if ( ss == "000000000" && sum < RESULT )
                {
                        RESULT = sum;
                        ANSWER = ans;
                }
        }
        for ( int i = 0 ; i < ANSWER.length() - 1 ; i++ )
                cout << ANSWER[i] << " ";
        cout << ANSWER[ ANSWER.length()-1 ] << endl;
        return 0;
}

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to