USACO shuttle

  这道题用广搜即可,只需要加两个优化就行。。 代码如下:

  

/*
    ID: m1500293
    LANG: C++
    PROG: shuttle
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <set>
#include <iostream>

using namespace std;

struct State
{
    string now, ans;
    State(string now="", string ans=""):now(now), ans(ans) {}
};

bool judge(int n, State st)
{
    for(int i=0; i<n; i++)
        if(st.now[i]!='b') return false;
    if(st.now[n]!=' ') return false;
    for(int i=n+1; i<=n+1+n-1; i++)
        if(st.now[i]!='w') return false;
    return true;
}

set<string> vis;
string bfs(int n)
{
    queue<State> que;
    string now = "";
    for(int i=0; i<n; i++) now+='w';
    now+=' ';
    for(int i=0; i<n; i++) now+='b';
    que.push(State(now, ""));
    //cout<<now<<endl;
    vis.insert(now);
    while(!que.empty())
    {
        State tp = que.front(); que.pop();
        if(judge(n, tp)) return tp.ans;
        int idx = 0;
        for(int i=0; i<tp.now.length(); i++) if(tp.now[i]==' ')
        {
            idx = i;
            break;
        }
        int len = tp.now.length();
        string str = tp.now;
        if(idx-2>=0 && str[idx-2]!=str[idx-1] && str[idx-2]=='w')
        {
            swap(str[idx-2], str[idx]);
            if(vis.find(str) == vis.end())
            {
                char c = '0'+idx-2;
                que.push(State(str, tp.ans+c));
                vis.insert(str);
            }
            swap(str[idx-2], str[idx]);
        }
        if(idx-1>=0 && str[idx-1]=='w')
        {
            swap(str[idx-1], str[idx]);
            if(vis.find(str) == vis.end())
            {
                char c = '0'+idx-1;
                que.push(State(str, tp.ans+c));
                vis.insert(str);
            }
            swap(str[idx-1], str[idx]);
        }
        if(idx+1 < len && str[idx+1]=='b')
        {
            swap(str[idx+1], str[idx]);
            if(vis.find(str) == vis.end())
            {
                char c = '0'+idx+1;
                que.push(State(str, tp.ans+c));
                vis.insert(str);
            }
            swap(str[idx+1], str[idx]);
        }
        if(idx+2<len && str[idx+2]!=str[idx+1] && str[idx+2]=='b')
        {
            swap(str[idx], str[idx+2]);
            if(vis.find(str) == vis.end())
            {
                char c = '0' + idx+2;
                que.push(State(str, tp.ans+c));
                vis.insert(str);
            }
            swap(str[idx], str[idx+2]);
        }
    }
    return "";
}

int main()
{
    freopen("shuttle.in", "r", stdin);
    freopen("shuttle.out", "w", stdout);
    int n;
    scanf("%d", &n);
    string s = bfs(n);
    int num = 0;
    for(int i=0; i<s.length(); i++)
    {
        //printf("%c%c", s[i]+1, i==s.length()-1?'\n':' ');
        printf("%d", s[i]-'0'+1);
        num++;
        if(num==20)
        {
            printf("\n");
            num = 0;
        }
        else if(i != s.length()-1)
            printf(" ");
    }
    if(s.length()%20!=0) printf("\n");
    return 0;
}

 

posted @ 2016-01-28 22:06  xing-xing  阅读(144)  评论(0编辑  收藏  举报