p2739 Shuttle Puzzle

观察样例得知就是和离'_'左边最近的'w'交换位置,然后和离'_'右边最近的'b'交换位置,轮流进行。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#define mkp make_pair
using namespace std;
const double EPS=1e-8;
typedef long long lon;
const lon SZ=1030,INF=0x7FFFFFFF,mod=9901;

void work(string str,string tmp,int n)
{
    vector<int> res;
    int pos=str.find('_');
    for(int time=1;;++time)
    {
        if(str==tmp)break;
        for(int i=0;i<min(time,n);++i)
        {
            if(str==tmp)break;
            int dst;
            if(time&1)dst=str.find_last_of('w',pos);
            else dst=str.find('b',pos);
            if(dst==-1)break;
            //cout<<str<<endl;
            //cout<<dst<<endl;
            res.push_back(dst);
            swap(str[pos],str[dst]);
            pos=dst;
        }
    }
    for(int i=0;i<res.size();++i)
    {
        if(i%20!=0)cout<<" ";
        cout<<res[i]+1;
        if((i+1)%20==0)cout<<endl;
    }cout<<endl;
}

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin);
    lon casenum;
    //cin>>casenum;
    //for(lon time=1;time<=casenum;++time)
    {
        int n;
        cin>>n;
        string str(n,'w');
        str+='_';
        str+=string(n,'b');
        string tmp(n,'b');
        tmp+='_';
        tmp+=string(n,'w');
        work(str,tmp,n);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/gaudar/p/9839403.html