Tuesday, 28 April 2015

UVA 10309 Turn the Lights Off

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

string tmp[12];

int lightSwitch(int n)
{
    int cnt=0;
    for(int i=0;i<10;i++) //bit check for first row
        if(n & (1<<i))
        {
            cnt++;
            //top-left
            for(int j=max(i-1, 0);j<=min(i+1, 9);j++)
            {
                if(tmp[0][j]=='#') tmp[0][j]='O';
                else tmp[0][j]='#';
            }
            //2nd row
            if(tmp[1][i]=='#') tmp[1][i]='O';
            else tmp[1][i]='#';
        }
    //cout<<n<<" "<<cnt<<endl;
    for(int i=1;i<10;i++)
    {
        for(int j=0;j<10;j++)
            if(tmp[i-1][j]=='O')
            {
                cnt++;
                tmp[i-1][j]=='#'; //above

                for(int k=max(j-1, 0);k<=min(j+1, 9);k++)
                {
                    if(tmp[i][k]=='#') tmp[i][k]='O';
                    else tmp[i][k]='#';
                }

                if(i<9)
                {
                    if(tmp[i+1][j]=='#') tmp[i+1][j]='O';
                    else tmp[i+1][j]='#';
                }
            }
    }
    bool chk=true;
    for(int i=0;i<10;i++)
        if(tmp[9][i]=='O')
            chk=false;
    if(!chk) cnt=1e9;
return cnt;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n, tc, kk=1;
    string s, ss[12];
    while(cin>>s && s!="end")
    {
        for(int i=0;i<10;i++)
            cin>>ss[i];
        int mn=1e9;
        for(int i=0;i<1024;i++)
        {
            for(int j=0;j<10;j++)
                tmp[j]=ss[j];
            int mnn=lightSwitch(i);
            //if(mnn==0) cout<<i<<endl;
            mn=min(mn, mnn);

        }

        if(mn>100) mn=-1;
        cout<<s<<" "<<mn<<"\n";
    }
return 0;
}

No comments:

Post a Comment