usaco-2.2-lamps-pass

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

/*
ID: qq104801
LANG: 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: lamps
LANG: C++

Compiling...
Compile: OK

Executing...
   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 ----
10
0
-1
-1
------- test 2 ----
10
0
-1
1 -1
------- test 3 ----
20
3
-1
1 3 5 -1
------- test 4 ----
50
100
1 -1
-1
------- test 5 ----
75
250
-1
-1
------- test 6 ----
100
8394
1 7 13 19 25 31 37 43 49 55 -1
64 -1
------- test 7 ----
100
2000
31 86 23 -1
42 -1
------- test 8 ----
100
8950
-1
-1

Keep up the good work!
Thanks for your submission!

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

posted on 2014-09-04 19:43  深蓝无忌  阅读(140)  评论(0编辑  收藏  举报

导航