首页 > 代码库 > usaco-2.2-lamps-pass

usaco-2.2-lamps-pass

呵呵,这个调试了好一阵子,用上C++的vector<vector<int> >,这个技术很炫。

/*ID: qq104801LANG: C++TASK: lamps*/#include <iostream>#include <fstream>#include <cstring>#include <vector>#include <cstdio>#include <algorithm>using namespace std;int n,c;int on,off;vector<int> ons;vector<int> offs;vector<int> state;int _count;vector<long long> hash;vector<vector<int> > result;bool check(){    for(int i=0;i<on;i++)        if(state[ons[i]-1]!=1)return false;        for(int i=0;i<off;i++)        if(state[offs[i]-1]!=0)return false;        return true;}void press(int i){    switch(i)    {        case 1: for(int i=0;i<n;i++)state[i]=not state[i];break;        case 2: for(int i=0;i<n;i+=2)state[i]=not state[i];break;        case 3: for(int i=1;i<n;i+=2)state[i]=not state[i];break;        case 4: for(int i=0;3*i<n;i++){int k=3*i;state[k]=not state[k];}break;    }}void dfs(int deep){    if(deep>c)return;    if(check())    {        long long sum=0;                for(int i=0;i<n;i++)        {            sum*=10;            sum+=state[i];        }        vector<long long>::iterator it=find(hash.begin(),hash.end(),sum);        if(it==hash.end())        {            _count++;            hash.push_back(sum);            result.push_back(state);        }    }    for(int j=1;j<=4;j++)    {        press(j);        dfs(deep+1);        press(j);    }}void test(){        freopen("lamps.in","r",stdin);    freopen("lamps.out","w",stdout);    cin>>n>>c;    on=off=0;    _count=0;    int _on,_off;    while(cin>>_on && _on != -1){        ons.push_back(_on);on++;    }        while(cin>>_off && _off != -1){        offs.push_back(_off);off++;    }    while(c>4)c-=2;    for(int i=0;i<n;i++)        state.push_back(1);    //cout<<state.size()<<endl;    if(c==0 && off==0)    {        for(int j=0;j<state.size();j++)            cout<<state[j];        cout<<endl;        return;    }    else if(c>0)    {        dfs(0);        sort(result.begin(),result.end());    }        vector<vector<int> >::iterator it=result.begin();    for(;it!=result.end();it++)    {        vector<int> x=*it;        for(int j=0;j<x.size();j++)            cout<<x[j];        cout<<endl;    }    if(_count==0 || (c==0 && off!=0))cout<<"IMPOSSIBLE"<<endl;    }int main () {            test();            return 0;}

test data:

USER: cn tom [qq104801]TASK: lampsLANG: C++Compiling...Compile: OKExecuting...   Test 1: TEST OK [0.005 secs, 3516 KB]   Test 2: TEST OK [0.011 secs, 3516 KB]   Test 3: TEST OK [0.011 secs, 3516 KB]   Test 4: TEST OK [0.005 secs, 3516 KB]   Test 5: TEST OK [0.008 secs, 3516 KB]   Test 6: TEST OK [0.011 secs, 3516 KB]   Test 7: TEST OK [0.014 secs, 3516 KB]   Test 8: TEST OK [0.011 secs, 3516 KB]All tests OK.Your program (‘lamps‘) produced all correct answers! This is your submission #7 for this problem. Congratulations!Here are the test data inputs:------- test 1 ----100-1-1------- test 2 ----100-11 -1------- test 3 ----203-11 3 5 -1------- test 4 ----501001 -1-1------- test 5 ----75250-1-1------- test 6 ----10083941 7 13 19 25 31 37 43 49 55 -164 -1------- test 7 ----100200031 86 23 -142 -1------- test 8 ----1008950-1-1Keep up the good work!Thanks for your submission!

边界判定很..........

usaco-2.2-lamps-pass